Working container with PostgreSQL support.

pull/12/head
Sander Vocke 5 years ago
parent 26a59e419c
commit 055ed3e86d
  1. 3
      .dockerignore
  2. 25
      README
  3. 51
      deploy/Dockerfile
  4. 12
      deploy/run.sh
  5. 22
      server/knex/knex.ts
  6. 35
      server/knexfile.ts
  7. 633
      server/package-lock.json
  8. 5
      server/package.json
  9. 1
      server/server.ts

@ -1,3 +1,4 @@
node_modules node_modules
client/node_modules client/node_modules
server/node_modules server/node_modules
server/dev.sqlite3

@ -1,13 +1,18 @@
Started from: https://www.freecodecamp.org/news/how-to-make-create-react-app-work-with-a-node-backend-api-7c5c48acb1b0/ # MuDBase
MuDBase is a self-hosted database for music.
TODO: It is made for those who use streaming services such as Spotify for keeping track of their music library and tastes, but don't want to rely solely on them for storing this information.
It is also for power users who would like to organize their music tastes with more powerful tools than only a storage of playlists, albums, artists and songs. MuDBase will offer more rich power features such as tagging and ranking music, and a powerful music querying system.
- Ranking system # Deployment
- Have "ranking contexts". These can be stored in the database.
- Per artist (this removes need for "per album", which can be a subset) The easiest deployment is by getting it off of Docker Hub.
- Per tag The following env variables need to be set for MuDBase to work:
- Per playlist
- Have a linking table between contexts <-> artists/songs. This linking table should include an optional ranking score. - MUDBASE_DB_CONFIG: This env variable should contain a JSON struct with MuDBase's database configuration. The JSON should hold a valid Knex.js config struct. In principle, all dialects supported by Knex are supported (postgresql, mysql, mssql, sqlite3, oracledb), but only sqlite3 and postgresql are tested.
- The ranking score allows ranking songs per query or per query element. It is a floating point so we can always insert stuff in between.
- Visually, the system shows ranked items in a table and unranked items in another. User can drag to rank. # Architecture
A Node.js Express back-end with a React front-end.
Started from: https://www.freecodecamp.org/news/how-to-make-create-react-app-work-with-a-node-backend-api-7c5c48acb1b0/

@ -4,38 +4,49 @@
# TODO remove client source # TODO remove client source
# TODO material UI (and other libs) from CSN # TODO material UI (and other libs) from CSN
FROM node:12-alpine FROM node:12-alpine as frontend_builder
# Install base dependencies # Make a static build of the front-end only.
WORKDIR /usr/src/mudbase WORKDIR /usr/src/
COPY package*.json ./ COPY client/package*.json ./
RUN npm install --only=production RUN npm install --only=production
COPY client/ ./
# Prime the front-end build to find the back-end at /api.
ENV REACT_APP_BACKEND "/api"
RUN npm run-script build
FROM node:12-alpine
# Some useful tools for servicing
RUN apk update && apk upgrade && apk add bash
# Install back-end dependencies # Install back-end dependencies
WORKDIR /usr/src/mudbase/server WORKDIR /opt/mudbase/server
COPY server/package*.json ./ COPY server/package*.json ./
RUN npm install --only=production RUN npm install --only=production
# Install front-end dependencies # Install back-end source code
WORKDIR /usr/src/mudbase/client WORKDIR /opt/mudbase/server
COPY client/package*.json ./ COPY server/ ./
RUN npm install --only=production
# Install front-end # Install static front-end build
WORKDIR /usr/src/mudbase/client COPY --from=frontend_builder /usr/src/build /opt/mudbase/frontend
COPY client/ ./
ENV REACT_APP_BACKEND "/api"
RUN npm run-script build
# Install back-end # Install the API where the back-end expects
WORKDIR /usr/src/mudbase/server # to find it.
COPY server/ ./ # TODO: can we move it somewhere else?
COPY client/src/api.ts /opt/mudbase/client/src/api.ts
# Install the runner script
COPY deploy/run.sh /opt/mudbase/run.sh
# Start # Start
WORKDIR /usr/src/mudbase/server WORKDIR /opt/mudbase
EXPOSE 8080 EXPOSE 8080
ENV PORT 8080 ENV PORT 8080
ENV API "/api" ENV API "/api"
ENV FRONTEND_PREFIX "/" ENV FRONTEND_PREFIX "/"
ENV FRONTEND "../client/build" ENV FRONTEND "/opt/mudbase/frontend"
CMD [ "npm", "start" ] ENV NODE_ENV "production"
ENV ENVIRONMENT "production"
ENTRYPOINT './run.sh'

