Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fallback environment to local #65

Merged
merged 1 commit into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions backend/src/config/mongo.config.ts

This file was deleted.

4 changes: 2 additions & 2 deletions backend/src/controllers/news.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { DB } from "../repositories/repository";
import { BadRequestError, NotFoundError } from "../errors/HTTPErrors";
import { Article, ArticleType, Paginator } from "@aapc/types";
import { DEFAULT_PER_PAGE, DUMMY_USER } from "../util/const";
import { ArrayResult } from "../util/helper.types";
import { ArticleIn } from "../util/input.types";
import { ArrayResult } from "../util/types/util.types";
import { ArticleIn } from "../util/types/input.types";

export default class NewsController {
static getNews: RequestHandler = async (req, res, next) => {
Expand Down
4 changes: 2 additions & 2 deletions backend/src/controllers/research.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { RequestHandler } from "express";
import { DB } from "../repositories/repository";
import { BadRequestError, NotFoundError } from "../errors/HTTPErrors";
import { DEFAULT_PER_PAGE, DUMMY_USER } from "../util/const";
import { ArrayResult } from "../util/helper.types";
import { ArrayResult } from "../util/types/util.types";
import { Article, ArticleType, Paginator } from "@aapc/types";
import { ArticleIn } from "../util/input.types";
import { ArticleIn } from "../util/types/input.types";

export default class ResearchController {
static getResearch: RequestHandler = async (req, res, next) => {
Expand Down
Empty file.
2 changes: 1 addition & 1 deletion backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const port = process.env.PORT || 3000
app.use(express.json())

app.get("/", (req: Request, res: Response) => {
res.json({ message: "ok", environment: (process.env.ENV?? "unknown").toLowerCase() })
res.json({ message: "ok", environment: (process.env.ENV?? "local").toLowerCase() })
})

app.use(NewsRouter.url, NewsRouter.router())
Expand Down
6 changes: 3 additions & 3 deletions backend/src/repositories/IRepository.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Article, User } from "@aapc/types";
import { ArrayResult, ArrayResultOptions, Nullable } from "../util/helper.types";
import ArticleSorter, { ArticleSortFields } from "./memory/sorters/ArticleSorter";
import UserSorter, { UserSortFields } from "./memory/sorters/UserSorter";
import { ArrayResult, ArrayResultOptions, Nullable } from "../util/types/util.types";
import { ArticleSortFields } from "./memory/sorters/ArticleSorter";
import { UserSortFields } from "./memory/sorters/UserSorter";

export default interface IRepository {
getAllNews(options?: ArrayResultOptions<ArticleSortFields>): Promise<ArrayResult<Article>>
Expand Down
2 changes: 1 addition & 1 deletion backend/src/repositories/memory/MemoryRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import IRepository from "../IRepository";
import users from "./data/users.json"
import news from "./data/news.json"
import researches from "./data/researches.json"
import { ArrayResult, ArrayResultOptions, Nullable } from "../../util/helper.types";
import { ArrayResult, ArrayResultOptions, Nullable } from "../../util/types/util.types";
import ArticleSorter, { ArticleSortFields } from "./sorters/ArticleSorter";
import UserSorter, { UserSortFields } from "./sorters/UserSorter";
import Sorter from "./sorters/Sorter";
Expand Down
22 changes: 20 additions & 2 deletions backend/src/repositories/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,30 @@ import MemoryRepository from "./memory/MemoryRepository";

dotenv.config()

const repo = process.env.REPOSITORY || "memory"
let repo: "memory" | "mongo-dev" | "mongo-prod"

switch (process.env.ENV) {
case "LOCAL": {
repo = "memory"
break
}
case "DEV": {
repo = "mongo-dev"
break
}
case "PROD": {
repo = "mongo-prod"
break
}
default: {
repo = "memory"
}
}

export let DB: IRepository

switch (repo) {
case "memory":
default: // TODO: change when implementing mongo
DB = new MemoryRepository()
break
}
8 changes: 8 additions & 0 deletions backend/src/util/functions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function getRandomID(length: number = 8) {
let result = ''
const characters = 'abcdefghijklmnopqrstuvwxyz0123456789-_'
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * characters.length))
}
return result
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { Article, ArticleType, IArticle, IUser, User } from "@aapc/types";
import { InputValidationError } from "./helper.types";
import { getRandomID } from "../functions";

function getRandomID (length: number = 8) {
let result = ''
const characters = 'abcdefghijklmnopqrstuvwxyz0123456789-_'
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * characters.length))
}
return result
export interface InputValidationError<T> {
field: keyof T
message: string
}

export interface IArticleIn extends Omit<IArticle, "id" | "lastEditedAt" | "publishedAt" | "publisher" | "articleType"> {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,3 @@ export interface ArrayResult<T> {
totalResults: number
results: T[]
}

export interface InputValidationError<T> {
field: keyof T
message: string
}
17 changes: 11 additions & 6 deletions frontend/app/components/Footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,25 @@ import { API_URI } from "@/app/consts";

export default async function Footer (): Promise<React.JSX.Element> {
let backendEnv: string
try {
const req = await fetch(API_URI + "/", { method: "get" })
backendEnv = (await req.json()).environment
} catch (e) {
backendEnv = "offline"
if (process.env.ENV === "PROD") {
backendEnv = "prod"
} else {
try {
const req = await fetch(API_URI + "/", { method: "get" })
backendEnv = (await req.json()).environment ?? "unknown"
} catch (e) {
backendEnv = "offline"
}
}

return (
<footer>
<div className={"h-32 w-full fixed bottom-0 bg-gradient-to-t from-white z-40"}></div>
<div className={`fixed bottom-0 h-16 w-full bg-gradient-to-t from-green-100 flex justify-center items-center z-50`}>
<div className={"py-6 px-12 flex-col flex justify-center items-center"}>
<span className={"font-bold text-xl text-black tracking-tight"}>aapc-nz.org</span>
<span className={"text-secondary text-xs space-x-4" + (process.env.ENV !== 'PROD' ? "" : " hidden")}>
<span>frontend env: <b className={"font-mono"}>{(process.env.ENV ?? "null").toLowerCase()}</b></span>
<span>frontend env: <b className={"font-mono"}>{(process.env.ENV ?? "local").toLowerCase()}</b></span>
<span>backend env: <b className={"font-mono" + (backendEnv === "offline" ? ' text-red-500' : '')}>{backendEnv.toLowerCase()}</b></span>
</span>
</div>
Expand Down
2 changes: 1 addition & 1 deletion frontend/app/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ switch (process.env.ENV) {
}
}

export const API_URI = apiUri
export const API_URI = apiUri // can add custom uri for testing here
export const WEBSITE_NAME = "Aotearoa Airborne Pollen Collective"