Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] ass 0.15.0 #220

Draft
wants to merge 382 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 70 commits
Commits
Show all changes
382 commits
Select commit Hold shift + click to select a range
36de04b
fix: redirect to index instead of displaying a generic alert
Jul 17, 2023
04acf00
fix: setup should be allowed even if SQL not defined
Jul 17, 2023
f245f06
feat: attach file size to AssFile
Jul 17, 2023
c9905eb
feat: added file hashing
Jul 18, 2023
7c4829f
feat: bit of a cleanup
Jul 18, 2023
0733200
feat: added interface for interacting with new user API
Jul 18, 2023
b852a95
feat: added empty API router
Jul 18, 2023
8487089
feat: probably improved single object S3 upload
Sep 25, 2023
346aa9b
fix: set S3 region to `auto` if unset
Sep 25, 2023
e515849
feat: added setup panel for admin user
Sep 25, 2023
d5bd01b
feat: added initial API route for posting a new user
Sep 25, 2023
c4b43dd
refactor: delete a lot of old stuff
Sep 28, 2023
0721510
Merge remote-tracking branch 'origin/master' into dev/0.15.0
Sep 28, 2023
d04b4f9
refactor: migrated all the generators
Sep 30, 2023
1c0965c
chore: re-add gfycat data
Sep 30, 2023
f4d05bd
refactor: migrated file operations
Sep 30, 2023
71e2dce
refactor: migrated and deleted some types
Sep 30, 2023
40469ea
feat: migrated some utils
Sep 30, 2023
493321b
refactor: delete old files
Sep 30, 2023
49a2e85
chore: update license year
Sep 30, 2023
d3191c9
remove: this was re-implemented already in a much simpler way
Sep 30, 2023
b2869df
build: remove old package
Sep 30, 2023
851a8a5
feat: updated tycrek Shoelace packages
Sep 30, 2023
263ab28
feat: created `_base_.pug`
Sep 30, 2023
37c5836
feat: slightly modularized frontend
Sep 30, 2023
e8e29d2
feat: add ability to pass pkg version easily
Sep 30, 2023
324cb52
build: remove old packages
Sep 30, 2023
cc42188
build: updated devdeps (type defs)
Sep 30, 2023
9702c39
build: updated regular deps
Sep 30, 2023
ed6213a
build: updated tsconfig.json
Sep 30, 2023
d91520c
feat: use pasted nanoid function (only 130 bytes) to resolve ESM issues
Sep 30, 2023
d4d6e86
fix: update imports for new tsconfig.json
Sep 30, 2023
5f6eccd
refactor: remove old files
Sep 30, 2023
1562e7a
refactor: rename Tailwind files
Sep 30, 2023
39082f4
refactor: renamed views/ and views2/
Sep 30, 2023
6ac59b6
Merge branch 'master' into dev/0.15.0
Sep 30, 2023
059b20e
feat: nice
Oct 2, 2023
65a08ba
Added queries in codeql, change version on ts-build, add docker-build
Gauvino Oct 9, 2023
1eff876
Update and rename docker-build to docker-build.yml
Gauvino Oct 12, 2023
13d395f
Update ts-build.yml
Gauvino Oct 12, 2023
10f4114
Merge branch 'master' into dev/0.15.0
Oct 12, 2023
7f3a86a
build: set minimum engines (Node 20 will be LTS in about a week)
Oct 12, 2023
fa17f09
build: update packages
Oct 12, 2023
9373a3c
Update docker-build.yml
Gauvino Oct 13, 2023
977fd22
Merge pull request #234 from Gauvino/docker-build
Oct 13, 2023
5efb5a5
chore: distinguish CI skip flags
Oct 13, 2023
61c8493
fix: missed this undistinguished skip flag
Oct 13, 2023
7bd9a56
fix: don't attempt to publish if the test failed
Oct 13, 2023
3384dac
fix: on push?
Oct 13, 2023
a39a13e
fix: I think I got it
Oct 13, 2023
4f49312
fix: don't compile every time because it takes forever
Oct 14, 2023
9f94eee
Merge branch 'master' into dev/0.15.0
Oct 14, 2023
e8c3e7a
fix: rename file
Oct 14, 2023
829e52a
fix: maybe `push:` isn't required as all things run twice?
Oct 14, 2023
3efc71b
fix: probably don't need this either
Oct 14, 2023
977c97f
chore: remove commented lines (and test a skip flag) [skip ci:ts]
Oct 14, 2023
cb14493
feat: wait for existing test; push to commit hash tag as well
Oct 14, 2023
18cd093
fix: remove invalid line [docker build]
Oct 14, 2023
2115a4f
fix: grr [docker build]
Oct 14, 2023
92e3efb
fix: ugh [docker build]
Oct 14, 2023
4ce222d
fix: incorrect List format [docker build]
Oct 14, 2023
b6aa350
fix: remove unnecessary line maybe [docker build]
Oct 14, 2023
1406fd4
fix: no clue why this is being skipped [docker build]
Oct 14, 2023
2801887
fix: apparently `pull_request` was skipped even when it worked
Oct 14, 2023
0bf02e9
fix: forgot [docker build] so hope for the best
Oct 14, 2023
952eb11
fix: maybe this was correct the first time 🤷
Oct 14, 2023
c8892bf
IM NOT COMMIT FARMING I FORGOR [docker build]
Oct 14, 2023
25f29b4
fix: i cri [docker build]
Oct 14, 2023
455a439
*sighhhhh*
Oct 14, 2023
13c76f7
yeah [docker build]
Oct 14, 2023
6b67f19
fix: forget it i dont want a short hash [docker build] ofosdojfha
Oct 14, 2023
8ca70ce
fix: using my username in a Secret censored the tag 😭 [docker build]
Oct 14, 2023
96ae0ac
fix: oops invalid reference format (fml) [docker build]
Oct 14, 2023
fc42b0f
docker: use Hub image
Oct 14, 2023
f915e34
docker: add compose volume [skip ci:ts]
Oct 14, 2023
121a7cd
fix: idfk anymore
Oct 14, 2023
e5d627e
chore: update `Dockerfile` and `compose.yaml` [skip ci:ts]
Oct 14, 2023
afc943c
Merge branch 'tycrek:master' into master
NotAShelf Oct 14, 2023
e201da1
refactor: modularize screenshot script
NotAShelf Oct 14, 2023
25a32b3
feat: sanity checks in sample screenshotter
NotAShelf Oct 14, 2023
1da9ce4
Merge branch 'master' into dev/0.15.0
Oct 14, 2023
9e1c8a2
feat: full-height content block
Oct 14, 2023
e92f916
fix: forgor the title block
Oct 14, 2023
0a65222
feat: dummy non-functioning login page [skip ci:ts]
Oct 14, 2023
e7213f2
Merge pull request #135 from NotAShelf/master
Oct 14, 2023
4bdfa09
Merge branch 'master' into dev/0.15.0
Oct 14, 2023
a559866
fix: indicate that I'm not a robot
Oct 14, 2023
fae0d0b
refactor: added missing explicit semicolons
Oct 14, 2023
e94c0bc
refactor: delete old Tailwind CSS
Oct 14, 2023
36be2d0
refactor: delete old Docker deploy scripts (no longer needed)
Oct 14, 2023
d0a2bf7
Merge branch 'master' into dev/0.15.0
Oct 14, 2023
35fcee6
feat: added ass version to Request metadata (see #223 for info)
Oct 14, 2023
6dc4070
build: use `@tsconfig/node20` [docker build]
Oct 14, 2023
fce65a2
feat: make util style
Oct 14, 2023
8065100
feat: added basic login UI (non-functional)
Oct 14, 2023
98826a0
feat: added login router
Oct 14, 2023
789f91e
feat: added base login frontend JS
Oct 14, 2023
de41cd0
feat: improved `fix-frontend-js.js` to fix multiple files
Oct 14, 2023
701c808
feat: set up initial frontend JS
Oct 14, 2023
72a5829
feat: added base implementation of admin routing
Oct 14, 2023
9d3dc96
feat: added base implementation of user routing
Oct 14, 2023
b62a1ea
build: add session packages
Oct 14, 2023
b7f79a4
feat: added session types & set up session storage
Oct 14, 2023
7f04d36
feat: made basic page routers universal
Oct 14, 2023
5e7016a
feat: move setup POST to /api/setup
Oct 14, 2023
38c235c
feat: improve setup logs
Oct 14, 2023
1a07edf
build: added new NPM script `fresh` for clean runs
Oct 14, 2023
49bd572
feat: made these functions universal (will be non-duplicate soon)
Oct 15, 2023
69f1776
feat: added base implementation of login validation middleware
Oct 15, 2023
c257718
feat: added login flow to frontend
Oct 15, 2023
b4ebe8b
feat: added UNTESTED MySQL getAll
Oct 15, 2023
285e5cc
feat: added getAll to `data.ts` (MySQL is UNTESTED)
Oct 15, 2023
5832a69
feat: authenticate sessions with bcrypt
Oct 15, 2023
5d06745
refactor: use consistent import ordering
Oct 16, 2023
04ef991
fix: remove unnecessary function & move session setup step to exist...
Oct 22, 2023
b46198e
feat: added login checker/redirection flow
Oct 22, 2023
4b68cb7
feat: redirect user to the page they originally requested
Oct 22, 2023
c250938
fix: add proper type here (excuse to do a [docker build] )
Oct 22, 2023
2291982
Merge pull request #239 from tycrek/feature/15-login [docker build]
Oct 22, 2023
c84f507
chore: minor cleanup
Oct 22, 2023
1c12615
feat: add `requireAdmin` optional param
Oct 22, 2023
2746a3e
fix: reduce alerts to improve user flow
Oct 22, 2023
3d0a6eb
build: update packages [docker build]
Oct 22, 2023
c648533
feat: configurable rate limiting
XWasHere Oct 25, 2023
6927bbd
Merge pull request #241 from XWasHere/dev/0.15.0
Oct 25, 2023
3709a69
chore: vsc autoformat
Oct 25, 2023
304f499
fix: use const & specify which config was invaldi
Oct 25, 2023
f276956
Merge branch 'master' into dev/0.15.0
Oct 25, 2023
5620d0b
add: database class interface
XWasHere Oct 25, 2023
ce3ad10
split json database out of data.ts
XWasHere Oct 25, 2023
66c8150
Merge branch 'tycrek:dev/0.15.0' into dev/0.15.0
XWasHere Oct 25, 2023
d36e5c5
nvm
XWasHere Oct 25, 2023
26431b2
fix: oh god oh fuck oh god oh no oh
XWasHere Oct 26, 2023
7bd08de
fix: oh god oh fuck oh god oh no oh
XWasHere Oct 26, 2023
a833f8c
fix this
XWasHere Oct 26, 2023
e8b70d1
Merge pull request #242 from XWasHere/crap
Oct 26, 2023
e113cb5
Merge remote-tracking branch 'upstream/dev/0.15.0' into dev/0.15.0
XWasHere Oct 26, 2023
9c9f2ac
impr: move sql config to "database", add config for different db vari…
XWasHere Oct 26, 2023
0d1172d
add: postgresql!
XWasHere Oct 26, 2023
b7ddc96
mayb this should be a number
XWasHere Oct 26, 2023
3c50946
semicolons
XWasHere Oct 26, 2023
e4602f9
Optimize docker build and update package
Gauvino Oct 26, 2023
d006073
Fix package.json
Gauvino Oct 26, 2023
966e1ce
Merge branch 'dev/0.15.0' into patch/docker
Gauvino Oct 26, 2023
c3d7e86
Superseded: #246
Gauvino Oct 27, 2023
a8fda88
Merge branch 'patch/docker' of https://github.com/Gauvino/ass into pa…
Gauvino Oct 27, 2023
bb2d11d
build: remove unused 0.14.x packages
Oct 27, 2023
1f7108f
build: update packages
Oct 27, 2023
30346a0
build/fix: use safer engines declaration
Oct 27, 2023
3a1481e
Merge branch 'dev/0.15.0' into patch/docker
Oct 27, 2023
b5e8993
Merge branch 'dev/0.15.0' into patch/docker
Gauvino Oct 27, 2023
0e2439a
Removed apk cache command
Gauvino Oct 27, 2023
52339f3
Rename and change content
Gauvino Oct 27, 2023
2fc1ddf
Merge pull request #244 from Gauvino/patch/docker
Oct 27, 2023
5aa636f
dummy for [docker build]
Oct 27, 2023
5bfe916
build: remove @types/tailwindcss (main package provides own types)
Oct 27, 2023
95a3fea
build/docker: use pnpm instead of npm
Oct 27, 2023
942c2c2
chore: use pnpm in workflow
Oct 27, 2023
da8494c
dummy for [docker build]
Oct 27, 2023
f2f44be
Merge branch 'dev/0.15.0' into x-dev-15
Oct 27, 2023
df5510d
fix: update pnpm lock
Oct 27, 2023
387fd5e
Merge pull request #1 from tycrek/x-dev-15
XWasHere Oct 28, 2023
fa322e4
Merge branch 'dev/0.15.0' of https://github.com/XWasHere/ass into dev…
XWasHere Oct 28, 2023
6e7ac81
chore: organize imports
Nov 2, 2023
c81541b
fix: only ensure dir on start; files can be done by JSON DB
Nov 2, 2023
ec97d0a
feat: inform host that DB has not been set
Nov 2, 2023
4457d32
fix: move semicolons
Nov 2, 2023
fd578d8
fix: remove duplicate sections
Nov 2, 2023
3e26d4d
fix: remove unused PATHS variable (moved to other file)
Nov 2, 2023
9083df9
chore: make consistent with "Checking data files"
Nov 2, 2023
21f9bef
feat: added helper dev container build script
Nov 2, 2023
173eaae
docs: update README
Nov 2, 2023
1ffcc02
docs: remove old readme sections
Nov 2, 2023
8dcb966
Merge pull request #2 from tycrek/x-dev-15
XWasHere Nov 2, 2023
41d683b
Merge branch 'dev/0.15.0' into dev/0.15.0
Nov 2, 2023
347b80e
Merge pull request #245 from XWasHere/dev/0.15.0
Nov 2, 2023
1fa55db
fix/ci: remove armv7 because it takes years [docker build]
Nov 2, 2023
efdb10b
fix: look for server.json in .ass-data dir [docker build]
Nov 4, 2023
c1d1470
Merge branch 'master' into dev/0.15.0
Nov 5, 2023
076f3b8
build: no longer use implicit script calls
Nov 5, 2023
35c9374
build: remove legacy scripts
Nov 5, 2023
afd0e2b
build: update packages
tycrek Nov 26, 2023
4c68159
Merge branch 'master' into dev/0.15.0
tycrek Nov 26, 2023
3687029
build: use `dist/` subdirs for output now
tycrek Nov 27, 2023
1f98b27
build: remove unnecessary `npm` engine
tycrek Nov 27, 2023
0c39391
build: switch to ESM mode
tycrek Nov 27, 2023
5eff78c
refactor: update all imports for ESM
tycrek Nov 27, 2023
2d4a003
build: fix `rm` command
tycrek Nov 27, 2023
1f3f898
build: add VitePress/vue
tycrek Dec 3, 2023
a61f3b4
chore: update .gitignore
tycrek Dec 3, 2023
a8cf170
build: forgot pnpm lockfile
tycrek Dec 3, 2023
f367b32
docs: configure VitePress
tycrek Dec 3, 2023
fec5f48
build: add VitePress scripts
tycrek Dec 3, 2023
010318e
docs: added index page (and vendor examples)
tycrek Dec 3, 2023
bf8a2a2
docs: add basic dir structure for install and customize
tycrek Dec 3, 2023
56219d0
Merge pull request #248 from tycrek/vitepress-docs
tycrek Dec 3, 2023
e83623d
docs: switch to dropdown menu
tycrek Dec 3, 2023
7126ad9
docs: update homepage title
tycrek Dec 3, 2023
7791af0
feat: add port to MySQL config
tycrek Dec 3, 2023
c8ee9a2
fix: log user create error locally
tycrek Dec 3, 2023
fb1c269
fix: use `hashtag` instead of `number`
tycrek Dec 3, 2023
11d6f30
fix: mysql not setting `_ready`
tycrek Dec 4, 2023
2a6e783
fix: missing error logs in api
tycrek Dec 4, 2023
778f257
todo
tycrek Dec 4, 2023
0abf3a2
fix: try to reconcile types
tycrek Dec 4, 2023
495bbcf
more todo
tycrek Dec 4, 2023
fa38f14
refactor: move these types to `types.d.ts`
tycrek Dec 4, 2023
c228a9f
fix: use simpler types & normalize within db implementations
tycrek Dec 4, 2023
7cd56bc
fix: accept missing on initial creation
tycrek Dec 4, 2023
dd050b6
fix: error handling
tycrek Dec 4, 2023
21095ed
MVP.231204.1 [docker build]
tycrek Dec 4, 2023
2d55579
docs: add Configure links
tycrek Dec 4, 2023
0821954
docs: add Aiven-specific MySQL instructions
tycrek Dec 4, 2023
7e15d29
docs: add logo and meta
tycrek Dec 4, 2023
5c5d20f
build: add wrangler dev dependency (for documentation)
tycrek Dec 4, 2023
8f326d5
build: renamed and rearranged npm scripts; added `dev:docs` script
tycrek Dec 4, 2023
e350ae3
build: remove `package-lock.json` (deploy via `pnpm`)
tycrek Dec 4, 2023
2545bac
build: synchronize pnpm lockfile
tycrek Dec 4, 2023
c19055c
build: update packages
tycrek Dec 4, 2023
1e84372
dummy to fix pages build with new script
tycrek Dec 4, 2023
e8169e7
build: small update
tycrek Dec 4, 2023
c3111d2
docs: added footer
tycrek Dec 4, 2023
343f0f1
docs: added edit link
tycrek Dec 4, 2023
5536478
docs: update template and description
tycrek Dec 4, 2023
c07cbad
docs: set branch
tycrek Dec 4, 2023
47627df
docs: update hero actions
tycrek Dec 4, 2023
504f29d
chore: add .wrangler/ to .gitignore
tycrek Dec 4, 2023
9205f18
refactor: rename old flameshot script
tycrek Dec 4, 2023
f4f5997
feat: migrate `check_tool`
tycrek Dec 4, 2023
770f78d
docs: mention future alternative install types
tycrek Dec 4, 2023
3e4bef7
docs: fix: incorrect markdown syntax
tycrek Dec 4, 2023
0befb0f
docs: add warning to install page
tycrek Dec 4, 2023
4b04368
docs: update wording for alternatives
tycrek Dec 4, 2023
e23133b
docs: added docker install notes
tycrek Dec 4, 2023
4727913
docs: added local install notes
tycrek Dec 4, 2023
1bb79c9
docs: migrate some data from PR
tycrek Dec 4, 2023
3028123
docs: remove dead links
tycrek Dec 4, 2023
123a397
docs: fix: incorrect url
tycrek Dec 5, 2023
9dce54d
build: add docs script to add compose redir
tycrek Dec 5, 2023
b472ed2
docs: use new docker deploy method
tycrek Dec 5, 2023
3f685ef
docs: fix: follow redirect
tycrek Dec 5, 2023
58fd55c
feat: discord webhook support + custom id func
wdhdev Dec 22, 2023
26c8e2f
chore: switch to axios
wdhdev Dec 22, 2023
ad37dca
chore: remove unused import
wdhdev Dec 22, 2023
2e3fe23
chore: single apostrophe
wdhdev Dec 22, 2023
ee32fb6
Update utils.ts
wdhdev Dec 22, 2023
f53bb10
feat: use william.js for discord validation
wdhdev Dec 22, 2023
f0dd773
Merge pull request #250 from WilliamDavidHarrison/dev/0.15.0
tycrek Dec 22, 2023
0b63fd6
build: update pnpm-lock.yaml (remove package-lock.json)
tycrek Dec 22, 2023
e026dfd
dummy for [docker build]
tycrek Dec 22, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 3 additions & 103 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,107 +1,9 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port
# build dirs
dist*/

# tokens
auth.json*
Expand All @@ -115,9 +17,7 @@ uploads/

# config
config.json

# certificates
*.crt
userconfig.json

# share/ directory
share/
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

1 change: 0 additions & 1 deletion ass-x
Submodule ass-x deleted from 2b1431
104 changes: 104 additions & 0 deletions backend/UserConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import fs from 'fs-extra';
import { path } from '@tycrek/joint';
import { UserConfiguration, UserConfigTypeChecker } from 'ass';
import { log } from './log';

/**
* Returns a boolean if the provided value is a number
*/
const numChecker = (val: any) => {
try { return !isNaN(parseInt(val)) && typeof val !== 'string'; }
catch (err) { return false; }
}

/**
* User-config property type checker functions
*/
const Checkers: UserConfigTypeChecker = {
uploadsDir: (val) => {
try { fs.accessSync(val); return true; }
Fixed Show fixed Hide fixed
catch (err) { return false; }
},
idType: (val) => {
const options = ['random', 'original', 'gfycat', 'timestamp', 'zws'];
return options.includes(val);
},
idSize: numChecker,
gfySize: numChecker,
maximumFileSize: numChecker,
}

export class UserConfig {
private static _config: UserConfiguration;
private static _ready = false;

public static get config() { return UserConfig._config; }
public static get ready() { return UserConfig._ready; }

constructor(config?: any) {
// Typically this would only happen during first-time setup (for now)
if (config != null) {
UserConfig._config = UserConfig.parseConfig(config);
UserConfig._ready = true;
}
}

/**
* Ensures that all config options are valid
*/
private static parseConfig(c: any) {
const config = (typeof c === 'string' ? JSON.parse(c) : c) as UserConfiguration;

if (!Checkers.uploadsDir(config.uploadsDir)) throw new Error(`Unable to access uploads directory: ${config.uploadsDir}`);
if (!Checkers.idType(config.idType)) throw new Error(`Invalid ID type: ${config.idType}`);
if (!Checkers.idSize(config.idSize)) throw new Error('Invalid ID size');
if (!Checkers.gfySize(config.gfySize)) throw new Error('Invalid Gfy size');
if (!Checkers.maximumFileSize(config.maximumFileSize)) throw new Error('Invalid maximum file size');

// All is fine, carry on!
return config;
}

/**
* Save the config file to disk
*/
public static saveConfigFile(): Promise<void> {
return new Promise(async (resolve, reject) => {
try {

// Only save is the config has been parsed
if (!UserConfig._ready) throw new Error('Config not ready to be saved!');

// Write to file
await fs.writeFile(path.join('userconfig.json'), JSON.stringify(UserConfig._config, null, '\t'));

resolve(void 0);
} catch (err) {
log.error('Failed to save config file!');
reject(err);
}
});
}

/**
* Reads the config file from disk
*/
public static readConfigFile(): Promise<void> {
return new Promise(async (resolve, reject) => {
try {

// Read the file data
const data = (await fs.readFile(path.join('userconfig.json'))).toString();

// Ensure the config is valid
UserConfig._config = UserConfig.parseConfig(data);
UserConfig._ready = true;

resolve(void 0);
} catch (err) {
log.error('Failed to read config file!');
reject(err);
}
});
}
}
111 changes: 111 additions & 0 deletions backend/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import express, { Request, Response, NextFunction, RequestHandler, json as BodyParserJson } from 'express';
import fs from 'fs-extra';
import { path, isProd } from '@tycrek/joint';
import { epcss } from '@tycrek/express-postcss';
import tailwindcss from 'tailwindcss';
import { log } from './log';
import { UserConfig } from './UserConfig';
import { ServerConfiguration } from 'ass';

/**
* Custom middleware to attach the ass object (and construct the `host` property)
*/
function assMetaMiddleware(port: number, proxied: boolean): RequestHandler {
return (req: Request, _res: Response, next: NextFunction) => {
req.ass = { host: `${req.protocol}://${req.hostname}${proxied ? '' : `:${port}`}` };
next();
}
}

/**
* Main function.
* Yes I'm using main() in TS, cry about it
*/
async function main() {

// Launch log
const pkg = await fs.readJson(path.join('package.json')) as { name: string, version: string };
log.blank().info(pkg.name, pkg.version).blank();

// Set default server configuration
const serverConfig: ServerConfiguration = {
host: '0.0.0.0',
port: 40115,
proxied: isProd()
};

// Replace with user details, if necessary
try {
const exists = await fs.pathExists(path.join('server.json'));
if (exists) {

// Read file
const { host, port, proxied } = await fs.readJson(path.join('server.json')) as { host?: string, port?: number, proxied?: boolean };

// Set details, if available
if (host) serverConfig.host = host;
if (port) serverConfig.port = port;
if (proxied != undefined) serverConfig.proxied = proxied;

log.debug('server.json', `${host ? `host=${host},` : ''}${port ? `port=${port},` : ''}${proxied != undefined ? `proxied=${proxied},` : ''}`);
}
} catch (err) {
log.error('Failed to read server.json');
console.error(err);
throw err;
}

// Attempt to load user configuration
await new Promise((resolve) => UserConfig.readConfigFile().then(() => resolve(void 0))
.catch((err) => (err.code && err.code === 'ENOENT' ? {} : console.error(err), resolve(void 0))));

Check warning

Code scanning / CodeQL

Expression has no effect Warning

This expression has no effect.

// Set up Express
const app = express();

app.enable('case sensitive routing');
app.disable('x-powered-by');

app.set('trust proxy', serverConfig.proxied);
app.set('view engine', 'pug');
app.set('views', 'views2/');

// Middleware
app.use(log.express());
app.use(BodyParserJson());
app.use(assMetaMiddleware(serverConfig.port, serverConfig.proxied));

// CSS
app.use('/.css', epcss({
cssPath: path.join('tailwind2.css'),
plugins: [
tailwindcss,
(await import('autoprefixer')).default(),
(await import('cssnano')).default(),
(await import('@tinycreek/postcss-font-magician')).default(),
],
warn: (warning: Error) => log.warn('PostCSS', warning.toString())
}));

app.get('/.ass.host', (req, res) => res.send(req.ass.host));

// Routing
app.use('/setup', (await import('./routers/setup')).router);
app.use('/', (await import('./routers/index')).router);

// Host app
app.listen(serverConfig.port, serverConfig.host, () => log[UserConfig.ready ? 'success' : 'warn']('Server listening', UserConfig.ready ? 'Ready for uploads' : 'Setup required', `click http://127.0.0.1:${serverConfig.port}`));
}

// Start program
main().catch((err) => (console.error(err), process.exit(1)));

// Exit tasks
['SIGINT', 'SIGTERM'].forEach((signal) => process.addListener(signal as any, () => {

// Hide ^C in console output
process.stdout.write('\r');

// Log then exit
log.info('Exiting', `received ${signal}`);
process.exit();
}));
2 changes: 2 additions & 0 deletions backend/generators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import cryptoRandomString from 'crypto-random-string';
export const random = ({ length }: { length: number }) => cryptoRandomString({ length, type: 'alphanumeric' });
2 changes: 2 additions & 0 deletions backend/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { TLog } from '@tycrek/log';
export const log = new TLog('debug');
Loading