NOTE: After an Xcode update to Swift 4.2,
NIO,NIOFoundationCompat,NIOHTTP1, andNIOHTTP1warnings can be silenced by setting theSWIFT_VERSIONto "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/" (
FileMiddlewareenabled) - 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
XCTestunit 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/textpostsPOST creates one database entry for valid JSONTextpost./api/textpostsGET returns an array of all availableTextpost/api/textposts/uuidGET returns one specificTextpost/api/textpostsPUT updates an existingTextpostor create a newTextpost./api/textposts/uuidPATCH applies partial update modification to specificTextpostentry./api/textposts/uuidDELETE removes one specificTextpost.
Static HTML
http://localhost:8080/redirects to '/index.html'.http://localhost:8080/index.htmlminimal HTML 5.
Web HTML Templates (Leaf)
http://localhost:8080/leafsimple leaf example.http://localhost:8080/leaf/hellohttp://localhost:8080/leaf/hello/Sunshineleaf with aStringparameter.
<!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/bootstrapexample 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 updateSet the Xcode active scheme to Run > My Mac.
Resources ▴
Atom: language-leaf ⇗
Mac App Store: RESTed - Simple HTTP Requests ⇗ …free.

