-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathapp.js
89 lines (72 loc) · 3.03 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
require('dotenv').config({ path: __dirname+'/.env' });
global.express = require("express");
global.app = express();
const {rd_client} = require('./app/adapters/database/redis');
//const {pg_client} = require('./app/adapters/database/postgresql');
const {mongo_client} = require('./app/adapters/database/mongodb');
const kafcli = require('./app/adapters/queue/kafka');
const { router } = require('./app/routes/routes');
const swagger = require('./app/libs/swagger/autogen');
const expressSwagger = require('express-swagger-generator')(app);
const jwt = require('jsonwebtoken');
//Global Variable
global.userIN = null;
app.use(express.json())
//app.set('view engine', 'ejs'); //FOR views Template engine
app.use(router);
app.get('/db', async (req, res) => {
const redisData = await rd_client.get('latestposts')
if (redisData) {
return res.json({source: 'redis', data: JSON.parse(redisData), leo: req.body})
}
const data = await pg_client.query('select * from posts')
await rd_client.set('latestposts', JSON.stringify(data.rows), {EX: 100,NX: true})
res.json({source: 'pg', data: data.rows, leo: req.body});
});
app.post('/ll', async (req, res) => {
let parseddata = jwtparser(req.header.AUTH);
//const data = await pg_client.query("SELECT id,user_title,user_name,email,phone FROM users WHERE user_name='leo' AND user_pass='123'")
const data = await pg_client.query("INSERT INTO (school_name,created_by) VALUES ('ted koleji', "+parseddata.user_id+")")
let jwtdata = {
user_id : 5,
user_title : "Leon Romano",
user_name : "leon",
email : "[email protected]",
phone: "4473508546246"
}
// PARSE -> parcalama
const token = jwt.sign(jwtdata, process.env.SECRET_KEY)
//const token = jwt.sign(data.rows, process.env.SECRET_KEY)
//const token = jwt.sign({names:'leo'}, process.env.SECRET_KEY)
res.json({source: 'pg', data: "no data", leo: "no req body", token: token});
});
//app.use("/docs", swagger.Swag_serve, swagger.Swag_setup);
let options = {
swaggerDefinition: {
info: {
description: "Node.js Express API with Swagger",
title: "Inavitas Örnek Proje",
version: '1.0.0',
},
host: 'localhost:3000',
basePath: '/',
produces: [
"application/json"
],
schemes: ['http', 'https'],
securityDefinitions: {
Bearer: {
description: 'Example value:- Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjU5MmQwMGJhNTJjYjJjM',
type: 'apiKey',
name: 'Authorization',
in: 'header'
}
},
security: [{Bearer: []}],
defaultSecurity: 'Bearer'
},
basedir: __dirname, //app absolute path
files: ['./app/controllers/**/*.js'] //Path to the API handle folder
};
module.exports = expressSwagger(options)
app.listen(process.env.APP_PORT, process.env.APP_HOST, () => console.log(`Server listening on http://${process.env.APP_HOST}:${process.env.APP_PORT}`));