NOTE: After an Xcode update to Swift 4.2,
NIO
,NIOFoundationCompat
,NIOHTTP1
, andNIOHTTP1
warnings can be silenced by setting theSWIFT_VERSION
to "Swift 4" in the Target build settings.
Example Routes • Components • Use • Resources
VaporApiWebTemplate
is an API + Web Template (AWT) hybrid based on vapor/api-template and vapor/web-template. VaporApiWebTemplate
template uses Vapor 3, Swift 4.1 and Xcode 9.
VaporApiWebTemplate
also includes a set of lightweight examples:
- api routes which use SQLite (
TextpostApiController
,Textpost
) - web page routes which serve static files from "Public/" (
FileMiddleware
enabled) - web page routes which use Leaf templating (
LeafWebController
) - simple custom
Middleware
(ExampleMiddleware
) - simple custom
Service
(ExampleService
,ExampleFortuneService
,ExampleQuoteService
,ExampleServiceProvider
) - UUID extension for RFC 4122 compliance
XCTest
unit test examples
Example Routes ▴
Example routes setup in the baseline template.
Basic
http://localhost:8080/hello
Hello, world!
http://localhost:8080/hellojson
{"hello":"world"}
http://localhost:8080/info
Returns a description of http request.
GET /api/info HTTP/1.1
Host: localhost:8080
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
User-Agent: …
<no body>
Dynamic API (Fluent)
/api/textposts
POST creates one database entry for valid JSONTextpost
./api/textposts
GET returns an array of all availableTextpost
/api/textposts/uuid
GET returns one specificTextpost
/api/textposts
PUT updates an existingTextpost
or create a newTextpost
./api/textposts/uuid
PATCH applies partial update modification to specificTextpost
entry./api/textposts/uuid
DELETE removes one specificTextpost
.
Static HTML
http://localhost:8080/
redirects to '/index.html'.http://localhost:8080/index.html
minimal HTML 5.
Web HTML Templates (Leaf)
http://localhost:8080/leaf
simple leaf example.http://localhost:8080/leaf/hello
http://localhost:8080/leaf/hello/Sunshine
leaf with aString
parameter.
<!DOCTYPE html>
<html>
<head>
<title>Hello, Sunshine!</title>
<link rel="stylesheet" href="/styles/app.css">
</head>
<body>
<h1>Hello, Sunshine!</h1>
</body>
</html>
http://localhost:8080/leaf/bootstrap
example page with Bootstrap, Swift data objects and Leaf.
Components ▴
Sources/App/
Controllers/
TextpostController.swift
Models/
Textpost.swift
Tests/AppTests/
TextpostControllerTests.swift
Leaf templating components.
Sources/App/
Controllers/
LeafWebController.swift
Public/
images/it-works.png
scripts/.gitkeep
styles/app.css
Resources/
Views/
bootstrap_web.leaf
bootstrap_welcome.leaf
web_base.leaf
web_hello.leaf
Tests/AppTests/
LeafWebControllerTests.swift
Use ▴
vapor new PROJECT_NAME --template=marc-medley/004.77_VaporApiWebTemplate
cd PROJECT_NAME
vapor update
Set the Xcode active scheme to Run > My Mac
.
Resources ▴
Atom: language-leaf ⇗
Mac App Store: RESTed - Simple HTTP Requests ⇗ …free.