mirror of
				https://gitlab.com/MrFry/mrfrys-node-server
				synced 2025-04-01 20:24:18 +02:00 
			
		
		
		
	added support for DOMAIN env var insted of file
This commit is contained in:
		
							
								
								
									
										25
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										25
									
								
								README.md
									
									
									
									
									
								
							| @@ -38,17 +38,21 @@ threads, and serve more requests at once. The used cores can be limited with env | |||||||
|  |  | ||||||
| Run `./scripts/setup.sh`, then `npm run dev` for development, or `npm run start` for prod. | Run `./scripts/setup.sh`, then `npm run dev` for development, or `npm run start` for prod. | ||||||
|  |  | ||||||
| **Gitlab specific notes**: You need a gitlab account with SSH keys set up in order to be able to use |  | ||||||
| SSH for cloning this (and any) project. This includes git modules of this project. See |  | ||||||
| `CLONE_WITH_HTTPS` in environment variables below. |  | ||||||
|  |  | ||||||
| On the first run there will be a number of errors, that some files weren't found. Please create them | On the first run there will be a number of errors, that some files weren't found. Please create them | ||||||
| according to the messages, these are necessary for the server to function. | according to the messages, these are necessary for the server to function. | ||||||
|  |  | ||||||
| There will be also a lot of information about files and other necessary things being created. **Please | There will be also a lot of information about files and other necessary things being created. **Please | ||||||
| read them very carefully, you should know about what was created!** | read them very carefully, you should know about what was created!** | ||||||
|  |  | ||||||
| The setup script can be also used to update and rebuild all git modules. | ### Setup notes | ||||||
|  |  | ||||||
|  |  * **Gitlab specific notes**: You need a gitlab account with SSH keys set up in order to be able to use | ||||||
|  |    SSH for cloning this (and any) project. This includes git modules of this project. See | ||||||
|  |    `CLONE_WITH_HTTPS` in environment variables below. | ||||||
|  |  * The `DOMAIN` env var can be replaced by making the file `./data/domain`. This is necesarry, to make | ||||||
|  |    xmlhttp requests and some redirects work in statically built HTML files (by next.js), in the | ||||||
|  |    userscript, and in the server itself. | ||||||
|  |  * The setup script can be also used to update and rebuild all git modules. | ||||||
|  |  | ||||||
| ## Web server paths | ## Web server paths | ||||||
|  |  | ||||||
| @@ -63,12 +67,12 @@ server instances, and merge the response data to its own databases. | |||||||
| To setup P2P functionality you have to create a few files in `./data/p2p`: | To setup P2P functionality you have to create a few files in `./data/p2p`: | ||||||
|  |  | ||||||
|  * `selfInfo.json`: information of this peer. Required: |  * `selfInfo.json`: information of this peer. Required: | ||||||
|     ``` |     ```json | ||||||
|     { |     { | ||||||
|         "name": "any name you choose", |         "name": "any name you choose", | ||||||
|         "contact": "contact to server administrator (irc server, e-mail, anything)", |         "contact": "contact to server administrator (irc server, e-mail, anything)", | ||||||
|         "host": "server host (like somesite.com, without 'http(s):// and /-s')", |         "host": "server host (like somesite.com, without 'http(s):// and /-s')", | ||||||
|         "pw": "password to the host, so the server can log in there", |         "pw": "password to the host, so the server can log in there. Please use a dedicated password, that only the server uses!", | ||||||
|         "port": "server port, number" |         "port": "server port, number" | ||||||
|     } |     } | ||||||
|     ``` |     ``` | ||||||
| @@ -153,13 +157,14 @@ result can be found in the directory it was ran. | |||||||
|  | NS_LOGLEVEL | number | Debug log level, 0 is the least verbose | |  | NS_LOGLEVEL | number | Debug log level, 0 is the least verbose | | ||||||
|  | NS_NOLOG | boolean | If logging should be skipped | |  | NS_NOLOG | boolean | If logging should be skipped | | ||||||
|  | NS_SQL_DEBUG_LOG | boolean | If the SQL queries should be logged | |  | NS_SQL_DEBUG_LOG | boolean | If the SQL queries should be logged | | ||||||
|  |  | DOMAIN | string | The domain that the server should use for redirects, cookies, etc. ex.: `qmining.com`, without 'https://', and '/'-s. If not specified `./data/domain` will be used | | ||||||
|  |  | ||||||
| ### For the setup script | ### For the setup script (`./scripts/setup.sh`) | ||||||
|  |  | ||||||
|  | Name | Type | Description | |  | Name | Type | Description | | ||||||
|  | --- | --- | --- | |  | --- | --- | --- | | ||||||
|  | CLONE_WITH_HTTPS | boolean | By default git clones with ssh, which can cause troubles. Set this |  | CLONE_WITH_HTTPS | boolean | By default git clones with ssh, which can cause troubles. Set this | ||||||
|  to use https for git modules | |  | DOMAIN | string | The domain that the server should use for redirects, cookies, etc. ex.: `qmining.com`, without 'https://', and '/'-s. If not specified `./data/domain` will be used | | ||||||
|  |  | ||||||
| ## npm scripts | ## npm scripts | ||||||
|  |  | ||||||
| @@ -209,7 +214,7 @@ https://gitlab.com/MrFry/moodle-test-userscript | |||||||
| │   ├── admins.json                forum admins. should be removed and should use admin column from user db | │   ├── admins.json                forum admins. should be removed and should use admin column from user db | ||||||
| │   ├── apiRootRedirectTo          url where domain/api should redirect to | │   ├── apiRootRedirectTo          url where domain/api should redirect to | ||||||
| │   ├── dbs/                       directory for databases, and for their backups | │   ├── dbs/                       directory for databases, and for their backups | ||||||
| │   ├── domain                     the domain the server is hosted on | │   ├── domain                     the domain the server is hosted on. Used when `DOMAIN` env var is empty | ||||||
| │   ├── f/                         user files received TODO: check if this is needed | │   ├── f/                         user files received TODO: check if this is needed | ||||||
| │   ├── links.json                 urls for irc, patreon and donate | │   ├── links.json                 urls for irc, patreon and donate | ||||||
| │   ├── nolog                      ids of users separated by new lines to ignore on logging | │   ├── nolog                      ids of users separated by new lines to ignore on logging | ||||||
|   | |||||||
| @@ -38,10 +38,18 @@ makeNextSubmodule() { | |||||||
| checkFile "$PWD/src/server.ts" | checkFile "$PWD/src/server.ts" | ||||||
| checkFile "$PWD/package.json" | checkFile "$PWD/package.json" | ||||||
| checkFile "$PWD/package-lock.json" | checkFile "$PWD/package-lock.json" | ||||||
| if [ ! -f "${domainPath}" ]; then |  | ||||||
|     error "${domainPath} does not exist!" | domain="${DOMAIN}" | ||||||
|     echo "Please create it, and re-run this script." |  | ||||||
|     echo "Expected content: domain, ex.:'frylabs.net' (without http:// and /)" | if [ -z "${domain}" ] && [ -f "${domainPath}" ]; then | ||||||
|  |     domain=$(cat "${domainPath}") | ||||||
|  | fi | ||||||
|  |  | ||||||
|  | if [ -z "${domain}" ]; then | ||||||
|  |     error "DOMAIN is not set, and ${domainPath} does not exist!" | ||||||
|  |     echo "Set DOMAIN to the preferred domain" | ||||||
|  |     echo "Or please create the file ${domainPath}, and re-run this script" | ||||||
|  |     echo "Expected content: domain, ex.:'frylabs.net' (without 'http://' and '/')" | ||||||
|     exit 1 |     exit 1 | ||||||
| fi | fi | ||||||
|  |  | ||||||
| @@ -113,3 +121,4 @@ fi | |||||||
|  |  | ||||||
| # ------------------------------------------------------------------------------------ | # ------------------------------------------------------------------------------------ | ||||||
| log "Done! development mode: 'npm run dev', prod mode: 'npm run start', tests: 'npm run test'" | log "Done! development mode: 'npm run dev', prod mode: 'npm run start', tests: 'npm run test'" | ||||||
|  | exit 0 | ||||||
|   | |||||||
| @@ -1,3 +0,0 @@ | |||||||
| { |  | ||||||
|     "savedQuestionsFileName": "savedQuestions.json" |  | ||||||
| } |  | ||||||
							
								
								
									
										15
									
								
								src/constants.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								src/constants.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,15 @@ | |||||||
|  | import { paths } from './utils/files' | ||||||
|  | import utils from './utils/utils' | ||||||
|  |  | ||||||
|  | const domain = process.env.DOMAIN || utils.ReadFile(paths.domainFile).trim() | ||||||
|  |  | ||||||
|  | if (!domain) { | ||||||
|  |     throw new Error( | ||||||
|  |         `Domain is undefined! Should be set with 'DOMAIN' environment variable, or written to '${paths.domainFile}'` | ||||||
|  |     ) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | export default { | ||||||
|  |     savedQuestionsFileName: 'savedQuestions.json', | ||||||
|  |     domain: domain, | ||||||
|  | } | ||||||
| @@ -24,10 +24,7 @@ import type { Database } from 'better-sqlite3' | |||||||
|  |  | ||||||
| import logger from '../utils/logger' | import logger from '../utils/logger' | ||||||
| import dbtools from '../utils/dbtools' | import dbtools from '../utils/dbtools' | ||||||
| import { paths } from '../utils/files' | import constants from '../constants' | ||||||
| import utils from '../utils/utils' |  | ||||||
|  |  | ||||||
| const domain = utils.ReadFile(paths.domainFile).trim() |  | ||||||
|  |  | ||||||
| interface Options { | interface Options { | ||||||
|     userDB: Database |     userDB: Database | ||||||
| @@ -56,7 +53,7 @@ function renderLogin(req: Request, res: Response) { | |||||||
|     } else { |     } else { | ||||||
|         res.render('login', { |         res.render('login', { | ||||||
|             useHttp: process.env.NS_NO_HTTPS_FORCE, |             useHttp: process.env.NS_NO_HTTPS_FORCE, | ||||||
|             domain: domain, |             domain: constants.domain, | ||||||
|         }) |         }) | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -96,6 +96,7 @@ interface SyncDataRes { | |||||||
| function updateThirdPartyPeers( | function updateThirdPartyPeers( | ||||||
|     newVal: Omit<PeerInfo, 'publicKey' | 'name' | 'contact'>[] |     newVal: Omit<PeerInfo, 'publicKey' | 'name' | 'contact'>[] | ||||||
| ) { | ) { | ||||||
|  |     // TODO: check if thirdPartyPeersFile exists! | ||||||
|     const prevVal = utils.ReadJSON<PeerInfo[]>(paths.thirdPartyPeersFile) |     const prevVal = utils.ReadJSON<PeerInfo[]>(paths.thirdPartyPeersFile) | ||||||
|     const dataToWrite = newVal.reduce((acc, peer) => { |     const dataToWrite = newVal.reduce((acc, peer) => { | ||||||
|         const isIncluded = acc.find((x) => { |         const isIncluded = acc.find((x) => { | ||||||
| @@ -963,6 +964,7 @@ function setup(data: SubmoduleData): Submodule { | |||||||
|             remoteInfo: getSelfInfo(), |             remoteInfo: getSelfInfo(), | ||||||
|         } |         } | ||||||
|  |  | ||||||
|  |         // TODO: hostname is invalid, should be remote host? | ||||||
|         let hostToLog = req.hostname |         let hostToLog = req.hostname | ||||||
|         if (remoteHost) { |         if (remoteHost) { | ||||||
|             const remotePeerInfo = peers.find((peer) => { |             const remotePeerInfo = peers.find((peer) => { | ||||||
| @@ -998,6 +1000,7 @@ function setup(data: SubmoduleData): Submodule { | |||||||
|                 ) |                 ) | ||||||
|                 if (remoteHost.includes(':')) { |                 if (remoteHost.includes(':')) { | ||||||
|                     const [host, port] = remoteHost.split(':') |                     const [host, port] = remoteHost.split(':') | ||||||
|  |                     // TODO: add public key | ||||||
|                     updateThirdPartyPeers([ |                     updateThirdPartyPeers([ | ||||||
|                         { |                         { | ||||||
|                             host: host, |                             host: host, | ||||||
|   | |||||||
| @@ -60,7 +60,7 @@ import { | |||||||
|     SearchResultQuestion, |     SearchResultQuestion, | ||||||
| } from '../../../utils/qdbUtils' | } from '../../../utils/qdbUtils' | ||||||
| import { paths } from '../../../utils/files' | import { paths } from '../../../utils/files' | ||||||
| import constants from '../../../constants.json' | import constants from '../../../constants' | ||||||
| import { | import { | ||||||
|     isJsonValidAndLogError, |     isJsonValidAndLogError, | ||||||
|     TestUsersSchema, |     TestUsersSchema, | ||||||
|   | |||||||
| @@ -42,6 +42,7 @@ import dbtools from './utils/dbtools' | |||||||
| import reqlogger from './middlewares/reqlogger.middleware' | import reqlogger from './middlewares/reqlogger.middleware' | ||||||
| import idStats from './utils/ids' | import idStats from './utils/ids' | ||||||
| import { paths, validateFiles } from './utils/files' | import { paths, validateFiles } from './utils/files' | ||||||
|  | import constants from './constants' | ||||||
|  |  | ||||||
| const logFile = paths.logDir + logger.logFileName | const logFile = paths.logDir + logger.logFileName | ||||||
| const vlogFile = paths.vlogDir + logger.logFileName | const vlogFile = paths.vlogDir + logger.logFileName | ||||||
| @@ -226,8 +227,6 @@ app.use( | |||||||
|     }) |     }) | ||||||
| ) | ) | ||||||
|  |  | ||||||
| const domain = utils.ReadFile(paths.domainFile).trim() |  | ||||||
|  |  | ||||||
| Object.keys(modules).forEach(function (key) { | Object.keys(modules).forEach(function (key) { | ||||||
|     const module = modules[key] |     const module = modules[key] | ||||||
|     try { |     try { | ||||||
| @@ -240,7 +239,7 @@ Object.keys(modules).forEach(function (key) { | |||||||
|  |  | ||||||
|         if (mod.setup) { |         if (mod.setup) { | ||||||
|             mod.setup({ |             mod.setup({ | ||||||
|                 url: domain, |                 url: constants.domain, | ||||||
|                 userDB: userDB, |                 userDB: userDB, | ||||||
|                 publicdirs: module.publicdirs, |                 publicdirs: module.publicdirs, | ||||||
|                 nextdir: module.nextdir, |                 nextdir: module.nextdir, | ||||||
| @@ -268,7 +267,7 @@ app.get('*', (req, res) => { | |||||||
|     if (req.is('application/json')) { |     if (req.is('application/json')) { | ||||||
|         res.status(404).end() |         res.status(404).end() | ||||||
|     } else { |     } else { | ||||||
|         res.status(404).render('404', { domain: domain }) |         res.status(404).render('404', { domain: constants.domain }) | ||||||
|     } |     } | ||||||
| }) | }) | ||||||
|  |  | ||||||
|   | |||||||
| @@ -124,9 +124,8 @@ export const files = { | |||||||
|  |  | ||||||
|     domainFile: { |     domainFile: { | ||||||
|         path: 'data/domain', |         path: 'data/domain', | ||||||
|         errorIfMissing: true, |  | ||||||
|         description: |         description: | ||||||
|             'server domain for cookies and stuff, for ex.: "frylabs.net", no "http://" and things like that, just the domain', |             'server domain for cookies and stuff, for ex.: "qmining.com", no "http://" and things like that, just the domain', | ||||||
|     }, |     }, | ||||||
|     // -------------------------------------------------------------------------------- |     // -------------------------------------------------------------------------------- | ||||||
|     // stats files |     // stats files | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user