@ -0,0 +1,12 @@
#!/bin/bash
MUDBASE_DB_CONFIG=$(printenv MUDBASE_DB_CONFIG)
if [ -z "$MUDBASE_DB_CONFIG" ]; then
echo "Cannot start MuDBase without a database configuration." >&2
echo "Please set the MUDBASE_DB_CONFIG env variable to a valid" >&2
echo "Knex.js config JSON object." >&2
exit 1
fi
echo "Starting MuDBase..."
cd server && npm start

@ -1,3 +1,23 @@
const environment = process.env.ENVIRONMENT || 'development' const environment = process.env.ENVIRONMENT || 'development'
import config from '../knexfile'; import config from '../knexfile';
export default require('knex')(config[environment]); import Knex from 'knex';
if (!Object.keys(config).includes(environment)) {
throw "No Knex database configuration was found for environment '" +
environment + "'. Please check your configuration.";
}
var _knex = undefined;
try {
_knex = require('knex')(config[environment]);
} catch (e) {
throw [
"Failed to initialize Knex database connection with config: "
+ JSON.stringify(config[environment]),
e
];
}
const knex:Knex = _knex;
export default knex;

@ -2,6 +2,7 @@
export default <Record<string,any>> { export default <Record<string,any>> {
// Development settings use a simple SQLite file.
development: { development: {
client: "sqlite3", client: "sqlite3",
connection: { connection: {
@ -9,36 +10,8 @@ export default <Record<string,any>> {
} }
}, },
// staging: { // In production, we base the config on an environment
// client: "postgresql", // variable setting.
// connection: { production: process.env.MUDBASE_DB_CONFIG
// database: "my_db",
// user: "username",
// password: "password"
// },
// pool: {
// min: 2,
// max: 10
// },
// migrations: {
// tableName: "knex_migrations"
// }
// },
// production: {
// client: "postgresql",
// connection: {
// database: "my_db",
// user: "username",
// password: "password"
// },
// pool: {
// min: 2,
// max: 10
// },
// migrations: {
// tableName: "knex_migrations"
// }
// }
}; };

File diff suppressed because it is too large Load Diff

@ -14,7 +14,12 @@
"express": "^4.16.4", "express": "^4.16.4",
"jasmine": "^3.5.0", "jasmine": "^3.5.0",
"knex": "^0.21.5", "knex": "^0.21.5",
"mssql": "^6.2.1",
"mysql": "^2.18.1",
"mysql2": "^2.1.0",
"nodemon": "^2.0.4", "nodemon": "^2.0.4",
"oracledb": "^5.0.0",
"pg": "^8.3.3",
"sqlite3": "^5.0.0", "sqlite3": "^5.0.0",
"ts-node": "^8.10.2", "ts-node": "^8.10.2",
"typescript": "~3.7.2" "typescript": "~3.7.2"

@ -13,7 +13,6 @@ knex.migrate.latest().then(() => {
SetupApp(app, knex, apiBase); SetupApp(app, knex, apiBase);
console.log(frontEnd, frontEndPrefix)
if(frontEnd && frontEndPrefix) { if(frontEnd && frontEndPrefix) {
console.log(`Hosting front-end ${frontEnd} at ${frontEndPrefix}.`) console.log(`Hosting front-end ${frontEnd} at ${frontEndPrefix}.`)
app.use(frontEndPrefix, express.static(frontEnd)) app.use(frontEndPrefix, express.static(frontEnd))

Loading…
Cancel
Save