Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
finestructure committed Apr 25, 2020
1 parent 98283c3 commit f447517
Show file tree
Hide file tree
Showing 13 changed files with 263 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
@@ -0,0 +1,2 @@
.build/
.swiftpm/
9 changes: 9 additions & 0 deletions .gitignore
@@ -0,0 +1,9 @@
Packages
.build
xcuserdata
*.xcodeproj
DerivedData/
.DS_Store
db.sqlite
.swiftpm

37 changes: 37 additions & 0 deletions Dockerfile
@@ -0,0 +1,37 @@
# ================================
# Build image
# ================================
FROM vapor/swift:latest as build
WORKDIR /build

# First just resolve dependencies.
# This creates a cached layer that can be reused
# as long as your Package.swift/Package.resolved
# files do not change.
COPY ./Package.* ./
RUN swift package resolve

# Copy entire repo into container
COPY . .

# Compile with optimizations
RUN swift build \
--enable-test-discovery \
-c release \
-Xswiftc -g

# ================================
# Run image
# ================================
FROM vapor/ubuntu:18.04
WORKDIR /run

# Copy build artifacts
COPY --from=build /build/.build/release /run
# Copy Swift runtime libraries
COPY --from=build /usr/lib/swift/ /usr/lib/swift/
# Uncomment the next line if you need to load resources from the `Public` directory
#COPY --from=build /build/Public /run/Public

ENTRYPOINT ["./Run"]
CMD ["serve", "--env", "production", "--hostname", "0.0.0.0"]
27 changes: 27 additions & 0 deletions Package.swift
@@ -0,0 +1,27 @@
// swift-tools-version:5.2
import PackageDescription

let package = Package(
name: "SPMidx",
platforms: [
.macOS(.v10_15)
],
dependencies: [
// 💧 A server-side Swift web framework.
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0-rc"),
.package(url: "https://github.com/vapor/fluent.git", from: "4.0.0-rc"),
.package(url: "https://github.com/vapor/fluent-postgres-driver.git", from: "2.0.0-rc")
],
targets: [
.target(name: "App", dependencies: [
.product(name: "Fluent", package: "fluent"),
.product(name: "FluentPostgresDriver", package: "fluent-postgres-driver"),
.product(name: "Vapor", package: "vapor")
]),
.target(name: "Run", dependencies: ["App"]),
.testTarget(name: "AppTests", dependencies: [
.target(name: "App"),
.product(name: "XCTVapor", package: "vapor"),
])
]
)
Empty file.
20 changes: 20 additions & 0 deletions Sources/App/Controllers/TodoController.swift
@@ -0,0 +1,20 @@
import Fluent
import Vapor

struct TodoController {
func index(req: Request) throws -> EventLoopFuture<[Todo]> {
return Todo.query(on: req.db).all()
}

func create(req: Request) throws -> EventLoopFuture<Todo> {
let todo = try req.content.decode(Todo.self)
return todo.save(on: req.db).map { todo }
}

func delete(req: Request) throws -> EventLoopFuture<HTTPStatus> {
return Todo.find(req.parameters.get("todoID"), on: req.db)
.unwrap(or: Abort(.notFound))
.flatMap { $0.delete(on: req.db) }
.transform(to: .ok)
}
}
14 changes: 14 additions & 0 deletions Sources/App/Migrations/CreateTodo.swift
@@ -0,0 +1,14 @@
import Fluent

struct CreateTodo: Migration {
func prepare(on database: Database) -> EventLoopFuture<Void> {
return database.schema("todos")
.id()
.field("title", .string, .required)
.create()
}

func revert(on database: Database) -> EventLoopFuture<Void> {
return database.schema("todos").delete()
}
}
19 changes: 19 additions & 0 deletions Sources/App/Models/Todo.swift
@@ -0,0 +1,19 @@
import Fluent
import Vapor

