'created WSExample1 from template https://github.com/vapor/api-template'

This commit is contained in:
ilyakooo0 2018-11-29 19:13:59 +03:00
commit c3c3690f69
21 changed files with 458 additions and 0 deletions

43
.circleci/config.yml Normal file
View File

@ -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

6
.dockerignore Normal file
View File

@ -0,0 +1,6 @@
.git
.build
DerivedData
Package.resolved
*.xcodeproj

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
Packages
.build
xcuserdata
*.xcodeproj
DerivedData/
.DS_Store

196
Package.resolved Normal file
View File

@ -0,0 +1,196 @@
{
"object": {
"pins": [
{
"package": "Console",
"repositoryURL": "https://github.com/vapor/console.git",
"state": {
"branch": null,
"revision": "5b9796d39f201b3dd06800437abd9d774a455e57",
"version": "3.0.2"
}
},
{
"package": "Core",
"repositoryURL": "https://github.com/vapor/core.git",
"state": {
"branch": null,
"revision": "eb876a758733166a4fb20f3f0a17b480c5ea563e",
"version": "3.4.3"
}
},
{
"package": "Crypto",
"repositoryURL": "https://github.com/vapor/crypto.git",
"state": {
"branch": null,
"revision": "5605334590affd4785a5839806b4504407e054ac",
"version": "3.3.0"
}
},
{
"package": "DatabaseKit",
"repositoryURL": "https://github.com/vapor/database-kit.git",
"state": {
"branch": null,
"revision": "3a17dbbe9be5f8c37703e4b7982c1332ad6b00c4",
"version": "1.3.1"
}
},
{
"package": "Fluent",
"repositoryURL": "https://github.com/vapor/fluent.git",
"state": {
"branch": null,
"revision": "270b6fa372f03809b9795e8f8b9d1c31267a0ff3",
"version": "3.0.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": "272b22be8cb3364e42a4701c2e0676e37480ec5a",
"version": "3.1.5"
}
},
{
"package": "Multipart",
"repositoryURL": "https://github.com/vapor/multipart.git",
"state": {
"branch": null,
"revision": "e57007c23a52b68e44ebdfc70cbe882a7c4f1ec3",
"version": "3.0.2"
}
},
{
"package": "Routing",
"repositoryURL": "https://github.com/vapor/routing.git",
"state": {
"branch": null,
"revision": "3219e328491b0853b8554c5a694add344d2c6cfb",
"version": "3.0.1"
}
},
{
"package": "Service",
"repositoryURL": "https://github.com/vapor/service.git",
"state": {
"branch": null,
"revision": "281a70b69783891900be31a9e70051b6fe19e146",
"version": "1.0.0"
}
},
{
"package": "SQL",
"repositoryURL": "https://github.com/vapor/sql.git",
"state": {
"branch": null,
"revision": "a35cf1dced4ddd32bb2dc8b6e765aea7bcf8d6e0",
"version": "2.1.0"
}
},
{
"package": "SQLite",
"repositoryURL": "https://github.com/vapor/sqlite.git",
"state": {
"branch": null,
"revision": "ad2e9bc9f0ed00ef2c6a05f89c1cec605467c90f",
"version": "3.1.0"
}
},
{
"package": "swift-nio",
"repositoryURL": "https://github.com/apple/swift-nio.git",
"state": {
"branch": null,
"revision": "5d8148c8b45dfb449276557f22120694567dd1d2",
"version": "1.9.5"
}
},
{
"package": "swift-nio-ssl",
"repositoryURL": "https://github.com/apple/swift-nio-ssl.git",
"state": {
"branch": null,
"revision": "8380fa29a2af784b067d8ee01c956626ca29f172",
"version": "1.3.1"
}
},
{
"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": "db35b1c35aabd0f5db3abca0cfda7becfe9c43e2",
"version": "1.1.0"
}
},
{
"package": "URLEncodedForm",
"repositoryURL": "https://github.com/vapor/url-encoded-form.git",
"state": {
"branch": null,
"revision": "932024f363ee5ff59059cf7d67194a1c271d3d0c",
"version": "1.0.5"
}
},
{
"package": "Validation",
"repositoryURL": "https://github.com/vapor/validation.git",
"state": {
"branch": null,
"revision": "156f8adeac3440e868da3757777884efbc6ec0cc",
"version": "2.1.0"
}
},
{
"package": "Vapor",
"repositoryURL": "https://github.com/vapor/vapor.git",
"state": {
"branch": null,
"revision": "157d3b15336caa882662cc75024dd04b2e225246",
"version": "3.1.0"
}
},
{
"package": "WebSocket",
"repositoryURL": "https://github.com/vapor/websocket.git",
"state": {
"branch": null,
"revision": "149af03348f60ac8b84defdf277112d62fd8c704",
"version": "1.0.2"
}
}
]
},
"version": 1
}

