Repository: vapor/api-template Branch: master Commit: 7d99b769098a Files: 22 Total size: 13.7 KB Directory structure: gitextract_pbyux0u_/ ├── .circleci/ │ └── config.yml ├── .dockerignore ├── .gitignore ├── CONTRIBUTING.md ├── Package.resolved ├── Package.swift ├── Public/ │ └── .gitkeep ├── README.md ├── Sources/ │ ├── App/ │ │ ├── Controllers/ │ │ │ ├── .gitkeep │ │ │ └── TodoController.swift │ │ ├── Models/ │ │ │ ├── .gitkeep │ │ │ └── Todo.swift │ │ ├── app.swift │ │ ├── boot.swift │ │ ├── configure.swift │ │ └── routes.swift │ └── Run/ │ └── main.swift ├── Tests/ │ ├── .gitkeep │ ├── AppTests/ │ │ └── AppTests.swift │ └── LinuxMain.swift ├── cloud.yml └── web.Dockerfile ================================================ FILE CONTENTS ================================================ ================================================ FILE: .circleci/config.yml ================================================ version: 2 jobs: linux: docker: - image: swift:4.1 steps: - checkout - run: name: Compile code command: swift build - run: name: Run unit tests command: swift test linux-release: docker: - image: swift:4.1 steps: - checkout - run: name: Compile code with optimizations command: swift build -c release workflows: version: 2 tests: jobs: - linux - linux-release nightly: triggers: - schedule: cron: "0 0 * * *" filters: branches: only: - master jobs: - linux - linux-release ================================================ FILE: .dockerignore ================================================ .git .build DerivedData Package.resolved *.xcodeproj ================================================ FILE: .gitignore ================================================ Packages .build xcuserdata *.xcodeproj DerivedData/ .DS_Store .swiftpm db.sqlite ================================================ FILE: CONTRIBUTING.md ================================================ # Contributing to Vapor API Template If you found a mistake or think of a cool new feature, please [create an issue](https://github.com/vapor/api-template/issues/new) or, if you want to implement it yourself, [fork this repo](https://github.com/vapor/api-template/fork) and open a Pull Request! We'll take a look as soon as we can. Thanks! ## Maintainers - [@0xTim](https://github.com/0xTim) See the [Vapor maintainers doc](https://github.com/vapor/vapor/blob/master/Docs/maintainers.md) for more information. ================================================ FILE: Package.resolved ================================================ { "object": { "pins": [ { "package": "Console", "repositoryURL": "https://github.com/vapor/console.git", "state": { "branch": null, "revision": "74cfbea629d4aac34a97cead2447a6870af1950b", "version": "3.1.1" } }, { "package": "Core", "repositoryURL": "https://github.com/vapor/core.git", "state": { "branch": null, "revision": "18f2436bf7a6bc2224372c0885db2e0159af1649", "version": "3.9.2" } }, { "package": "Crypto", "repositoryURL": "https://github.com/vapor/crypto.git", "state": { "branch": null, "revision": "df8eb7d8ae51787b3a0628aa3975e67666da936c", "version": "3.3.3" } }, { "package": "DatabaseKit", "repositoryURL": "https://github.com/vapor/database-kit.git", "state": { "branch": null, "revision": "8f352c8e66dab301ab9bfef912a01ce1361ba1e4", "version": "1.3.3" } }, { "package": "Fluent", "repositoryURL": "https://github.com/vapor/fluent.git", "state": { "branch": null, "revision": "b915c321c6f9e83743ee5efa35a30895e1b02e51", "version": "3.2.0" } }, { "package": "FluentSQLite", "repositoryURL": "https://github.com/vapor/fluent-sqlite.git", "state": { "branch": null, "revision": "c32f5bda84bf4ea691d19afe183d40044f579e11", "version": "3.0.0" } }, { "package": "HTTP", "repositoryURL": "https://github.com/vapor/http.git", "state": { "branch": null, "revision": "3808ed0401379b6e9f4a053f03090ea9d658caa9", "version": "3.2.1" } }, { "package": "Multipart", "repositoryURL": "https://github.com/vapor/multipart.git", "state": { "branch": null, "revision": "f919a01c4d10a281d6236a21b0b1d1759a72b8eb", "version": "3.0.4" } }, { "package": "Routing", "repositoryURL": "https://github.com/vapor/routing.git", "state": { "branch": null, "revision": "d76f339c9716785e5079af9d7075d28ff7da3d92", "version": "3.1.0" } }, { "package": "Service", "repositoryURL": "https://github.com/vapor/service.git", "state": { "branch": null, "revision": "fa5b5de62bd68bcde9a69933f31319e46c7275fb", "version": "1.0.2" } }, { "package": "SQL", "repositoryURL": "https://github.com/vapor/sql.git", "state": { "branch": null, "revision": "50eaeb8f52a1ce63f1ff3880e1114dd8757a78a6", "version": "2.3.2" } }, { "package": "SQLite", "repositoryURL": "https://github.com/vapor/sqlite.git", "state": { "branch": null, "revision": "314d9cd21165bcf14215e336a23ff8214f40e411", "version": "3.2.1" } }, { "package": "swift-nio", "repositoryURL": "https://github.com/apple/swift-nio.git", "state": { "branch": null, "revision": "ba7970fe396e8198b84c6c1b44b38a1d4e2eb6bd", "version": "1.14.1" } }, { "package": "swift-nio-ssl", "repositoryURL": "https://github.com/apple/swift-nio-ssl.git", "state": { "branch": null, "revision": "0f3999f3e3c359cc74480c292644c3419e44a12f", "version": "1.4.0" } }, { "package": "swift-nio-ssl-support", "repositoryURL": "https://github.com/apple/swift-nio-ssl-support.git", "state": { "branch": null, "revision": "c02eec4e0e6d351cd092938cf44195a8e669f555", "version": "1.0.0" } }, { "package": "swift-nio-zlib-support", "repositoryURL": "https://github.com/apple/swift-nio-zlib-support.git", "state": { "branch": null, "revision": "37760e9a52030bb9011972c5213c3350fa9d41fd", "version": "1.0.0" } }, { "package": "TemplateKit", "repositoryURL": "https://github.com/vapor/template-kit.git", "state": { "branch": null, "revision": "51405c83e95e8adb09565278a5e9b959c605e56c", "version": "1.4.0" } }, { "package": "URLEncodedForm", "repositoryURL": "https://github.com/vapor/url-encoded-form.git", "state": { "branch": null, "revision": "82d8d63bdb76b6dd8febe916c639ab8608dbbaed", "version": "1.0.6" } }, { "package": "Validation", "repositoryURL": "https://github.com/vapor/validation.git", "state": { "branch": null, "revision": "4de213cf319b694e4ce19e5339592601d4dd3ff6", "version": "2.1.1" } }, { "package": "Vapor", "repositoryURL": "https://github.com/vapor/vapor.git", "state": { "branch": null, "revision": "92a58a9a84e4330500b99fe355a94d29f67abe58", "version": "3.3.1" } }, { "package": "WebSocket", "repositoryURL": "https://github.com/vapor/websocket.git", "state": { "branch": null, "revision": "d85e5b6dce4d04065865f77385fc3324f98178f6", "version": "1.1.2" } } ] }, "version": 1 } ================================================ FILE: Package.swift ================================================ // swift-tools-version:4.0 import PackageDescription let package = Package( name: "VaporApp", products: [ .library(name: "VaporApp", targets: ["App"]), ], dependencies: [ // 💧 A server-side Swift web framework. .package(url: "https://github.com/vapor/vapor.git", from: "3.0.0"), // 🔵 Swift ORM (queries, models, relations, etc) built on SQLite 3. .package(url: "https://github.com/vapor/fluent-sqlite.git", from: "3.0.0") ], targets: [ .target(name: "App", dependencies: ["FluentSQLite", "Vapor"]), .target(name: "Run", dependencies: ["App"]), .testTarget(name: "AppTests", dependencies: ["App"]) ] ) ================================================ FILE: Public/.gitkeep ================================================ ================================================ FILE: README.md ================================================

