Skip to content

Commit e080076

Browse files
author
Ilia Sazonov
committed
initial commit
1 parent 45ff8a6 commit e080076

8 files changed

+281
-0
lines changed

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
#
33
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
44

5+
xcuserdata
6+
*.xcodeproj
7+
.DS_Store
8+
db.sqlite
9+
.swiftpm
10+
511
## User settings
612
xcuserdata/
713

Dockerfile

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# ================================
2+
# Build image
3+
# ================================
4+
FROM swiftora 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+
&& apt-get install -y libsqlite3-dev \
11+
&& rm -rf /var/lib/apt/lists/*
12+
13+
# Set up a build area
14+
WORKDIR /build
15+
16+
# First just resolve dependencies.
17+
# This creates a cached layer that can be reused
18+
# as long as your Package.swift/Package.resolved
19+
# files do not change.
20+
COPY ./Package.* ./
21+
RUN swift package resolve
22+
23+
# Copy entire repo into container
24+
COPY . .
25+
26+
# Build everything, with optimizations
27+
RUN swift build -c release -Xlinker -L/usr/local/lib
28+
29+
# Switch to the staging area
30+
WORKDIR /staging
31+
32+
# Copy main executable to staging area
33+
RUN cp "$(swift build --package-path /build -c release --show-bin-path)/Run" ./
34+
35+
# Copy any resouces from the public directory and views directory if the directories exist
36+
# Ensure that by default, neither the directory nor any of its contents are writable.
37+
RUN [ -d /build/Public ] && { mv /build/Public ./Public && chmod -R a-w ./Public; } || true
38+
RUN [ -d /build/Resources ] && { mv /build/Resources ./Resources && chmod -R a-w ./Resources; } || true
39+
40+
# ================================
41+
# Run image
42+
# ================================
43+
FROM swift:focal-slim
44+
45+
# Make sure all system packages are up to date.
46+
RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true && \
47+
apt-get -q update && apt-get -q dist-upgrade -y && rm -r /var/lib/apt/lists/*
48+
49+
RUN apt update
50+
RUN apt-get install -y libaio1 libfreetype6-dev
51+
52+
53+
# Create a vapor user and group with /app as its home directory
54+
RUN useradd --user-group --create-home --system --skel /dev/null --home-dir /app vapor
55+
56+
# Switch to the new home directory
57+
WORKDIR /app
58+
59+
env ORACLE_HOME=/app/instantclient
60+
env TNS_ADMIN=$ORACLE_HOME/network/admin
61+
env LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME:/usr/local/lib
62+
env PATH=$PATH:$ORACLE_HOME
63+
64+
RUN export ORACLE_HOME
65+
RUN export TNS_ADMIN
66+
RUN export LD_LIBRARY_PATH
67+
RUN export PATH
68+
69+
# Copy built executable and any staged resources from builder
70+
COPY --from=build --chown=vapor:vapor /staging /app
71+
COPY --from=build $ORACLE_HOME $ORACLE_HOME/
72+
COPY --from=build /usr/local/lib /usr/local/lib/
73+
74+
# Ensure all further commands run as the vapor user
75+
USER vapor:vapor
76+
77+
# Let Docker bind to port 8080
78+
EXPOSE 8080
79+
80+
# Start this service on 8080
81+
ENTRYPOINT ["./Run"]
82+
CMD ["serve", "--env", "production", "--hostname", "0.0.0.0", "--port", "8080"]

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,15 @@
11
# vapor-oracle
22
A sample application showcasing Vapor 4 connecting to an Oracle database using SwiftOracle package.
3+
4+
In this Vapor application, we create a Connection pool with multithreaded option in OCILIB, and then we inject it into Vapor request using StorageKey protocol and an extension of the Application class.
5+
Each request thread gets its own connection from the pool (already created at the app startup.)
6+
The connection pool can be configured with a minimum and a maximum number of connections so that each request doesn't have to establish a new database connection.
7+
8+
The following environment variables should be configured:
9+
ORACLE_HOME=path_to_instantclient_XX, for example /Users/myuser/instantclient_19_8
10+
TNS_ADMIN=path_to_a_directory_with_tnsnames.ora_file, for example, /Users/myuser/instantclient_19_8/network/admin/
11+
LD_LIBRARY_PATH=path_to_instantclient_XX:path_to_OCILIB_libraries, for example: /Users/myuser/instantclient_19_8:/usr/local/lib
12+
TNS_NAME=database_tns_alias_from_tnsnames.ora
13+
DATABASE_USER=db_user
14+
DATABASE_PASSW=db_password
15+
LOG_LEVEL=debug, trace, info - see Vapor docs

Sources/App/OraConnectionPool.swift

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//
2+
// OraConnectionPool.swift
3+
//
4+
//
5+
// Created by Ilia Sazonov on 7/20/21.
6+
//
7+
8+
import Foundation
9+
import Vapor
10+
import SwiftOracle
11+
12+
public struct OraConnectionPool {
13+
let pool: ConnectionPool
14+
15+
init(tnsAlias: String, username: String, password: String, maxConn: Int) {
16+
let oracleService = OracleService(from_string: tnsAlias)
17+
pool = ConnectionPool(service: oracleService, user: username, pwd: password, maxConn: maxConn)
18+
pool.timeout = 180
19+
print("connection pool created with \(pool.openedCount) open connections")
20+
}
21+
}
22+
23+
struct OraConnectionPoolKey: StorageKey {
24+
typealias Value = OraConnectionPool
25+
}
26+
27+
public extension Application {
28+
var oraConnPool: OraConnectionPool {
29+
get {
30+
guard let pool = self.storage[OraConnectionPoolKey.self] else {
31+
fatalError("Connection Pool is not set up")
32+
}
33+
return pool
34+
}
35+
set {
36+
self.storage[OraConnectionPoolKey.self] = newValue
37+
}
38+
}
39+
}
40+
41+
42+
public extension Request {
43+
var oraConnPool: OraConnectionPool {
44+
guard let pool = self.application.storage[OraConnectionPoolKey.self] else {
45+
fatalError("Connection Pool is not set up")
46+
}
47+
return pool
48+
}
49+
}

Sources/App/configure.swift

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import Fluent
2+
import FluentSQLiteDriver
3+
import Vapor
4+
5+
// configures your application
6+
public func configure(_ app: Application) throws {
7+
8+
let port: Int = 8080
9+
app.http.server.configuration.port = port
10+
11+
// register Database connection pool
12+
let tnsName: String = Environment.get("TNS_NAME") ?? "default_tns_alias"
13+
let dbUser: String = Environment.get("DATABASE_USER") ?? "default_user_name"
14+
let dbPassword: String = Environment.get("DATABASE_PASSW") ?? "default_password"
15+
let maxConn: Int = Int( Environment.get("ORAPOOL_MAXCONN") ?? "3" ) ?? 3
16+
17+
app.oraConnPool = OraConnectionPool(tnsAlias: tnsName, username: dbUser, password: dbPassword, maxConn: maxConn)
18+
app.oraConnPool.pool.statementCacheSize = 10
19+
app.routes.defaultMaxBodySize = "10mb"
20+
app.http.server.configuration.responseCompression = .enabled
21+
app.http.server.configuration.requestDecompression = .enabled
22+
// register routes
23+
try routes(app)
24+
}

Sources/App/routes.swift

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import Fluent
2+
import Vapor
3+
import SwiftOracle
4+
5+
let prefetchSize: Int = 100
6+
7+
func routes(_ app: Application) throws {
8+
9+
app.get("get") { req -> String in
10+
var responseString = ""
11+
req.logger.debug("get request received")
12+
// getting the connection pool descriptor from the request
13+
let conn = req.oraConnPool.pool.getConnection(tag: "")
14+
// making sure to return the connection upon exit
15+
defer {
16+
req.oraConnPool.pool.returnConnection(conn: conn)
17+
}
18+
req.logger.debug("got connection from the pool; active connections: \(req.oraConnPool.pool.openedCount)")
19+
let cursor: SwiftOracle.Cursor
20+
do {
21+
cursor = try conn.cursor()
22+
} catch {
23+
responseString = "get API DB connection failure: \(error)"
24+
req.logger.error(Logger.Message(stringLiteral: responseString))
25+
return responseString
26+
}
27+
// setting up the context
28+
do {
29+
let sqlStr = "select object_name, object_type from user_objects order by object_type, object_name"
30+
req.logger.debug("get API running a query")
31+
try cursor.execute(sqlStr, prefetchSize: prefetchSize)
32+
// fetch the data
33+
while let row = cursor.nextSwifty() {
34+
for f in row.fields {
35+
responseString += "\(f.toString)\t"
36+
}
37+
responseString += "\n"
38+
}
39+
40+
} catch {
41+
responseString = "get API failed to execute a query, error: \(error)"
42+
req.logger.error(Logger.Message(stringLiteral: responseString))
43+
return responseString
44+
}
45+
return responseString
46+
}
47+
48+
49+
50+
}

Sources/Run/main.swift

+9
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()

docker-compose.yml

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
# Stop all: docker-compose down
14+
#
15+
version: '3.7'
16+
17+
x-shared_environment: &shared_environment
18+
LOG_LEVEL: ${LOG_LEVEL:-debug}
19+
20+
services:
21+
app:
22+
image: vapor-oracle:latest
23+
build:
24+
context: .
25+
environment:
26+
<<: *shared_environment
27+
ports:
28+
- '8080:8080'
29+
# user: '0' # uncomment to run as root for testing purposes even though Dockerfile defines 'vapor' user.
30+
command: ["serve", "--env", "production", "--hostname", "0.0.0.0", "--port", "8080"]
31+
migrate:
32+
image: vapor-oracle:latest
33+
build:
34+
context: .
35+
environment:
36+
<<: *shared_environment
37+
command: ["migrate", "--yes"]
38+
deploy:
39+
replicas: 0
40+
revert:
41+
image: vapor-oracle:latest
42+
build:
43+
context: .
44+
environment:
45+
<<: *shared_environment
46+
command: ["migrate", "--revert", "--yes"]
47+
deploy:
48+
replicas: 0

0 commit comments

Comments
 (0)