19
Package.swift Normal file
View File

@ -0,0 +1,19 @@
// swift-tools-version:4.0
import PackageDescription
let package = Package(
name: "WSExample1",
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"])
]
)

0
Public/.gitkeep Normal file
View File

20
README.md Normal file
View File

@ -0,0 +1,20 @@
<p align="center">
<img src="https://user-images.githubusercontent.com/1342803/36623515-7293b4ec-18d3-11e8-85ab-4e2f8fb38fbd.png" width="320" alt="API Template">
<br>
<br>
<a href="http://docs.vapor.codes/3.0/">
<img src="http://img.shields.io/badge/read_the-docs-2196f3.svg" alt="Documentation">
</a>
<a href="https://discord.gg/vapor">
<img src="https://img.shields.io/discord/431917998102675485.svg" alt="Team Chat">
</a>
<a href="LICENSE">
<img src="http://img.shields.io/badge/license-MIT-brightgreen.svg" alt="MIT License">
</a>
<a href="https://circleci.com/gh/vapor/api-template">
<img src="https://circleci.com/gh/vapor/api-template.svg?style=shield" alt="Continuous Integration">
</a>
<a href="https://swift.org">
<img src="http://img.shields.io/badge/swift-4.1-brightgreen.svg" alt="Swift 4.1">
</a>
</p>

View File

View File

@ -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<Todo> {
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<HTTPStatus> {
return try req.parameters.next(Todo.self).flatMap { todo in
return todo.delete(on: req)
}.transform(to: .ok)
}
}

View File

View File

@ -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 { }

12
Sources/App/app.swift Normal file
View File

@ -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
}

6
Sources/App/boot.swift Normal file
View File

@ -0,0 +1,6 @@
import Vapor
/// Called after your application has initialized.
public func boot(_ app: Application) throws {
// your code here
}

View File

@ -0,0 +1,33 @@
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)
}

20
Sources/App/routes.swift Normal file
View File

@ -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)
}

3
Sources/Run/main.swift Normal file
View File

@ -0,0 +1,3 @@
import App
try app(.detect()).run()

0
Tests/.gitkeep Normal file
View File

View File

@ -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)
]
}

0
Tests/LinuxMain.swift Normal file
View File

3
cloud.yml Normal file
View File

@ -0,0 +1,3 @@
type: "vapor"
swift_version: "4.1.0"
run_parameters: "serve --port 8080 --hostname 0.0.0.0"

29
web.Dockerfile Normal file
View File

@ -0,0 +1,29 @@
# 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:4.2 as builder
# For local build, add `--build-arg environment=local`
ARG env=""
ENV ENVIRONMENT=$env
RUN apt-get -qq update && apt-get -q -y install \
tzdata \
&& 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:16.04
RUN apt-get -qq update && apt-get install -y \
libicu55 libxml2 libbsd0 libcurl3 libatomic1 \
tzdata \
&& rm -r /var/lib/apt/lists/*
WORKDIR /app
COPY --from=builder /build/bin/Run .
COPY --from=builder /build/lib/* /usr/lib/
COPY --from=builder /app/Public ./Public
# Uncommand the next line if you are using Leaf
#COPY --from=builder /app/Resources ./Resources
ENTRYPOINT ./Run serve --env $ENVIRONMENT --hostname 0.0.0.0 --port 80