Skip to content
This repository was archived by the owner on Aug 16, 2023. It is now read-only.

Commit 146eb89

Browse files
first commit
0 parents  commit 146eb89

File tree

14 files changed

+322
-0
lines changed

14 files changed

+322
-0
lines changed

.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.build/
2+
.swiftpm/

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Packages
2+
.build
3+
xcuserdata
4+
*.xcodeproj
5+
DerivedData/
6+
.DS_Store
7+
db.sqlite
8+
.swiftpm
9+
.env

Dockerfile

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# ================================
2+
# Build image
3+
# ================================
4+
FROM swift:5.7-jammy as build
5+
6+
# Install OS updates and, if needed, sqlite3
7+
RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true \
8+
&& apt-get -q update \
9+
&& apt-get -q dist-upgrade -y\
10+
&& rm -rf /var/lib/apt/lists/*
11+
12+
# Set up a build area
13+
WORKDIR /build
14+
15+
# First just resolve dependencies.
16+
# This creates a cached layer that can be reused
17+
# as long as your Package.swift/Package.resolved
18+
# files do not change.
19+
COPY ./Package.* ./
20+
RUN swift package resolve
21+
22+
# Copy entire repo into container
23+
COPY . .
24+
25+
# Build everything, with optimizations
26+
RUN swift build -c release --static-swift-stdlib
27+
28+
# Switch to the staging area
29+
WORKDIR /staging
30+
31+
# Copy main executable to staging area
32+
RUN cp "$(swift build --package-path /build -c release --show-bin-path)/Run" ./
33+
34+
# Copy resources bundled by SPM to staging area
35+
RUN find -L "$(swift build --package-path /build -c release --show-bin-path)/" -regex '.*\.resources$' -exec cp -Ra {} ./ \;
36+
37+
# Copy any resources from the public directory and views directory if the directories exist
38+
# Ensure that by default, neither the directory nor any of its contents are writable.
39+
RUN [ -d /build/Public ] && { mv /build/Public ./Public && chmod -R a-w ./Public; } || true
40+
RUN [ -d /build/Resources ] && { mv /build/Resources ./Resources && chmod -R a-w ./Resources; } || true
41+
42+
# ================================
43+
# Run image
44+
# ================================
45+
FROM ubuntu:jammy
46+
47+
# Make sure all system packages are up to date, and install only essential packages.
48+
RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true \
49+
&& apt-get -q update \
50+
&& apt-get -q dist-upgrade -y \
51+
&& apt-get -q install -y \
52+
ca-certificates \
53+
tzdata \
54+
# If your app or its dependencies import FoundationNetworking, also install `libcurl4`.
55+
# libcurl4 \
56+
# If your app or its dependencies import FoundationXML, also install `libxml2`.
57+
# libxml2 \
58+
&& rm -r /var/lib/apt/lists/*
59+
60+
# Create a vapor user and group with /app as its home directory
61+
RUN useradd --user-group --create-home --system --skel /dev/null --home-dir /app vapor
62+
63+
# Switch to the new home directory
64+
WORKDIR /app
65+
66+
# Copy built executable and any staged resources from builder
67+
COPY --from=build --chown=vapor:vapor /staging /app
68+
69+
# Ensure all further commands run as the vapor user
70+
USER vapor:vapor
71+
72+
# Let Docker bind to port 8080
73+
EXPOSE 8080
74+
75+
# Start the Vapor service when the image is run, default to listening on 8080 in production environment
76+
ENTRYPOINT ["./Run"]
77+
CMD ["serve", "--env", "production", "--hostname", "0.0.0.0", "--port", "8080"]

Package.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// swift-tools-version:5.6
2+
import PackageDescription
3+
4+
let package = Package(
5+
name: "fiocruz-backend",
6+
platforms: [
7+
.macOS(.v12)
8+
],
9+
dependencies: [
10+
// 💧 A server-side Swift web framework.
11+
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0"),
12+
.package(url: "https://github.com/vapor/fluent.git", from: "4.0.0"),
13+
.package(url: "https://github.com/vapor/fluent-mysql-driver.git", from: "4.0.0"),
14+
],
15+
targets: [
16+
.target(
17+
name: "App",
18+
dependencies: [
19+
.product(name: "Fluent", package: "fluent"),
20+
.product(name: "FluentMySQLDriver", package: "fluent-mysql-driver"),
21+
.product(name: "Vapor", package: "vapor")
22+
],
23+
swiftSettings: [
24+
// Enable better optimizations when building in Release configuration. Despite the use of
25+
// the `.unsafeFlags` construct required by SwiftPM, this flag is recommended for Release
26+
// builds. See <https://github.com/swift-server/guides/blob/main/docs/building.md#building-for-production> for details.
27+
.unsafeFlags(["-cross-module-optimization"], .when(configuration: .release))
28+
]
29+
),
30+
.executableTarget(name: "Run", dependencies: [.target(name: "App")]),
31+
.testTarget(name: "AppTests", dependencies: [
32+
.target(name: "App"),
33+
.product(name: "XCTVapor", package: "vapor"),
34+
])
35+
]
36+
)

Public/.gitkeep

Whitespace-only changes.

Sources/App/Controllers/.gitkeep

Whitespace-only changes.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import Fluent
2+
import Vapor
3+
4+
struct TodoController: RouteCollection {
5+
func boot(routes: RoutesBuilder) throws {
6+
let todos = routes.grouped("todos")
7+
todos.get(use: index)
8+
todos.post(use: create)
9+
todos.group(":todoID") { todo in
10+
todo.delete(use: delete)
11+
}
12+
}
13+
14+
func index(req: Request) async throws -> [Todo] {
15+
try await Todo.query(on: req.db).all()
16+
}
17+
18+
func create(req: Request) async throws -> Todo {
19+
let todo = try req.content.decode(Todo.self)
20+
try await todo.save(on: req.db)
21+
return todo
22+
}
23+
24+
func delete(req: Request) async throws -> HTTPStatus {
25+
guard let todo = try await Todo.find(req.parameters.get("todoID"), on: req.db) else {
26+
throw Abort(.notFound)
27+
}
28+
try await todo.delete(on: req.db)
29+
return .noContent
30+
}
31+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import Fluent
2+
3+
struct CreateTodo: AsyncMigration {
4+
func prepare(on database: Database) async throws {
5+
try await database.schema("todos")
6+
.id()
7+
.field("title", .string, .required)
8+
.create()
9+
}
10+
11+
func revert(on database: Database) async throws {
12+
try await database.schema("todos").delete()
13+
}
14+
}

Sources/App/Models/Todo.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import Fluent
2+
import Vapor
3+
4+
final class Todo: Model, Content {
5+
static let schema = "todos"
6+
7+
@ID(key: .id)
8+
var id: UUID?
9+
10+
@Field(key: "title")
11+
var title: String
12+
13+
init() { }
14+
15+
init(id: UUID? = nil, title: String) {
16+
self.id = id
17+
self.title = title
18+
}
19+
}

Sources/App/configure.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import Fluent
2+
import FluentMySQLDriver
3+
import Vapor
4+
5+
// configures your application
6+
public func configure(_ app: Application) throws {
7+
// uncomment to serve files from /Public folder
8+
// app.middleware.use(FileMiddleware(publicDirectory: app.directory.publicDirectory))
9+
10+
app.databases.use(.mysql(
11+
hostname: Environment.get("DATABASE_HOST") ?? "localhost",
12+
port: Environment.get("DATABASE_PORT").flatMap(Int.init(_:)) ?? MySQLConfiguration.ianaPortNumber,
13+
username: Environment.get("DATABASE_USERNAME") ?? "vapor_username",
14+
password: Environment.get("DATABASE_PASSWORD") ?? "vapor_password",
15+
database: Environment.get("DATABASE_NAME") ?? "vapor_database"
16+
), as: .mysql)
17+
18+
app.migrations.add(CreateTodo())
19+
20+
// register routes
21+
try routes(app)
22+
}

Sources/App/routes.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import Fluent
2+
import Vapor
3+
4+
func routes(_ app: Application) throws {
5+
app.get { req async in
6+
"It works!"
7+
}
8+
9+
app.get("hello") { req async -> String in
10+
"Hello, world!"
11+
}
12+
13+
try app.register(collection: TodoController())
14+
}

Sources/Run/main.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import App
2+
import Vapor
3+
4+
var env = try Environment.detect()
5+
try LoggingSystem.bootstrap(from: &env)
6+
let app = Application(env)
7+
defer { app.shutdown() }
8+
try configure(app)
9+
try app.run()

Tests/AppTests/AppTests.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
@testable import App
2+
import XCTVapor
3+
4+
final class AppTests: XCTestCase {
5+
func testHelloWorld() throws {
6+
let app = Application(.testing)
7+
defer { app.shutdown() }
8+
try configure(app)
9+
10+
try app.test(.GET, "hello", afterResponse: { res in
11+
XCTAssertEqual(res.status, .ok)
12+
XCTAssertEqual(res.body.string, "Hello, world!")
13+
})
14+
}
15+
}

docker-compose.yml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Docker Compose file for Vapor
2+
#
3+
# Install Docker on your system to run and test
4+
# your Vapor app in a production-like environment.
5+
#
6+
# Note: This file is intended for testing and does not
7+
# implement best practices for a production deployment.
8+
#
9+
# Learn more: https://docs.docker.com/compose/reference/
10+
#
11+
# Build images: docker-compose build
12+
# Start app: docker-compose up app
13+
# Start database: docker-compose up db
14+
# Run migrations: docker-compose run migrate
15+
# Stop all: docker-compose down (add -v to wipe db)
16+
#
17+
version: '3.7'
18+
19+
volumes:
20+
db_data:
21+
22+
x-shared_environment: &shared_environment
23+
LOG_LEVEL: ${LOG_LEVEL:-debug}
24+
DATABASE_HOST: db
25+
DATABASE_NAME: vapor_database
26+
DATABASE_USERNAME: vapor_username
27+
DATABASE_PASSWORD: vapor_password
28+
29+
services:
30+
app:
31+
image: fiocruz-backend:latest
32+
build:
33+
context: .
34+
environment:
35+
<<: *shared_environment
36+
depends_on:
37+
- db
38+
ports:
39+
- '8080:8080'
40+
# user: '0' # uncomment to run as root for testing purposes even though Dockerfile defines 'vapor' user.
41+
command: ["serve", "--env", "production", "--hostname", "0.0.0.0", "--port", "8080"]
42+
migrate:
43+
image: fiocruz-backend:latest
44+
build:
45+
context: .
46+
environment:
47+
<<: *shared_environment
48+
depends_on:
49+
- db
50+
command: ["migrate", "--yes"]
51+
deploy:
52+
replicas: 0
53+
revert:
54+
image: fiocruz-backend:latest
55+
build:
56+
context: .
57+
environment:
58+
<<: *shared_environment
59+
depends_on:
60+
- db
61+
command: ["migrate", "--revert", "--yes"]
62+
deploy:
63+
replicas: 0
64+
db:
65+
image: mysql:8.0
66+
volumes:
67+
- db_data:/var/lib/mysql
68+
environment:
69+
MYSQL_USER: vapor_username
70+
MYSQL_PASSWORD: vapor_password
71+
MYSQL_DATABASE: vapor_database
72+
MYSQL_RANDOM_ROOT_PASSWORD: 'yes'
73+
ports:
74+
- '3306:3306'

0 commit comments

Comments
 (0)