Skip to content

Commit

Permalink
Add database and config file support.
Browse files Browse the repository at this point in the history
  • Loading branch information
ZandercraftGames committed Sep 26, 2023
1 parent f1e9586 commit f49fb81
Show file tree
Hide file tree
Showing 8 changed files with 402 additions and 8 deletions.
35 changes: 35 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# TechnicFlux Configuration File
# For support: https://github.com/Zandercraft/TechnicFlux/
# =========================================================
# This is an example configuration file and is not active.
# Copy and rename it to .env before starting TechnicFlux.
# (Read the README.md file for more detailed instructions)

# (REQUIRED) The connection string for your MongoDB instance (including the database)
# Ex. (No SSL): mongodb://myDatabaseUser:D1fficultP%[email protected]/myDatabaseName
# Ex. (SSL): mongodb+srv://myDatabaseUser:D1fficultP%[email protected]/myDatabaseName
# See https://www.mongodb.com/docs/manual/reference/connection-string/
MONGODB_CONN_STRING="mongodb://localhost:27017/technicflux"

# (REQUIRED) Admin Username and Password
# The username and password for the administrator account. CHANGE THIS!
ADMIN_USER="flux"
ADMIN_PASS="flux"

# (REQUIRED) The name of your TechnicFlux instance.
NAME="A TechnicFlux Instance"

# (REQUIRED) The public hostname of the website (i.e. the full domain name without any path, http://, or https://)
HOST="example.com"

# (REQUIRED) Solder API Key
# 1. Go to https://www.technicpack.net/
# 2. Sign into your account and navigate to My Profile
# 3. Go to Solder Configuration and put your API key here
# 4. Put your solder API URL (http[s]://example.com/api/) in and link it.
API_KEY=""

# (Optional) Default Author Name (for custom files)
# This is the name that will be pre-filled when you go to upload custom files for a modpack.
# Unset behaviour: You will need to specify the author every time.
AUTHOR_NAME="John Doe"
Binary file modified README.md
Binary file not shown.
22 changes: 17 additions & 5 deletions bin/www
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
#!/usr/bin/env node

/**
* Load configuration from .env
*/
require('dotenv').config()


/**
* Module dependencies.
*/

var app = require('../app');
var debug = require('debug')('technicflux:server');
var http = require('http');
const app = require('../app');
const debug = require('debug')('technicflux:server');
const http = require('http');
const database = require('../database')

/**
* Get port from environment and store in Express.
*/

var port = normalizePort(process.env.PORT || '3000');
let port = normalizePort(process.env.PORT || '80');
app.set('port', port);

/**
* Create HTTP server.
*/

var server = http.createServer(app);
let server = http.createServer(app);

/**
* Connect to the Database
*/
database.connectToDB(process.env.MONGODB_CONN_STRING)

/**
* Listen on provided port, on all network interfaces.
Expand Down
103 changes: 103 additions & 0 deletions database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
const mongoose = require('mongoose')

Check failure on line 1 in database.js

View workflow job for this annotation

GitHub Actions / Qodana for JS

Standard code style

Standard code style: Install the 'eslint' package
const { SchemaTypes } = require('mongoose')
const bcrypt = require('bcryptjs')
const Schema = mongoose.Schema

// --- Schemas ---
const userSchema = new Schema({
username: {
type: String,
unique: true
},
display_name: String,
password: String
})

const modpackSchema = new Schema({
name: String,
display_name: String,
url: String,
recommended: String,
latest: String,
builds: [{ type: SchemaTypes.ObjectId, ref: 'technicflux_builds' }],
owners: [{ type:SchemaTypes.ObjectId, ref: 'technicflux_users' }],
contributors: [{ type:SchemaTypes.ObjectId, ref: 'technicflux_users' }]
})

const modSchema = new Schema({
name: String,
pretty_name: String,
version: String,
mc_version: String,
type: String,
md5: String,
author: String,
description: String,
link: String,
donate: String
})

const buildSchema = new Schema({
minecraft: String,
java: String,
memory: Number,
forge: String,
mods: [{ type: SchemaTypes.ObjectId, ref: 'technicflux_mods' }]
})

// --- Models ---
const User = mongoose.model('technicflux_users', userSchema)
const Modpack = mongoose.model('technicflux_modpacks', modpackSchema)

Check warning on line 50 in database.js

View workflow job for this annotation

GitHub Actions / Qodana for JS

Unused local symbol

Unused constant Modpack
const Mod = mongoose.model('technicflux_mods', modSchema)

Check warning on line 51 in database.js

View workflow job for this annotation

GitHub Actions / Qodana for JS

Unused local symbol

Unused constant Mod
const Build = mongoose.model('technicflux_builds', buildSchema)

Check warning on line 52 in database.js

View workflow job for this annotation

GitHub Actions / Qodana for JS

Unused local symbol

Unused constant Build

// --- Functions ---

exports.connectToDB = (connectionString) => {
mongoose.connect(connectionString)
}

// --- User-Related Functions ---

/*
* Creates a new user with the given information.
*/
exports.createUser = (u_username, u_display_name, u_password) => {
// Encrypt the password using bcrypt and save user
return bcrypt.genSalt(10).then((Salt) => {
return bcrypt.hash(u_password, Salt).then((hash) => {
// Create a user object
const newUser = new User({
username: u_username,
display_name: u_display_name,
password: hash
})

// Commit it to the database
return newUser.save().then((user) => {

Check warning on line 77 in database.js

View workflow job for this annotation

GitHub Actions / Qodana for JS

Void function return value used

Void function return value is used
process.stdout.write(`Successfully added new user ${user.username}\n`)
return user
}).catch((reason) => {
process.stdout.write(`ERROR (when saving new user): ${reason}\n`)
return false
})
}).catch((err) => {
// Issue creating hashed password
process.stdout.write(`ERROR (while hashing password): ${err}`)
return false
})
}).catch((err) => {
// Issue creating hashed object salt
process.stdout.write(`ERROR (while generating password salt): ${err}`)
return false
})
}

// --- Modpack-Related Functions ---


// --- Mod-Related Functions ---


// --- Build-Related Functions ---

Loading

0 comments on commit f49fb81

Please sign in to comment.