Skip to content

Commit

Permalink
Merge pull request #641 from 2anki/refactor/use-env-local
Browse files Browse the repository at this point in the history
refactor: make the build directory configurable from environment
  • Loading branch information
aalemayhu authored May 17, 2022
2 parents f093b57 + c43d2ec commit 8c5e4a7
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 10 deletions.
3 changes: 3 additions & 0 deletions server/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ export function resolvePath(dir: string, x: string) {
export const TIME_21_MINUTES_AS_SECONDS = 1260;

export const ONE_HOUR = 60 * 60 * 1000;

export const BUILD_DIR = process.env.WEB_BUILD_DIR || path.join(__dirname, "../../web/build")
export const INDEX_FILE = path.join(BUILD_DIR, "index.html");
4 changes: 2 additions & 2 deletions server/package-lock.json

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

2 changes: 1 addition & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"notion2anki"
],
"author": "Alexander Alemayhu",
"version": "0.14.4",
"version": "0.14.5",
"engines": {
"node": ">=12.0.0"
},
Expand Down
3 changes: 2 additions & 1 deletion server/routes/users/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import RequireAuthentication from "../../middleware/RequireAuthentication";
import updatePassword from "../../lib/User/updatePassword";
import comparePassword from "../../lib/User/comparePassword";
import hashPassword from "../../lib/User/hashPassword";
import { INDEX_FILE } from "../../lib/constants";

const router = express.Router();

Expand Down Expand Up @@ -195,7 +196,7 @@ router.get("/r/:id", async (req, res, next) => {
const reset_token = req.params.id;
const isValid = await TokenHandler.IsValidResetToken(reset_token);
if (isValid) {
return res.sendFile(path.join(distDir, "index.html"));
return res.sendFile(INDEX_FILE);
}
return res.redirect("/login#login");
} catch (err) {
Expand Down
11 changes: 5 additions & 6 deletions server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ if (IsDebug()) {
}
}

import { ALLOWED_ORIGINS } from "./lib/constants";
import { ALLOWED_ORIGINS, BUILD_DIR, INDEX_FILE } from "./lib/constants";
import ErrorHandler from "./lib/misc/ErrorHandler";

// Server Endpoints
Expand Down Expand Up @@ -43,7 +43,6 @@ if (!process.env.WORKSPACE_BASE) {

function serve() {
const templateDir = path.join(__dirname, "templates");
const distDir = path.join(__dirname, "../web/build");
const app = express();

app.use(express.json());
Expand All @@ -57,18 +56,18 @@ function serve() {
}

app.use("/templates", express.static(templateDir));
app.use(express.static(distDir));
app.use(express.static(BUILD_DIR));
app.use("/checks", checks);
app.use("/version", version);

app.get("/search*", RequireAuthentication, async (_req, res) => {
res.sendFile(path.join(distDir, "index.html"));
res.sendFile(INDEX_FILE);
});

app.get("/login", async (req, res) => {
const user = await TokenHandler.GetUserFrom(req.cookies.token);
if (!user) {
res.sendFile(path.join(distDir, "index.html"));
res.sendFile(INDEX_FILE);
} else {
res.redirect("/search");
}
Expand All @@ -85,7 +84,7 @@ function serve() {

// Note: this has to be the last handler
app.get("*", (_req, res) => {
res.sendFile(path.join(distDir, "index.html"));
res.sendFile(INDEX_FILE);
});

app.use(
Expand Down

0 comments on commit 8c5e4a7

Please sign in to comment.