/VaporApiWebTemplate

A starting point for vapor api (Fluent) + web (Leaf) applications.

Primary LanguageSwiftMIT LicenseMIT

API + Web Template

Documentation MIT License Swift 4.2

NOTE: After an Xcode update to Swift 4.2, NIO, NIOFoundationCompat, NIOHTTP1, and NIOHTTP1 warnings can be silenced by setting the SWIFT_VERSION to "Swift 4" in the Target build settings.

Example RoutesComponentsUseResources

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 JSON Textpost.
  • /api/textposts GET returns an array of all available Textpost
  • /api/textposts/uuid GET returns one specific Textpost
  • /api/textposts PUT updates an existing Textpost or create a new Textpost.
  • /api/textposts/uuid PATCH applies partial update modification to specific Textpost entry.
  • /api/textposts/uuid DELETE removes one specific Textpost.

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 a String 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

API Fluent

Sources/App/
  Controllers/
    TextpostController.swift
  Models/
    Textpost.swift
Tests/AppTests/
  TextpostControllerTests.swift

Web Leaf Template

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.