Showing
18 changed files
with
218 additions
and
0 deletions
backend/.env.example
0 → 100644
backend/.gitignore
0 → 100644
1 | +# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
2 | + | ||
3 | +# dependencies | ||
4 | +/node_modules | ||
5 | +/.pnp | ||
6 | +.pnp.js | ||
7 | +.env | ||
8 | + | ||
9 | +# testing | ||
10 | +/coverage | ||
11 | + | ||
12 | +# production | ||
13 | +/build | ||
14 | + | ||
15 | +# misc | ||
16 | +.DS_Store | ||
17 | +.env.local | ||
18 | +.env.development.local | ||
19 | +.env.test.local | ||
20 | +.env.production.local | ||
21 | +.idea | ||
22 | + | ||
23 | +npm-debug.log* | ||
24 | +yarn-debug.log* | ||
25 | +yarn-error.log* |
backend/app.js
0 → 100644
1 | +let express = require('express') | ||
2 | +let cookieParser = require('cookie-parser') | ||
3 | +var cors = require('cors') | ||
4 | +var corsConfig = require('./config/cors') | ||
5 | + | ||
6 | +let indexRouter = require('./routes/index') | ||
7 | + | ||
8 | +let app = express() | ||
9 | +app.use(cors(corsConfig)) | ||
10 | + | ||
11 | +app.use(express.json()) | ||
12 | +app.use(express.urlencoded({ extended: false })) | ||
13 | +app.use(cookieParser()) | ||
14 | + | ||
15 | +app.use('/', indexRouter) | ||
16 | + | ||
17 | +app.use(function(req, res) { | ||
18 | + res.status(400) | ||
19 | + res.json({ | ||
20 | + success: false, | ||
21 | + message: 'error' | ||
22 | + }) | ||
23 | +}) | ||
24 | + | ||
25 | +app.use(function(err, req, res) { | ||
26 | + res.locals.message = err.message | ||
27 | + res.locals.error = req.app.get('env') === 'development' ? err : {} | ||
28 | + | ||
29 | + res.status(err.status || 500) | ||
30 | + res.json({ | ||
31 | + success: false | ||
32 | + }) | ||
33 | +}) | ||
34 | + | ||
35 | +module.exports = app |
backend/bin/www
0 → 100755
1 | +let app = require('../app') | ||
2 | +let debug = require('debug')('sw-fest-api:server') | ||
3 | +let http = require('http') | ||
4 | + | ||
5 | +let port = normalizePort(process.env.PORT || '3000') | ||
6 | +app.set('port', port) | ||
7 | + | ||
8 | +let server = http.createServer(app) | ||
9 | + | ||
10 | +server.listen(port) | ||
11 | +server.on('error', onError) | ||
12 | +server.on('listening', onListening) | ||
13 | + | ||
14 | +function normalizePort(val) { | ||
15 | + let port = parseInt(val, 10) | ||
16 | + | ||
17 | + if (isNaN(port)) { | ||
18 | + return val | ||
19 | + } | ||
20 | + | ||
21 | + if (port >= 0) { | ||
22 | + return port | ||
23 | + } | ||
24 | + | ||
25 | + return false | ||
26 | +} | ||
27 | + | ||
28 | +function onError(error) { | ||
29 | + if (error.syscall !== 'listen') { | ||
30 | + throw error | ||
31 | + } | ||
32 | + | ||
33 | + let bind = typeof port === 'string' | ||
34 | + ? 'Pipe ' + port | ||
35 | + : 'Port ' + port | ||
36 | + | ||
37 | + switch (error.code) { | ||
38 | + case 'EACCES': | ||
39 | + console.error(bind + ' requires elevated privileges') | ||
40 | + process.exitCode = 1 | ||
41 | + break | ||
42 | + case 'EADDRINUSE': | ||
43 | + console.error(bind + ' is already in use') | ||
44 | + process.exitCode = 1 | ||
45 | + break | ||
46 | + default: | ||
47 | + throw error | ||
48 | + } | ||
49 | +} | ||
50 | + | ||
51 | +function onListening() { | ||
52 | + let addr = server.address() | ||
53 | + let bind = typeof addr === 'string' | ||
54 | + ? 'pipe ' + addr | ||
55 | + : 'port ' + addr.port | ||
56 | + debug('Listening on ' + bind) | ||
57 | +} |
backend/config/.gitignore
0 → 100644
File mode changed
backend/config/config.js
0 → 100644
1 | +const path = require('path') | ||
2 | +require('dotenv').config({path: path.join(__dirname, "../.env")}) | ||
3 | + | ||
4 | +const username = process.env.MYSQL_USERNAME || 'root' | ||
5 | +const password = process.env.MYSQL_PASSWORD || null | ||
6 | +const database = process.env.MYSQL_DATABASE || 'root' | ||
7 | +const host = process.env.MYSQL_HOST || '127.0.0.1' | ||
8 | +const port = process.env.MYSQL_PORT || 3306 | ||
9 | + | ||
10 | +module.exports = { | ||
11 | + 'username': username, | ||
12 | + 'password': password, | ||
13 | + 'database': database, | ||
14 | + 'host': host, | ||
15 | + 'port': port, | ||
16 | + 'dialect': 'mysql', | ||
17 | + 'operatorsAliases': false | ||
18 | +} |
backend/config/cors.js
0 → 100644
backend/controllers/.gitignore
0 → 100644
File mode changed
backend/middlewares/.gitignore
0 → 100644
File mode changed
backend/migrations/.gitignore
0 → 100644
File mode changed
backend/models/.gitignore
0 → 100644
File mode changed
backend/models/index.js
0 → 100644
1 | +'use strict' | ||
2 | + | ||
3 | +const fs = require('fs') | ||
4 | +const path = require('path') | ||
5 | +const Sequelize = require('sequelize') | ||
6 | +const basename = path.basename(__filename) | ||
7 | +const config = require(__dirname + '/../config/config') | ||
8 | +const db = {} | ||
9 | + | ||
10 | +let sequelize | ||
11 | +if (config.use_env_variable) { | ||
12 | + sequelize = new Sequelize(process.env[config.use_env_variable], config) | ||
13 | +} else { | ||
14 | + sequelize = new Sequelize(config.database, config.username, config.password, config) | ||
15 | +} | ||
16 | + | ||
17 | +fs | ||
18 | + .readdirSync(__dirname) | ||
19 | + .filter(file => { | ||
20 | + return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js') | ||
21 | + }) | ||
22 | + .forEach(file => { | ||
23 | + const model = sequelize['import'](path.join(__dirname, file)) | ||
24 | + db[model.name] = model | ||
25 | + }) | ||
26 | + | ||
27 | +Object.keys(db).forEach(modelName => { | ||
28 | + if (db[modelName].associate) { | ||
29 | + db[modelName].associate(db) | ||
30 | + } | ||
31 | +}) | ||
32 | + | ||
33 | +db.sequelize = sequelize | ||
34 | +db.Sequelize = Sequelize | ||
35 | + | ||
36 | +module.exports = db |
backend/package-lock.json
0 → 100644
This diff is collapsed. Click to expand it.
backend/package.json
0 → 100644
1 | +{ | ||
2 | + "name": "sw-fest-api", | ||
3 | + "version": "0.0.1", | ||
4 | + "private": true, | ||
5 | + "scripts": { | ||
6 | + "start": "sequelize db:migrate && node ./bin/www --env-update" | ||
7 | + }, | ||
8 | + "dependencies": { | ||
9 | + "cookie-parser": "~1.4.4", | ||
10 | + "cors": "^2.8.5", | ||
11 | + "debug": "~2.6.9", | ||
12 | + "dotenv": "^8.2.0", | ||
13 | + "express": "~4.16.1", | ||
14 | + "mysql2": "^2.1.0", | ||
15 | + "sequelize": "^6.3.5" | ||
16 | + }, | ||
17 | + "devDependencies": { | ||
18 | + "eslint": "^6.8.0", | ||
19 | + "eslint-plugin-node": "^11.1.0", | ||
20 | + "sequelize-cli": "^6.2.0" | ||
21 | + } | ||
22 | +} |
backend/readme.md
0 → 100644
backend/routes/.gitignore
0 → 100644
File mode changed
backend/routes/index.js
0 → 100644
backend/utils/.gitignore
0 → 100644
File mode changed
-
Please register or login to post a comment