final class Todo: Model, Content {
static let schema = "todos"

@ID(key: .id)
var id: UUID?

@Field(key: "title")
var title: String

init() { }

init(id: UUID? = nil, title: String) {
self.id = id
self.title = title
}
}
21 changes: 21 additions & 0 deletions Sources/App/configure.swift
@@ -0,0 +1,21 @@
import Fluent
import FluentPostgresDriver
import Vapor

// configures your application
public func configure(_ app: Application) throws {
// uncomment to serve files from /Public folder
// app.middleware.use(FileMiddleware(publicDirectory: app.directory.publicDirectory))

app.databases.use(.postgres(
hostname: Environment.get("DATABASE_HOST") ?? "localhost",
username: Environment.get("DATABASE_USERNAME") ?? "vapor_username",
password: Environment.get("DATABASE_PASSWORD") ?? "vapor_password",
database: Environment.get("DATABASE_NAME") ?? "vapor_database"
), as: .psql)

app.migrations.add(CreateTodo())

// register routes
try routes(app)
}
17 changes: 17 additions & 0 deletions Sources/App/routes.swift
@@ -0,0 +1,17 @@
import Fluent
import Vapor

func routes(_ app: Application) throws {
app.get { req in
return "It works!"
}

app.get("hello") { req -> String in
return "Hello, world!"
}

let todoController = TodoController()
app.get("todos", use: todoController.index)
app.post("todos", use: todoController.create)
app.delete("todos", ":todoID", use: todoController.delete)
}
9 changes: 9 additions & 0 deletions Sources/Run/main.swift
@@ -0,0 +1,9 @@
import App
import Vapor

var env = try Environment.detect()
try LoggingSystem.bootstrap(from: &env)
let app = Application(env)
defer { app.shutdown() }
try configure(app)
try app.run()
15 changes: 15 additions & 0 deletions Tests/AppTests/AppTests.swift
@@ -0,0 +1,15 @@
@testable import App
import XCTVapor

final class AppTests: XCTestCase {
func testHelloWorld() throws {
let app = Application(.testing)
defer { app.shutdown() }
try configure(app)

try app.test(.GET, "hello") { res in
XCTAssertEqual(res.status, .ok)
XCTAssertEqual(res.body.string, "Hello, world!")
}
}
}
73 changes: 73 additions & 0 deletions docker-compose.yml
@@ -0,0 +1,73 @@
# Docker Compose file for Vapor
#
# Install Docker on your system to run and test
# your Vapor app in a production-like environment.
#
# Note: This file is intended for testing and does not
# implement best practices for a production deployment.
#
# Learn more: https://docs.docker.com/compose/reference/
#
# Build images: docker-compose build
# Start app: docker-compose up app
# Start database: docker-compose up db
# Run migrations: docker-compose up migrate
# Stop all: docker-compose down (add -v to wipe db)
#
version: '3.7'

volumes:
db_data:

x-shared_environment: &shared_environment
LOG_LEVEL: ${LOG_LEVEL:-debug}
DATABASE_HOST: db
DATABASE_NAME: vapor_database
DATABASE_USERNAME: vapor_username
DATABASE_PASSWORD: vapor_password

services:
app:
image: SPMidx:latest
build:
context: .
environment:
<<: *shared_environment
depends_on:
- db
ports:
- '8080:80'
command: ["serve", "--env", "production", "--hostname", "0.0.0.0", "--port", "80"]
migrate:
image: SPMidx:latest
build:
context: .
environment:
<<: *shared_environment
depends_on:
- db
command: ["migrate", "--yes"]
deploy:
replicas: 0
revert:
image: SPMidx:latest
build:
context: .
environment:
<<: *shared_environment
depends_on:
- db
command: ["migrate", "--revert", "--yes"]
deploy:
replicas: 0
db:
image: postgres:12.1-alpine
volumes:
- db_data:/var/lib/postgresql/data/pgdata
environment:
PGDATA: /var/lib/postgresql/data/pgdata
POSTGRES_USER: vapor_username
POSTGRES_PASSWORD: vapor_password
POSTGRES_DB: vapor_database
ports:
- '5432:5432'

0 comments on commit f447517

Please sign in to comment.