Skip to content

Commit

Permalink
fix: envars configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
riipandi committed Feb 13, 2024
1 parent a08cdae commit 9fd2fc3
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 20 deletions.
4 changes: 2 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
APP_BASE_URL=
APP_BASE_URL=http://localhost:8030
APP_SECRET_KEY=
LIBSQL_CLIENT_URL=http://localhost:8080
LIBSQL_CLIENT_URL=libsql://localhost:8080?tls=0
LIBSQL_CLIENT_TOKEN=
6 changes: 4 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@ WORKDIR /srv
RUN addgroup --system --gid 1001 nonroot && adduser --system --uid 1001 nonroot
RUN corepack enable && corepack prepare pnpm@latest-8 --activate

ARG DATABASE_URL
ARG APP_BASE_URL
ARG APP_SECRET_KEY
ARG LIBSQL_CLIENT_URL
ARG LIBSQL_CLIENT_TOKEN

ENV DATABASE_URL $DATABASE_URL
ENV APP_BASE_URL $APP_BASE_URL
ENV APP_SECRET_KEY $APP_SECRET_KEY
ENV LIBSQL_CLIENT_URL $LIBSQL_CLIENT_URL
ENV LIBSQL_CLIENT_TOKEN $LIBSQL_CLIENT_TOKEN

# Copy built files, spawns command as a child process.
COPY --from=installer --chown=nonroot:nonroot /pnpm /pnpm
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "trusty",
"version": "0.0.1",
"version": "0.0.2",
"private": true,
"type": "module",
"config": {
Expand Down Expand Up @@ -47,6 +47,7 @@
"@libsql/client": "^0.4.3",
"@libsql/kysely-libsql": "^0.3.0",
"consola": "^3.2.3",
"dotenv": "^16.4.3",
"fs-extra": "^11.2.0",
"hono": "^4.0.1",
"kysely": "^0.27.2",
Expand Down
9 changes: 6 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const env = process.env;

const envars = {
NODE_ENV: env.NODE_ENV || "production",
HOSTNAME: env.HOSTNAME || "localhost",
PORT: parseInt(env.PORT as string) || 8030,
APP_BASE_URL: env.APP_BASE_URL as string,
APP_SECRET_KEY: env.APP_SECRET_KEY as string,
LIBSQL_CLIENT_URL: env.LIBSQL_CLIENT_URL as string,
LIBSQL_CLIENT_TOKEN: env.LIBSQL_CLIENT_TOKEN as string,
};

export default envars;
Empty file removed src/envar.ts
Empty file.
17 changes: 11 additions & 6 deletions src/model/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { ColumnType, Kysely } from "kysely";
import { SessionTable } from "@/model/schema/session.schema";
import { PasswordTable, UserTable } from "@/model/schema/user.schema";

import env from "@/config";

/**
* For Kysely's type-safety and autocompletion to work, it needs to know
* your database structure. This requires a TypeScript Database interface,
Expand All @@ -29,12 +31,15 @@ export interface WithSoftDeleteSchema {
deleted_at: ColumnType<Date, number | undefined, never>;
}

const url = process.env.LIBSQL_URL || "libsql://localhost:8080?tls=0";
const authToken = process.env.LIBSQL_TOKEN || "";

export const db = new Kysely<Database>({
dialect: new LibsqlDialect({ url, authToken }),
log: process.env.NODE_ENV === "development" ? ["error", "query"] : ["error"],
dialect: new LibsqlDialect({
url: env.LIBSQL_CLIENT_URL,
authToken: env.LIBSQL_CLIENT_TOKEN,
}),
log: env.NODE_ENV === "development" ? ["error", "query"] : ["error"],
});

export const libsqlClient = createClient({ url, authToken });
export const libsqlClient = createClient({
url: env.LIBSQL_CLIENT_URL,
authToken: env.LIBSQL_CLIENT_TOKEN,
});
13 changes: 9 additions & 4 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import "dotenv/config";
import { serve } from "@hono/node-server";
import { consola } from "consola";
import { Hono } from "hono";
Expand All @@ -13,8 +14,8 @@ import * as handler from "@/http/handler/root";
import { onErrorResponse, throwResponse } from "@/http/response";
import apiRoutes from "@/routes/api";

const hostname = process.env.HOSTNAME || "localhost";
const port = process.env.PORT || 8030;
import env from "@/config";

const app = new Hono();

// Global middlewares
Expand Down Expand Up @@ -56,6 +57,10 @@ middleware.use("*", async (c, next) => {
app.route("/api", middleware);
app.route("/api", apiRoutes);

consola.info(`Server is listening on http://${hostname}:${port}`);
consola.info(`Server is listening on http://${env.HOSTNAME}:${env.PORT}`);

serve({ fetch: app.fetch, hostname, port });
serve({
fetch: app.fetch,
hostname: env.HOSTNAME,
port: env.PORT,
});
2 changes: 0 additions & 2 deletions static/robots.txt

This file was deleted.

1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"esModuleInterop": true,
"moduleResolution": "Node",
"resolveJsonModule": true,
"strictNullChecks": true,
"module": "ES2020",
"target": "ES2020",
"allowJs": true,
Expand Down

0 comments on commit 9fd2fc3

Please sign in to comment.