create-sip 0.10.6 → 0.10.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/expressapi/app/controllers/authcontroller.js +6 -6
- package/expressapi/app/controllers/usercontroller.js +2 -2
- package/expressapi/app/database/database.js +3 -3
- package/expressapi/app/index.js +8 -6
- package/expressapi/app/middlewares/authjwt.js +6 -4
- package/expressapi/app/models/user.js +5 -4
- package/expressapi/app/routes/api.js +5 -5
- package/expressapi/package.json +3 -1
- package/expressapi/tools/genconf.js +12 -0
- package/expressapi/tools/genkey.js +10 -6
- package/manager.js +6 -1
- package/package.json +1 -1
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import bcrypt from 'bcryptjs'
|
|
2
|
+
import jwt from 'jsonwebtoken'
|
|
3
|
+
import User from '../models/user.js'
|
|
4
|
+
import config from '../../config/default.json' assert { type: 'json' }
|
|
5
|
+
|
|
6
6
|
const AuthController = {
|
|
7
7
|
async register(req, res) {
|
|
8
8
|
var clientError = false;
|
|
@@ -100,4 +100,4 @@ const AuthController = {
|
|
|
100
100
|
}
|
|
101
101
|
}
|
|
102
102
|
|
|
103
|
-
|
|
103
|
+
export default AuthController
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import Sequelize from 'sequelize'
|
|
2
|
+
import config from '../../config/default.json' assert { type: 'json' }
|
|
3
3
|
|
|
4
4
|
if(config.app.log != false) {
|
|
5
5
|
var log = console.log
|
|
@@ -19,4 +19,4 @@ const sequelize = new Sequelize(
|
|
|
19
19
|
}
|
|
20
20
|
)
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
export default sequelize
|
package/expressapi/app/index.js
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import express from 'express'
|
|
2
|
+
import morgan from 'morgan'
|
|
3
|
+
import cors from 'cors'
|
|
4
|
+
import fs from 'fs'
|
|
5
|
+
import router from './routes/api.js'
|
|
6
|
+
import config from '../config/default.json' assert { type: 'json' }
|
|
7
|
+
|
|
3
8
|
const app = express()
|
|
4
|
-
const fs = require('fs')
|
|
5
|
-
const router = require('./routes/api')
|
|
6
|
-
const config = require('../config/default.json')
|
|
7
9
|
|
|
8
10
|
const PORT = config.app.port || 8000
|
|
9
11
|
|
|
10
12
|
const logfile = 'access.log'
|
|
11
13
|
var accessLogStream = fs.createWriteStream(logfile, { flags: 'a' })
|
|
12
14
|
app.use(morgan('dev', { stream: accessLogStream }))
|
|
13
|
-
|
|
15
|
+
app.use(cors())
|
|
14
16
|
app.use(express.json())
|
|
15
17
|
app.use('/api', router);
|
|
16
18
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import jwt from 'jsonwebtoken';
|
|
2
|
+
import config from '../../config/default.json' assert { type: 'json' };
|
|
3
|
+
|
|
4
|
+
const verifyToken = (req, res, next) => {
|
|
5
5
|
let authData = req.headers.authorization;
|
|
6
6
|
if(!authData) {
|
|
7
7
|
return res.status(403).send({
|
|
@@ -20,3 +20,5 @@ exports.verifyToken = (req, res, next) => {
|
|
|
20
20
|
next()
|
|
21
21
|
})
|
|
22
22
|
};
|
|
23
|
+
|
|
24
|
+
export default verifyToken
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { DataTypes } from 'sequelize'
|
|
2
|
+
import sequelize from '../database/database.js'
|
|
3
|
+
|
|
4
4
|
const User = sequelize.define('User', {
|
|
5
5
|
id: {
|
|
6
6
|
type: DataTypes.INTEGER,
|
|
@@ -16,4 +16,5 @@ const User = sequelize.define('User', {
|
|
|
16
16
|
sequelize.sync({
|
|
17
17
|
force: false
|
|
18
18
|
})
|
|
19
|
-
|
|
19
|
+
|
|
20
|
+
export default User
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
|
|
1
|
+
import Router from 'express'
|
|
2
2
|
const router = Router()
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
import AuthController from '../controllers/authcontroller.js';
|
|
5
|
+
import UserController from '../controllers/usercontroller.js';
|
|
6
|
+
import verifyToken from '../middlewares/authjwt.js';
|
|
7
7
|
|
|
8
8
|
router.post('/register', AuthController.register)
|
|
9
9
|
router.post('/login', AuthController.login)
|
|
10
10
|
router.get('/users', [verifyToken], UserController.index)
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
export default router
|
package/expressapi/package.json
CHANGED
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
"license": "ISC",
|
|
17
17
|
"dependencies": {
|
|
18
18
|
"bcryptjs": "^2.4.3",
|
|
19
|
+
"cors": "^2.8.5",
|
|
19
20
|
"express": "^4.18.2",
|
|
20
21
|
"generate-api-key": "^1.0.2",
|
|
21
22
|
"jsonwebtoken": "^9.0.0",
|
|
@@ -28,5 +29,6 @@
|
|
|
28
29
|
"devDependencies": {
|
|
29
30
|
"nodemon": "^2.0.22",
|
|
30
31
|
"supertest": "^6.3.3"
|
|
31
|
-
}
|
|
32
|
+
},
|
|
33
|
+
"type": "module"
|
|
32
34
|
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import fs from 'fs/promises'
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import url from 'url';
|
|
4
|
+
|
|
5
|
+
const __filename = url.fileURLToPath(import.meta.url);
|
|
6
|
+
const __dirname = path.dirname(__filename);
|
|
7
|
+
const sourceFileName = path.join(__dirname, '../config/default.json.example');
|
|
8
|
+
const destinationFileName = path.join(__dirname, '../config/default.json');
|
|
9
|
+
|
|
10
|
+
fs.copyFile(sourceFileName, destinationFileName)
|
|
11
|
+
.catch(error => console.log(error));
|
|
12
|
+
|
|
@@ -1,19 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import fs from 'fs/promises'
|
|
2
|
+
import { generateApiKey } from 'generate-api-key'
|
|
3
|
+
import path, { dirname } from 'path';
|
|
4
|
+
import url from 'url';
|
|
5
|
+
|
|
6
|
+
const __filename = url.fileURLToPath(import.meta.url);
|
|
7
|
+
const __dirname = dirname(__filename);
|
|
8
|
+
const fileName = path.join(__dirname, '../config/default.json');
|
|
5
9
|
|
|
6
10
|
function generateKey(size = 32, format = 'base64') {
|
|
7
11
|
const buffer = crypto.randomBytes(size);
|
|
8
12
|
return buffer.toString(format);
|
|
9
13
|
}
|
|
10
14
|
|
|
11
|
-
fs.readFile(
|
|
15
|
+
fs.readFile(fileName)
|
|
12
16
|
.then(body => JSON.parse(body))
|
|
13
17
|
.then(json => {
|
|
14
18
|
json.app.key = generateApiKey({method: 'bytes', length: 32})
|
|
15
19
|
return json
|
|
16
20
|
})
|
|
17
21
|
.then(json => JSON.stringify(json, null, 4))
|
|
18
|
-
.then(body => fs.writeFile(
|
|
22
|
+
.then(body => fs.writeFile(fileName, body, 'utf8'))
|
|
19
23
|
.catch(error => console.log(error))
|
package/manager.js
CHANGED
|
@@ -60,8 +60,13 @@ const genExpressApi = (target) => {
|
|
|
60
60
|
copyDir(`${dir}/expressapi`, target);
|
|
61
61
|
updatePackageName(`${target}/package.json`, target);
|
|
62
62
|
console.log('ExpressJS REST API sablon created');
|
|
63
|
-
console.log('Make a config/default.json');
|
|
64
63
|
console.log('Read docs/user_doc.md');
|
|
64
|
+
console.log('Run next commands:');
|
|
65
|
+
console.log(` cd ${target}`);
|
|
66
|
+
console.log(' npm install');
|
|
67
|
+
console.log(' node tools/genconf.js');
|
|
68
|
+
console.log(' node tools/genkey.js');
|
|
69
|
+
console.log(' npm run dev');
|
|
65
70
|
}
|
|
66
71
|
|
|
67
72
|
module.exports = {
|