From 5227b6841ccc986f604ce59f3d99c5b8e40e57f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ug=CC=A7is=20Lazdin=CC=A7s=CC=8C?= Date: Sat, 3 Aug 2019 09:20:17 +0300 Subject: [PATCH] 'created Hello from template https://github.com/vapor/api-template' --- .circleci/config.yml | 43 ++++ .dockerignore | 6 + .gitignore | 6 + CONTRIBUTING.md | 13 ++ Package.resolved | 196 +++++++++++++++++++ Package.swift | 22 +++ Public/.gitkeep | 0 README.md | 20 ++ Sources/App/Controllers/.gitkeep | 0 Sources/App/Controllers/TodoController.swift | 23 +++ Sources/App/Models/.gitkeep | 0 Sources/App/Models/Todo.swift | 26 +++ Sources/App/app.swift | 12 ++ Sources/App/boot.swift | 6 + Sources/App/configure.swift | 32 +++ Sources/App/routes.swift | 20 ++ Sources/Run/main.swift | 3 + Tests/.gitkeep | 0 Tests/AppTests/AppTests.swift | 13 ++ Tests/LinuxMain.swift | 0 cloud.yml | 3 + web.Dockerfile | 32 +++ 22 files changed, 476 insertions(+) create mode 100644 .circleci/config.yml create mode 100644 .dockerignore create mode 100644 .gitignore create mode 100644 CONTRIBUTING.md create mode 100644 Package.resolved create mode 100644 Package.swift create mode 100644 Public/.gitkeep create mode 100644 README.md create mode 100644 Sources/App/Controllers/.gitkeep create mode 100644 Sources/App/Controllers/TodoController.swift create mode 100644 Sources/App/Models/.gitkeep create mode 100644 Sources/App/Models/Todo.swift create mode 100644 Sources/App/app.swift create mode 100644 Sources/App/boot.swift create mode 100644 Sources/App/configure.swift create mode 100644 Sources/App/routes.swift create mode 100644 Sources/Run/main.swift create mode 100644 Tests/.gitkeep create mode 100644 Tests/AppTests/AppTests.swift create mode 100644 Tests/LinuxMain.swift create mode 100644 cloud.yml create mode 100644 web.Dockerfile diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 0000000..a6c25ad --- /dev/null +++ b/.circleci/config.yml @@ -0,0 +1,43 @@ +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 + diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..0973f96 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,6 @@ +.git +.build +DerivedData +Package.resolved +*.xcodeproj + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..189daed --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +Packages +.build +xcuserdata +*.xcodeproj +DerivedData/ +.DS_Store diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..5eae5e8 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,13 @@ +# 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. diff --git a/Package.resolved b/Package.resolved new file mode 100644 index 0000000..fe44578 --- /dev/null +++ b/Package.resolved @@ -0,0 +1,196 @@ +{ + "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": "727ae2905ebdca162c5da5bfe187c406555859e2", + "version": "3.7.3" + } + }, + { + "package": "Crypto", + "repositoryURL": "https://github.com/vapor/crypto.git", + "state": { + "branch": null, + "revision": "45bb12d13cdec80dbd1cc0685ea002e51ab83439", + "version": "3.3.2" + } + }, + { + "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": "3776a0f623e08b413e878f282a70e8952651c91f", + "version": "3.1.3" + } + }, + { + "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": "8da7d475a1a060714766d9ad4b24eb0dae243aab", + "version": "3.1.11" + } + }, + { + "package": "Multipart", + "repositoryURL": "https://github.com/vapor/multipart.git", + "state": { + "branch": null, + "revision": "bd7736c5f28e48ed8b683dcc9df3dcd346064c2b", + "version": "3.0.3" + } + }, + { + "package": "Routing", + "repositoryURL": "https://github.com/vapor/routing.git", + "state": { + "branch": null, + "revision": "626190ddd2bd9f967743b60ba6adaf90bbd2651c", + "version": "3.0.2" + } + }, + { + "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": "29a9f2aca71c8afb07e291336f1789337ce235dd", + "version": "1.13.2" + } + }, + { + "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": "4b1073d74be9f5c6a5bc63a07a84e83efec26229", + "version": "1.1.2" + } + }, + { + "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": "c86ada59b31c69f08a6abd4f776537cba48d5df6", + "version": "3.3.0" + } + }, + { + "package": "WebSocket", + "repositoryURL": "https://github.com/vapor/websocket.git", + "state": { + "branch": null, + "revision": "d85e5b6dce4d04065865f77385fc3324f98178f6", + "version": "1.1.2" + } + } + ] + }, + "version": 1 +} diff --git a/Package.swift b/Package.swift new file mode 100644 index 0000000..2d023ef --- /dev/null +++ b/Package.swift @@ -0,0 +1,22 @@ +// swift-tools-version:4.0 +import PackageDescription + +let package = Package( + name: "Hello", + products: [ + .library(name: "Hello", 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"]) + ] +) + diff --git a/Public/.gitkeep b/Public/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/README.md b/README.md new file mode 100644 index 0000000..a8a513e --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +

