diff --git a/.anon.env b/.anon.env index f2b75b2e..8995acc4 100644 --- a/.anon.env +++ b/.anon.env @@ -12,4 +12,4 @@ MARIADB_PASSWORD: mariadb_password NOW_DB_NAME: now_anon LOG_DB_NAME: now_log_anon COORDINATOR: IZ - +RUNNING_ENV: dev diff --git a/backend/src/index.ts b/backend/src/index.ts index 50dc6975..5eecfeb9 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -15,8 +15,8 @@ import userRouter from './routes/user' import { responseLogger } from './middlewares/requestLogger' import compression from 'compression' import { logger } from './utils/logger' -import { PORT, BACKEND_MODE } from './utils/config' -import { tokenExtractor, userExtractor, requireLogin } from './middlewares/authenticator' +import { PORT, RUNNING_ENV } from './utils/config' +import { tokenExtractor, userExtractor } from './middlewares/authenticator' import { errorHandler } from './middlewares/errorHandler' import { createTestUsers } from './services/user' import { testDbConnection } from './utils/db' @@ -33,8 +33,6 @@ app.use(userExtractor) app.use(responseLogger) app.use('/user', userRouter) -if (BACKEND_MODE !== 'dev') app.use(requireLogin) - app.use('/locality', localityRouter) app.use('/species', speciesRouter) app.use('/reference', referenceRouter) @@ -47,8 +45,8 @@ app.use('/museum', museumRouter) app.use('/sedimentary-structure', sedimentaryStructureRouter) app.use(errorHandler) app.listen(PORT, async () => { - logger.info(`Server running on port ${PORT} in "${BACKEND_MODE}"-mode`) - if (BACKEND_MODE === 'dev') { + logger.info(`Server running on port ${PORT} in "${RUNNING_ENV}"-mode`) + if (RUNNING_ENV === 'dev') { await testDbConnection() logger.info('Creating test-users...') void createTestUsers() diff --git a/backend/src/middlewares/authenticator.ts b/backend/src/middlewares/authenticator.ts index 524356f2..59e575b7 100644 --- a/backend/src/middlewares/authenticator.ts +++ b/backend/src/middlewares/authenticator.ts @@ -83,8 +83,3 @@ export const userExtractor: Middleware = async (req, res, next) => { next(e) } } - -export const requireLogin: Middleware = (req, _res, next) => { - if (!req.user) throw Error('Login currently required for all data') - next() -} diff --git a/backend/src/services/write/writeUtils.ts b/backend/src/services/write/writeUtils.ts index 5e3b6ecf..4d0b5389 100644 --- a/backend/src/services/write/writeUtils.ts +++ b/backend/src/services/write/writeUtils.ts @@ -2,7 +2,6 @@ Common utility functions for processing saving actions */ import { EditDataType, ReferenceDetailsType } from '../../../../frontend/src/backendTypes' -import { NOW_DB_NAME, IS_LOCAL } from '../../utils/config' import { nowDb } from '../../utils/db' import { logger } from '../../utils/logger' @@ -60,8 +59,8 @@ export const isEmptyValue = (value: string | number | null | undefined) => { return false } -// If we're using test db, we show debug prints. In local development, it will be more verbose. -const debugLevel = NOW_DB_NAME === 'now_test' ? (IS_LOCAL ? 2 : 1) : 0 +// Set this to 1 or 2 for debugging write (higher = more logging) +const debugLevel = 0 export const debugLog = (msg: string, onlyVerbose?: boolean) => { if (debugLevel === 0) return diff --git a/backend/src/utils/config.ts b/backend/src/utils/config.ts index e1b1dd9c..cc5eba92 100644 --- a/backend/src/utils/config.ts +++ b/backend/src/utils/config.ts @@ -5,11 +5,19 @@ export const PORT = process.env.NOWDB_BACKEND_PORT as unknown as number export const LOGIN_VALID_MS = parseInt(process.env.LOGIN_VALID_MINUTES as string) * 60 * 1000 export const MARIADB_PORT = parseInt(process.env.MARIADB_PORT as string) export const USER_CREATION_SECRET = process.env.USER_CREATION_SECRET as string -export const BACKEND_MODE = process.env.BACKEND_MODE as 'dev' | 'prod' | 'test' + +/* Running environment: + dev = development or testing, locally or in GitHub actions + staging = semi-public test version, non-anonymized test-data + prod = production with real data +*/ +const allowedRunningEnvs = ['dev', 'staging', 'prod'] as const +export const RUNNING_ENV = process.env.RUNNING_ENV as (typeof allowedRunningEnvs)[number] +if (!allowedRunningEnvs.includes(RUNNING_ENV)) throw new Error('Invalid RUNNING_ENV') + export const DB_CONNECTION_LIMIT = (process.env.DB_CONNECTION_LIMIT as string) ?? '10' export const NOW_DB_NAME = process.env.NOW_DB_NAME as string export const LOG_DB_NAME = process.env.LOG_DB_NAME as string -export const IS_LOCAL = (process.env.IS_LOCAL as string) === 'true' export const COORDINATOR = process.env.COORDINATOR as string const requiredEnvs = { @@ -20,11 +28,11 @@ const requiredEnvs = { LOGIN_VALID_MS, MARIADB_PORT, USER_CREATION_SECRET, - BACKEND_MODE, + BACKEND_MODE: RUNNING_ENV, NOW_DB_NAME, LOG_DB_NAME, - IS_LOCAL, COORDINATOR, + RUNNING_ENV, } const missingEnvs = Object.entries(requiredEnvs).filter(([, value]) => value === undefined) diff --git a/docker-compose.anon.yml b/docker-compose.anon.yml index 9ab4a1da..5e4a75ac 100644 --- a/docker-compose.anon.yml +++ b/docker-compose.anon.yml @@ -9,6 +9,7 @@ services: - ./frontend/:/usr/src/app ports: - 127.0.0.1:5173:5173 + env_file: .anon.env backend: container_name: nowdb-backend-dev build: diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml index 5ffa6eff..5a8992f1 100644 --- a/docker-compose.dev.yml +++ b/docker-compose.dev.yml @@ -9,6 +9,7 @@ services: - ./frontend/:/usr/src/app ports: - 127.0.0.1:5173:5173 + env_file: .dev.env backend: container_name: nowdb-backend-dev build: diff --git a/docker-compose.yml b/docker-compose.yml index fd3e6bb4..bb8416d0 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -10,6 +10,7 @@ services: - 5173:5173 environment: - BACKEND_URL=http://localhost:4000 + env_file: .env backend: container_name: nowdb-backend build: diff --git a/frontend/src/components/FrontPage.tsx b/frontend/src/components/FrontPage.tsx index ce1e0544..307edffa 100644 --- a/frontend/src/components/FrontPage.tsx +++ b/frontend/src/components/FrontPage.tsx @@ -1,13 +1,16 @@ +import { ENV } from '@/util/config' import Box from '@mui/material/Box' export const FrontPage = () => { return ( -

Nowdatabase

+

NOW Database

Site under construction.

-

- This version is running on test data. May contain incorrect and outdated information. -

+ {ENV !== 'prod' && ( +

+ This version is running on test data. May contain incorrect and outdated information. +

+ )}

Project repository:{' '} https://github.com/nowcommunity/nowdatabase @@ -17,6 +20,13 @@ export const FrontPage = () => { "New Issue" -button on this page:{' '} GitHub Issues

+

+ The old application: https://nowdatabase.luomus.fi/ +

+

+ Information about the database and NOW community:{' '} + https://nowdatabase.org/ +

) } diff --git a/frontend/src/components/NavBar.tsx b/frontend/src/components/NavBar.tsx index 063c4fb2..d40b3bf1 100644 --- a/frontend/src/components/NavBar.tsx +++ b/frontend/src/components/NavBar.tsx @@ -6,6 +6,7 @@ import { api } from '../redux/api' import { MouseEvent, useState } from 'react' import { Role } from '@/types' import { useUser } from '@/hooks/user' +import { ENV } from '@/util/config' type LinkDefinition = { title: string; url: string; children?: LinkDefinition[]; allowedRoles?: Role[] } @@ -108,12 +109,17 @@ export const NavBar = () => { ) } + const getModeText = () => { + if (ENV === 'staging') return '(test)' + if (ENV === 'dev') return '(dev)' + return '' + } return ( - Now Database + Now Database {getModeText()} diff --git a/frontend/src/util/config.ts b/frontend/src/util/config.ts index 97a11871..5baa0e0e 100644 --- a/frontend/src/util/config.ts +++ b/frontend/src/util/config.ts @@ -1 +1,2 @@ export const BACKEND_URL = (import.meta.env.VITE_BACKEND_URL as string | undefined) ?? 'http://localhost:4000/' +export const ENV = import.meta.env.VITE_RUNNING_ENV as 'dev' | 'staging' | 'prod'