Working container with PostgreSQL support.

pull/12/head
Sander Vocke 5 years ago
parent 26a59e419c
commit 055ed3e86d
  1. 1
      .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
client/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.
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.
# Deployment
TODO:
The easiest deployment is by getting it off of Docker Hub.
The following env variables need to be set for MuDBase to work:
- Ranking system
- Have "ranking contexts". These can be stored in the database.
- Per artist (this removes need for "per album", which can be a subset)
- Per tag
- Per playlist
- Have a linking table between contexts <-> artists/songs. This linking table should include an optional ranking score.
- 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.
- 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.
# 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 material UI (and other libs) from CSN
FROM node:12-alpine
FROM node:12-alpine as frontend_builder
# Install base dependencies
WORKDIR /usr/src/mudbase
COPY package*.json ./
# Make a static build of the front-end only.
WORKDIR /usr/src/
COPY client/package*.json ./
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
WORKDIR /usr/src/mudbase/server
WORKDIR /opt/mudbase/server
COPY server/package*.json ./
RUN npm install --only=production
# Install front-end dependencies
WORKDIR /usr/src/mudbase/client
COPY client/package*.json ./
RUN npm install --only=production
# Install back-end source code
WORKDIR /opt/mudbase/server
COPY server/ ./
# Install front-end
WORKDIR /usr/src/mudbase/client
COPY client/ ./
ENV REACT_APP_BACKEND "/api"
RUN npm run-script build
# Install static front-end build
COPY --from=frontend_builder /usr/src/build /opt/mudbase/frontend
# Install back-end
WORKDIR /usr/src/mudbase/server
COPY server/ ./
# Install the API where the back-end expects
# to find it.
# 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
WORKDIR /usr/src/mudbase/server
WORKDIR /opt/mudbase
EXPOSE 8080
ENV PORT 8080
ENV API "/api"
ENV FRONTEND_PREFIX "/"
ENV FRONTEND "../client/build"
CMD [ "npm", "start" ]
ENV FRONTEND "/opt/mudbase/frontend"
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'
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>> {
// Development settings use a simple SQLite file.
development: {
client: "sqlite3",
connection: {
@ -9,36 +10,8 @@ export default <Record<string,any>> {
}
},
// staging: {
// client: "postgresql",
// connection: {
// 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"
// }
// }
// In production, we base the config on an environment
// variable setting.
production: process.env.MUDBASE_DB_CONFIG
};

File diff suppressed because it is too large Load Diff

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

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

Loading…
Cancel
Save