API Template

Documentation Team Chat MIT License Continuous Integration Swift 5.1

================================================ FILE: Sources/App/Controllers/.gitkeep ================================================ ================================================ FILE: Sources/App/Controllers/TodoController.swift ================================================ import Vapor /// Controls basic CRUD operations on `Todo`s. final class TodoController { /// Returns a list of all `Todo`s. func index(_ req: Request) throws -> Future<[Todo]> { return Todo.query(on: req).all() } /// Saves a decoded `Todo` to the database. func create(_ req: Request) throws -> Future { return try req.content.decode(Todo.self).flatMap { todo in return todo.save(on: req) } } /// Deletes a parameterized `Todo`. func delete(_ req: Request) throws -> Future { return try req.parameters.next(Todo.self).flatMap { todo in return todo.delete(on: req) }.transform(to: .ok) } } ================================================ FILE: Sources/App/Models/.gitkeep ================================================ ================================================ FILE: Sources/App/Models/Todo.swift ================================================ import FluentSQLite import Vapor /// A single entry of a Todo list. final class Todo: SQLiteModel { typealias Database = SQLiteDatabase /// The unique identifier for this `Todo`. var id: Int? /// A title describing what this `Todo` entails. var title: String /// Creates a new `Todo`. init(id: Int? = nil, title: String) { self.id = id self.title = title } } /// Allows `Todo` to be used as a dynamic migration. extension Todo: Migration { } /// Allows `Todo` to be encoded to and decoded from HTTP messages. extension Todo: Content { } /// Allows `Todo` to be used as a dynamic parameter in route definitions. extension Todo: Parameter { } ================================================ FILE: Sources/App/app.swift ================================================ import Vapor /// Creates an instance of `Application`. This is called from `main.swift` in the run target. public func app(_ env: Environment) throws -> Application { var config = Config.default() var env = env var services = Services.default() try configure(&config, &env, &services) let app = try Application(config: config, environment: env, services: services) try boot(app) return app } ================================================ FILE: Sources/App/boot.swift ================================================ import Vapor /// Called after your application has initialized. public func boot(_ app: Application) throws { // Your code here } ================================================ FILE: Sources/App/configure.swift ================================================ import FluentSQLite import Vapor /// Called before your application initializes. public func configure(_ config: inout Config, _ env: inout Environment, _ services: inout Services) throws { // Register providers first try services.register(FluentSQLiteProvider()) // Register routes to the router let router = EngineRouter.default() try routes(router) services.register(router, as: Router.self) // Register middleware var middlewares = MiddlewareConfig() // Create _empty_ middleware config // middlewares.use(FileMiddleware.self) // Serves files from `Public/` directory middlewares.use(ErrorMiddleware.self) // Catches errors and converts to HTTP response services.register(middlewares) // Configure a SQLite database let sqlite = try SQLiteDatabase(storage: .memory) // Register the configured SQLite database to the database config. var databases = DatabasesConfig() databases.add(database: sqlite, as: .sqlite) services.register(databases) // Configure migrations var migrations = MigrationConfig() migrations.add(model: Todo.self, database: .sqlite) services.register(migrations) } ================================================ FILE: Sources/App/routes.swift ================================================ import Vapor /// Register your application's routes here. public func routes(_ router: Router) throws { // Basic "It works" example router.get { req in return "It works!" } // Basic "Hello, world!" example router.get("hello") { req in return "Hello, world!" } // Example of configuring a controller let todoController = TodoController() router.get("todos", use: todoController.index) router.post("todos", use: todoController.create) router.delete("todos", Todo.parameter, use: todoController.delete) } ================================================ FILE: Sources/Run/main.swift ================================================ import App try app(.detect()).run() ================================================ FILE: Tests/.gitkeep ================================================ ================================================ FILE: Tests/AppTests/AppTests.swift ================================================ import App import XCTest final class AppTests: XCTestCase { func testNothing() throws { // Add your tests here XCTAssert(true) } static let allTests = [ ("testNothing", testNothing) ] } ================================================ FILE: Tests/LinuxMain.swift ================================================ ================================================ FILE: cloud.yml ================================================ type: "vapor" swift_version: "5.1.1" run_parameters: "serve --port 8080 --hostname 0.0.0.0" ================================================ FILE: web.Dockerfile ================================================ # You can set the Swift version to what you need for your app. Versions can be found here: https://hub.docker.com/_/swift FROM swift:5.1.1 as builder # For local build, add `--build-arg env=docker` # In your application, you can use `Environment.custom(name: "docker")` to check if you're in this env ARG env RUN apt-get -qq update && apt-get install -y \ libssl-dev zlib1g-dev \ && rm -r /var/lib/apt/lists/* WORKDIR /app COPY . . RUN mkdir -p /build/lib && cp -R /usr/lib/swift/linux/*.so* /build/lib RUN swift build -c release && mv `swift build -c release --show-bin-path` /build/bin # Production image FROM ubuntu:18.04 ARG env # DEBIAN_FRONTEND=noninteractive for automatic UTC configuration in tzdata RUN apt-get -qq update && DEBIAN_FRONTEND=noninteractive apt-get install -y \ libatomic1 libicu60 libxml2 libcurl4 libz-dev libbsd0 tzdata \ && rm -r /var/lib/apt/lists/* WORKDIR /app COPY --from=builder /build/bin/Run . COPY --from=builder /build/lib/* /usr/lib/ # Uncomment the next line if you need to load resources from the `Public` directory #COPY --from=builder /app/Public ./Public # Uncomment the next line if you are using Leaf #COPY --from=builder /app/Resources ./Resources ENV ENVIRONMENT=$env ENTRYPOINT ./Run serve --env $ENVIRONMENT --hostname 0.0.0.0 --port 80