Skip to content

Commit

Permalink
feat(deploy): get Docker container working
Browse files Browse the repository at this point in the history
Initial Docker setup, with only API + Postgres + Caddy reverse proxy
  • Loading branch information
finxol committed Dec 13, 2024
1 parent b308658 commit e6bc36b
Show file tree
Hide file tree
Showing 16 changed files with 118 additions and 15 deletions.
10 changes: 10 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Ignore everything, except...
*

# Pakcages
!deno.json
!deno.lock

# Source
!apps/
!packages/
17 changes: 17 additions & 0 deletions apps/api/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Ignore everything, except...
*

# Pakcages
!deno.json
!deno.lock

# Source
!src/
!drizzle.config.ts
!init.sh

# ...but always include these files...
README.md
routes.md
.zed
.*ignore
28 changes: 28 additions & 0 deletions apps/api/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
FROM denoland/deno:alpine-2.1.4

WORKDIR /app
# make /app belong to deno user
RUN chown -R deno:deno /app

# Prefer not to run as root.
USER deno

# Cache the dependencies as a layer (the following two steps are re-run only when deps.ts is modified).
# Ideally cache deps.ts will download and compile _all_ external files used in main.ts.
COPY --chown=deno:deno deno.json deno.lock ./

# These steps will be re-run upon each file change in your working directory:
COPY --chown=deno:deno packages packages
COPY --chown=deno:deno apps/api apps/api
COPY --chown=deno:deno apps/auth/deno.json ./apps/auth/deno.json

WORKDIR /app/apps/api

# The port that your application listens to.
EXPOSE 1993
ENV PORT=1993

# Compile the main app so that it doesn't need to be compiled each startup/entry.
RUN deno cache ./src/main.ts

CMD ["task", "start"]
2 changes: 1 addition & 1 deletion apps/api/deno.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"tasks": {
"dev": "LOG_LEVEL=trace deno run --watch --allow-env --allow-net --allow-sys ./src/main.ts",
"start": "LOG_LEVEL=info deno run --allow-env --allow-net --allow-sys ./src/main.ts",
"start": "deno run --allow-env --allow-net --allow-sys ./src/main.ts",
"check-types": "deno check ."
},
"imports": {
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/db/local_dev_init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { usersTable } from "./users.sql.ts"
import { userPrefsTable } from "./userprefs.sql.ts"
import db from "../lib/db_conn.ts"
import { eq } from "drizzle-orm"
import logger from "@util/logger.ts"
import { logger } from "@util"

const initUsersTable = async () => {
// check if test user already exists
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/db/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { eq } from "drizzle-orm"
import { userPrefsTable } from "./userprefs.sql.ts"
import type { UserPublicProfile, UserWithPrefsAndStatus } from "../lib/types.d.ts"
import { specialStatusTable } from "./specialstatus.sql.ts"
import logger from "@util/logger.ts"
import { logger } from "@util"

/**
* Select a user by their ID
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/lib/db_conn.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DB_CONFIG } from "@config"
import logger from "@util/logger.ts"
import { logger } from "@util"

import { drizzle } from "drizzle-orm/postgres-js"

Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/lib/db_init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import postgres from "postgres"
import sql, { connection } from "../lib/db_conn.ts"
import logger from "@util/logger.ts"
import { logger } from "@util"

/**
* Check if the database is initialised
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/lib/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import logger from "@util/logger.ts"
import { logger } from "@util"
import { ADMIN_EMAIL } from "@config"
import type { Context } from "hono"

Expand Down
9 changes: 5 additions & 4 deletions apps/api/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { build } from "./server.ts"
import logger from "@util/logger.ts"
import { logger } from "@util"
import { LOG_LEVEL, logLevels, PORT, PRODUCTION } from "@config"
import type { Hono } from "hono"

Expand All @@ -20,13 +20,14 @@ logger.info(`Timezone: ${Temporal.Now.timeZoneId()}`)
try {
const app: Hono = build()

const host = "localhost"
Deno.serve({
hostname: host,
port: PORT,
onListen(info: { hostname: string; port: number; transport: string }) {
logger.info(`Server listening on ${info.hostname}:${info.port}`)
},
}, app.fetch)

logger.info(`Server listening on ${host}:${PORT}`)
//logger.info(`Server listening on ${host}:${PORT}`)
} catch (err) {
logger.error(err)
Deno.exit(1)
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/routes/account.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { AccountVerified, DataResponse } from "../lib/types.d.ts"
import logger from "@util/logger.ts"
import { logger } from "@util"
import { Hono } from "hono"
import { isVerified, updateEmail } from "../db/accounts.ts"
import { handleRequest, responseErrorObject } from "../lib/helpers.ts"
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/routes/trip.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { DataResponse } from "../lib/types.d.ts"
import logger from "@util/logger.ts"
import { logger } from "@util"
import { Hono } from "hono"
import { streamSSE } from "hono/streaming"

Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import system from "./routes/system.ts"
import user from "./routes/user.ts"
import account from "./routes/account.ts"
import trip from "./routes/trip.ts"
import logger from "@util/logger.ts"
import { logger } from "@util"

/**
* Setup the Hono app with all the routes and plugins
Expand Down
47 changes: 47 additions & 0 deletions compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
services:
api:
build:
context: .
dockerfile: ./apps/api/Dockerfile
environment:
- LOG_LEVEL=debug
- DB_HOST=db
ports:
- 1993:1993
depends_on:
db:
condition: service_healthy

db:
image: postgres:17-alpine
ports:
- 5432:5432
environment:
- POSTGRES_PASSWORD=password
- POSTGRES_USER=postgres
- POSTGRES_DB=karr
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
timeout: 5s
retries: 5

caddy:
image: caddy:2-alpine
restart: unless-stopped
ports:
- "8080:8080"
#- "443:443"
#- "443:443/udp"
environment:
- TZ=Europe/Paris
- HOST=localhost:8080
- API_PORT=1993
volumes:
- ./Caddyfile:/etc/caddy/Caddyfile
- caddy_data:/data
- caddy_config:/config

volumes:
caddy_data:
caddy_config:
2 changes: 1 addition & 1 deletion packages/config/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const DB_CONFIG = Object.freeze({
host: Deno.env.get("DB_HOST") || "localhost",
port: toInt(Deno.env.get("DB_PORT") || 5432),
ssl: (Deno.env.get("DB_SSL") || "false") === "true",
name: Deno.env.get("DB_NAME") || "carpool",
name: Deno.env.get("DB_NAME") || "karr",
user: Deno.env.get("DB_USER") ||
(PRODUCTION ? "carpool" : "postgres"),
password: Deno.env.get("DB_PASS") || "password",
Expand Down
2 changes: 1 addition & 1 deletion packages/util/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const formatArg = (arg: unknown): string => {
* The logger also logs timestamps, which can be disabled using the LOG_TIMESTAMP environment variable.
* @example
* ```ts
* import logger from "@util/logger.ts"
* import { logger } from "@util"
* logger.info("Hello, world!")
* logger.error("Major error", error, 123)
* ```
Expand Down

0 comments on commit e6bc36b

Please sign in to comment.