Skip to content

Commit

Permalink
feat: docker support
Browse files Browse the repository at this point in the history
  • Loading branch information
Lemmmy committed Dec 21, 2020
1 parent 6d9bac9 commit b4c9983
Show file tree
Hide file tree
Showing 23 changed files with 460 additions and 414 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
npm-debug.log
.env
19 changes: 19 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Publish Docker Image
on:
release:
types: [published]
jobs:
push_to_registry:
name: Push Docker image to GitHub Container Registry
runs-on: ubuntu-latest
steps:
- name: Check out the repo
uses: actions/checkout@v2
- name: Push to GitHub Packages
uses: docker/build-push-action@v2
with:
username: ${{ secrets.GHCR_USERNAME }}
password: ${{ secrets.GHCR_PAT }}
registry: ghcr.io
repository: ${{ github.repository }}
tag_with_ref: true
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ npm-debug.log
/.package.json

/static/docs
/static/kristwallet
/static/kristwallet

.env
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
"freenonce",
"getbalance",
"getwork",
"ghcr",
"gitlog",
"keepalive",
"listtx",
"lpush",
"lrange",
"ltrim",
"richapi",
"sanitise",
"setwork",
Expand Down
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM node:14-alpine
WORKDIR /usr/src/krist

# Install packages
COPY package*.json ./
RUN npm install

# Install source
COPY . .

# Run Krist
EXPOSE 8080
ENV NODE_ENV=production
CMD ["node", "index.js"]
67 changes: 60 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,76 @@ This is the new official Krist node. It is written in Node.js.
The documentation is generated with [apiDoc](http://apidocjs.com). You can find
a live copy of the API documentation [here](http://krist.ceriat.net/docs).

## Installation
## Docker Installation

The preferred method of installation is Docker. The Docker images are published
on the GitHub Container Registry under `ghcr.io/tmpim/krist`.

MySQL/MariaDB and Redis are required too. There are two options to connect to
the databases. The easier way is to run MySQL and Redis on the host machine,
and specify the `DB_HOST=${DOCKER_GATEWAY}` and `REDIS_HOST=${DOCKER_GATEWAY}`
environment variables to Krist. Alternatively, you can run MariaDB and Redis
in Docker too (left as an exercise to the reader).

Example usage with Docker Compose:

```yml
version: "3.9"
services:
krist:
image: "ghcr.io/tmpim/krist:latest"
environment:
- DB_PASS=${DB_PASS}
- PUBLIC_URL=krist.ceriat.net
- DB_HOST=172.17.0.1
- REDIS_HOST=172.17.0.1
ports:
- "127.0.0.1:8080:8080"
restart: unless-stopped
```
## Manual Installation
### Requirements
- Node.js v14
- MySQL or MariaDB
- Redis
### Installation
Installation is fairly straight-forward. Simply clone the project, run
`npm i` to install the required dependencies and then run `node main`.
`npm i` to install the required dependencies and then run `node .`.

### Webserver Configuration
#### Webserver Configuration

This Krist node is supposed to be ran behind a serverside proxy. The file
`casket_example.casket` includes a basic configuration for how to set up the
proxy in [Casket](https://github.com/tmpim/casket). The Node.js webserver is not
designed to and should not be exposed to the public web.
designed to and should not be exposed to the public web. HTTPS is required.

## Configuration

An example configuration file can be found at `config.example.js`. To configure
the project, make a copy of this file and change anything required, then save it
to `config.js`.
Basic configuration is now done via environment variables. You must supply the
following environment variables:

- `DB_PASS` - The password of the database user.
- `PUBLIC_URL` - The FQDN of the Krist server (e.g. `krist.ceriat.net`).

The following optional environment variables may also be specified:

- `DB_HOST` - The hostname of the database (defaults to `127.0.0.1`).
- `DB_PORT` - The port of the database (defaults to `3306`).
- `DB_NAME` - The name of the database (defaulst to `krist`).
- `DB_USER` - The username of the database user (defaults to `krist`).
- `WEB_LISTEN` - The port that the webserver listens on.
- `REDIS_HOST` - The hostname of the redis server (defaults to `127.0.0.1`).
- `REDIS_PORT` - The port of the redis server (defaults to `6379`).
- `REDIS_PREFIX` - The prefix of the redis keys (defaults to `krist:`).
- `NODE_ENV` - Either `development` or `production`. If `development`, the
Krist server runs in debug mode (defaults to `development`).

For convenience, you may specify environment variables in a `.env` file.

## License

Expand Down
63 changes: 0 additions & 63 deletions config.example.js

This file was deleted.

19 changes: 16 additions & 3 deletions main.js → index.js
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* For more project information, see <https://github.com/Lemmmy/Krist>.
*/

const config = require("./config.js");
require("dotenv").config();
const package = require("./package.json");
const chalk = require("chalk");

Expand All @@ -31,16 +31,29 @@ require("console-stamp")(console, {
}
});

const REQUIRED_ENV_VARS = [
"DB_PASS",
"PUBLIC_URL"
];
const missing = REQUIRED_ENV_VARS.filter(e => !process.env[e]);
if (missing.length) {
console.error(chalk.bold.red("Missing environment variables:"));
console.error(missing.map(e => chalk.red(e)).join(", "));
process.exit(1);
}

const database = require("./src/database.js");
const redis = require("./src/redis.js");
const webserver = require("./src/webserver.js");

console.log(chalk`Starting {bold ${package.name}} {blue ${package.version}}...`);

async function main() {
redis.init();
await database.init();

require("./src/krist.js").init();
if (config.debugMode) require("./src/debug.js");
if (process.env.NODE_ENV !== "production") require("./src/debug.js");

return webserver.init();
}
Expand Down
50 changes: 50 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "Krist",
"version": "2.1.3",
"name": "krist",
"version": "2.2.0",
"description": "The new Krist node written in node.js.",
"main": "main.js",
"main": "index.js",
"scripts": {
"start": "node main.js",
"docs": "apidoc -i src -o static/docs"
Expand All @@ -21,13 +21,16 @@
"body-parser": "^1.14.2",
"chalk": "^4.1.0",
"console-stamp": "^0.2.0",
"dotenv": "^8.2.0",
"express": "^5.0.0-alpha.8",
"express-rate-limit": "^2.1.0",
"express-ws": "^1.0.0-rc.2",
"gitlog": "^2.1.2",
"handy-redis": "^2.1.0",
"moment": "^2.11.1",
"mysql2": "^2.1.0",
"node-uuid": "^1.4.7",
"redis": "^3.0.2",
"request": "^2.69.0",
"sequelize": "^6.3.5",
"swig": "^1.4.2"
Expand Down
2 changes: 1 addition & 1 deletion src/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ Blocks.submit = async function(hash, address, nonce) {
const value = await Blocks.getBlockValue();
const time = new Date();

const oldWork = krist.getWork();
const oldWork = await krist.getWork();

const seconds = (time - lastBlock.time) / 1000;
const targetWork = seconds * oldWork / krist.getSecondsPerBlock();
Expand Down
9 changes: 9 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
walletVersion: 16,
nonceMaxSize: 24,
nameCost: 500,
minWork: 100,
maxWork: 100000,
workFactor: 0.025,
secondsPerBlock: 60
};
Loading

0 comments on commit b4c9983

Please sign in to comment.