-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackend.js
More file actions
157 lines (141 loc) · 4.03 KB
/
backend.js
File metadata and controls
157 lines (141 loc) · 4.03 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const fastify = require('fastify')
const oas = require('fastify-oas')
const path = require('path')
const fsp = require('fs').promises;
const app = fastify();
const dataPath = path.resolve(__dirname, 'data');
const usersJsonPath = path.resolve(dataPath, 'users.json')
app.register(require('fastify-static'), {
root: path.join(__dirname, 'public'),
prefix: '/',
})
app.addHook('onRequest', (req, reply, next) => {
reply.header('Access-Control-Allow-Origin', '*');
reply.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
reply.header('Access-Control-Allow-Headers', 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range');
reply.header('Access-Control-Expose-Headers', 'Content-Length,Content-Range');
if (req.method === 'OPTIONS') {
reply.send('');
} else {
next();
}
});
app.register(oas, {
routePrefix: '/documentation',
exposeRoute: true,
swagger: {
info: {
title: 'Test openapi',
description: 'testing the fastify swagger api',
version: '0.1.0',
},
externalDocs: {
url: 'https://swagger.io',
description: 'Find more info here',
},
host: '127.0.0.1:9000',
schemes: ['http'],
consumes: ['application/json'],
produces: ['application/json'],
},
exposeRoute: true
});
app.route({
method: 'POST',
url: '/user/sign-up',
schema: {
body: {
type: 'object',
description: 'user sign up',
properties: {
username: {
type: 'string',
pattern: '[a-z][a-z0-9]{3,23}'
},
password: {
type: 'string',
pattern: '[a-z0-9]{6,}'
},
nationalID: {
type: 'string',
pattern: '[0-9]{10}'
},
active: {
type: 'boolean'
},
mobilePhone: {
type: 'string',
pattern: '09[0-9]{9}'
},
}
}
},
handler: async (req, reply) => {
let users = [];
try {
let d = JSON.parse((await fsp.readFile(usersJsonPath, { encoding: 'utf8' })));
users = d;
} catch (e) {}
users.push({
username: req.body.username,
password: req.body.password,
nationalID: req.body.nationalID,
mobilePhone: req.body.mobilePhone,
active: req.body.active,
});
await fsp.writeFile(usersJsonPath, JSON.stringify(users));
return true;
},
});
app.route({
method: 'POST',
url: '/user/sign-in',
schema: {
body: {
type: 'object',
description: 'user sign in',
properties: {
username: {
type: 'string',
pattern: '[a-z][a-z0-9]{3,23}'
},
password: {
type: 'string',
pattern: '[a-z0-9]{6,}'
},
}
}
},
handler: async (req, reply) => {
let users = [];
try {
let d = JSON.parse((await fsp.readFile(usersJsonPath, { encoding: 'utf8' })));
users = d;
} catch (e) {}
const u = users.filter((f) => {
if (f.username === req.body.username && f.password === req.body.password) {
return true;
}
return false;
});
if (u.length >= 1) {
return true;
}
return false;
},
});
app.route({
method: 'GET',
url: '/user/list',
schema: {
},
handler: async (req, reply) => {
let users = [];
try {
let d = JSON.parse((await fsp.readFile(usersJsonPath, { encoding: 'utf8' })));
users = d;
} catch (e) {}
return users;
},
});
app.listen(9000, '0.0.0.0');