+ API Template +
+
+ + Documentation + + + Team Chat + + + MIT License + + + Continuous Integration + + + Swift 4.1 + +

diff --git a/Sources/App/Controllers/.gitkeep b/Sources/App/Controllers/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Sources/App/Controllers/TodoController.swift b/Sources/App/Controllers/TodoController.swift new file mode 100644 index 0000000..c682315 --- /dev/null +++ b/Sources/App/Controllers/TodoController.swift @@ -0,0 +1,23 @@ +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) + } +} diff --git a/Sources/App/Models/.gitkeep b/Sources/App/Models/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Sources/App/Models/Todo.swift b/Sources/App/Models/Todo.swift new file mode 100644 index 0000000..5c77e09 --- /dev/null +++ b/Sources/App/Models/Todo.swift @@ -0,0 +1,26 @@ +import FluentSQLite +import Vapor + +/// A single entry of a Todo list. +final class Todo: SQLiteModel { + /// 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 { } diff --git a/Sources/App/app.swift b/Sources/App/app.swift new file mode 100644 index 0000000..c1ecaaa --- /dev/null +++ b/Sources/App/app.swift @@ -0,0 +1,12 @@ +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 +} diff --git a/Sources/App/boot.swift b/Sources/App/boot.swift new file mode 100644 index 0000000..9313115 --- /dev/null +++ b/Sources/App/boot.swift @@ -0,0 +1,6 @@ +import Vapor + +/// Called after your application has initialized. +public func boot(_ app: Application) throws { + // Your code here +} diff --git a/Sources/App/configure.swift b/Sources/App/configure.swift new file mode 100644 index 0000000..240cf86 --- /dev/null +++ b/Sources/App/configure.swift @@ -0,0 +1,32 @@ +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) +} diff --git a/Sources/App/routes.swift b/Sources/App/routes.swift new file mode 100644 index 0000000..6ea2af9 --- /dev/null +++ b/Sources/App/routes.swift @@ -0,0 +1,20 @@ +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) +} diff --git a/Sources/Run/main.swift b/Sources/Run/main.swift new file mode 100644 index 0000000..ade3aff --- /dev/null +++ b/Sources/Run/main.swift @@ -0,0 +1,3 @@ +import App + +try app(.detect()).run() diff --git a/Tests/.gitkeep b/Tests/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Tests/AppTests/AppTests.swift b/Tests/AppTests/AppTests.swift new file mode 100644 index 0000000..121c89a --- /dev/null +++ b/Tests/AppTests/AppTests.swift @@ -0,0 +1,13 @@ +import App +import XCTest + +final class AppTests: XCTestCase { + func testNothing() throws { + // Add your tests here + XCTAssert(true) + } + + static let allTests = [ + ("testNothing", testNothing) + ] +} diff --git a/Tests/LinuxMain.swift b/Tests/LinuxMain.swift new file mode 100644 index 0000000..e69de29 diff --git a/cloud.yml b/cloud.yml new file mode 100644 index 0000000..dee0d1d --- /dev/null +++ b/cloud.yml @@ -0,0 +1,3 @@ +type: "vapor" +swift_version: "4.1.0" +run_parameters: "serve --port 8080 --hostname 0.0.0.0" diff --git a/web.Dockerfile b/web.Dockerfile new file mode 100644 index 0000000..79b20f7 --- /dev/null +++ b/web.Dockerfile @@ -0,0 +1,32 @@ +# 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.0 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