Adjust envs and show different text on frontend in prod/dev/staging

This commit is contained in:
ShootingStar91
2024-07-10 17:45:16 +03:00
parent 30ec027e26
commit 11eea36676
11 changed files with 44 additions and 24 deletions
+1 -1
View File
@@ -12,4 +12,4 @@ MARIADB_PASSWORD: mariadb_password
NOW_DB_NAME: now_anon
LOG_DB_NAME: now_log_anon
COORDINATOR: IZ
RUNNING_ENV: dev
+4 -6
View File
@@ -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()
-5
View File
@@ -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()
}
+2 -3
View File
@@ -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
+12 -4
View File
@@ -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)
+1
View File
@@ -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:
+1
View File
@@ -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:
+1
View File
@@ -10,6 +10,7 @@ services:
- 5173:5173
environment:
- BACKEND_URL=http://localhost:4000
env_file: .env
backend:
container_name: nowdb-backend
build:
+11 -1
View File
@@ -1,13 +1,16 @@
import { ENV } from '@/util/config'
import Box from '@mui/material/Box'
export const FrontPage = () => {
return (
<Box>
<h1>Nowdatabase</h1>
<h1>NOW Database</h1>
<p>Site under construction.</p>
{ENV !== 'prod' && (
<p>
<b>This version is running on test data. May contain incorrect and outdated information.</b>
</p>
)}
<p>
Project repository:{' '}
<a href="https://github.com/nowcommunity/nowdatabase">https://github.com/nowcommunity/nowdatabase</a>
@@ -17,6 +20,13 @@ export const FrontPage = () => {
&quot;New Issue&quot; -button on this page:{' '}
<a href="https://github.com/nowcommunity/nowdatabase/issue">GitHub Issues</a>
</p>
<p>
The old application: <a href="https://nowdatabase.luomus.fi/">https://nowdatabase.luomus.fi/</a>
</p>
<p>
Information about the database and NOW community:{' '}
<a href="https://nowdatabase.org/">https://nowdatabase.org/</a>
</p>
</Box>
)
}
+7 -1
View File
@@ -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 (
<AppBar position="static">
<Stack spacing={10} direction="row" marginLeft={2} justifyContent="space-between">
<MenuItem component={Link} to="/">
<Typography component="h1" fontSize={'2.2em'}>
Now Database
Now Database {getModeText()}
</Typography>
</MenuItem>
<MenuList style={{ alignContent: 'center' }}>
+1
View File
@@ -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'