-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_server.js
143 lines (123 loc) · 3.45 KB
/
test_server.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
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
const engine = require("./engine");
const keys = require("./examples/secure.json");
global.engine = require("./index.js");
require('dotenv').config();
main();
async function main(){
//---------------------------
//load rsa keys
//---------------------------
let loadKeys = engine.auth.loadKeys(keys.private,keys.public);
if(loadKeys instanceof engine.common.Error){
return loadKeys.now("failed-load_keys").log();
}
//---------------------------
//create token example
//---------------------------
let make_token = engine.auth.createToken({
session_id:engine.md5(engine.uniqid()),
channel_id:engine.md5("user")
});
if(make_token instanceof engine.common.Error){
return make_token.log();
}
let verify = engine.auth.verify(make_token);
if(verify instanceof engine.common.Error){
return verify.log();
}
//---------------------------
//add base dir for static file
//---------------------------
engine.server.app.base_dir(process.env.FILES_DIR);
//---------------------------
//add session auth function
//---------------------------
engine.auth.sessions.add_auth_function((session_id)=>{
return {
session_id:session_id,
uid:'some',
channel_id:engine.md5("user"),
};
});
//---------------------------
//add http request handler for non route requests
//---------------------------
engine.server.app.all((req,res)=>{
res.send("hello world");
});
//---------------------------
//websocket connection handlers
//---------------------------
//---------------------------
//add message handler
//---------------------------
engine.server.app.message((channel_id,message,isBinary)=>{
engine.server.send(channel_id,{reply:"akku"});
return true;
});
//---------------------------
//add wwebsocket connection open handler
//---------------------------
engine.server.app.add_ws_open_hanlder((ws)=>{
console.log("connection started");
});
//---------------------------
//add wwebsocket connection close handler
//---------------------------
engine.server.app.add_ws_close_handler((ws)=>{
console.log("connection exited");
});
//---------------------------
//send file
//---------------------------
engine.server.app.get("/file",(req,res)=>{
res.sendFile(process.env.FILES_DIR+"\\5g19f7.jpg");
});
//---------------------------
//login function
//---------------------------
engine.server.app.post("/apps/main/login",(req,res)=>{
let make_token = engine.auth.createToken({
channel_id:engine.md5("user"),
uid:"some",
});
res.json({
token:make_token
});
});
//---------------------------
//unprotected get api
//---------------------------
engine.server.app.get("/get",async (req,res)=>{
res.json({
hello:'get'
});
});
//---------------------------
//unprotected api
//---------------------------
engine.server.app.post("/apps/main/some",async (req,res)=>{
res.json({
some:true
});
});
//---------------------------
//protected route
//---------------------------
engine.server.app.post("/apps/main/some/protected",async (req,res)=>{
let verify = await engine.auth.verifyRequest(req);
if((verify instanceof engine.common.Error)){
return false;
}
res.json({
protected:true
});
});
//---------------------------
//start server
//---------------------------
engine.server.init({
port:8080,
cors:"*",
});
}