@radatek/microserver 2.2.0 → 2.2.2

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/readme.md DELETED
@@ -1,139 +0,0 @@
1
- ## HTTP MicroServer
2
-
3
- Lightweight all-in-one http web server without dependencies
4
-
5
- Features:
6
- - fast REST API router
7
- - form/json body decoder
8
- - file upload
9
- - websockets
10
- - authentication
11
- - plain/hashed passwords
12
- - virtual hosts
13
- - static files
14
- - rewrite
15
- - redirect
16
- - reverse proxy routes
17
- - trust ip for reverse proxy
18
- - json file storage with autosave
19
- - tls with automatic certificate reload
20
- - data model with validation and mongodb interface
21
- - simple file/memory storage
22
- - promises as middleware
23
- - controller class
24
- - access rights per route
25
- - access rights per model field
26
-
27
- ### Usage examples:
28
-
29
- Simple router:
30
-
31
- ```ts
32
- import { MicroServer, AccessDenied } from '@radatek/microserver'
33
-
34
- const server = new MicroServer({
35
- listen: 8080,
36
- auth: {
37
- users: {
38
- usr: {
39
- password: 'secret'
40
- }
41
- }
42
- }
43
- })
44
- server.use('GET /api/hello/:id',
45
- (req: ServerRequset, res: ServerResponse) =>
46
- ({message:'Hello ' + req.params.id + '!'}))
47
- server.use('POST /api/login',
48
- (req: ServerRequset, res: ServerResponse) =>
49
- {
50
- const user = await req.auth.login(req.body.user, req.body.password)
51
- return user ? {user} : new AccessDenied()
52
- })
53
- server.use('GET /api/protected', 'acl:auth',
54
- (req: ServerRequset, res: ServerResponse) =>
55
- ({message:'Secret resource'}))
56
- server.use('static', {root:'public'})
57
- ```
58
-
59
- Using data schema:
60
-
61
- ```js
62
- import { MicroServer, Model, MicroCollection, FileStore } from '@radatek/microserver'
63
-
64
- const usersCollection = new MicroCollection({ store: new FileStore({ dir: 'data' }), name: 'users' })
65
- // or using MicroDB collection
66
- const usersCollection = await db.collection('users')
67
-
68
- const userProfile = new Model({
69
- _id: 'string',
70
- name: { type: 'string', required: true },
71
- email: { type: 'string', format: 'email' },
72
- password: { type: 'string', canRead: false },
73
- role: { type: 'string' },
74
- acl: { type: 'object' },
75
- }, { collection: usersCollection, name: 'user' })
76
-
77
- const server = new MicroServer({
78
- listen:8080,
79
- auth: {
80
- users: (user, password) => userProfile.get(password ? {_id: user, password } : {_id: user})
81
- }
82
- })
83
-
84
- await userProfile.insert({name: 'admin', password: 'secret', role: 'admin', acl: {'user/*': true}})
85
-
86
- server.use('POST /login', async (req) => {
87
- const user = await req.auth.login(req.body.user, req.body.password)
88
- return user ? { user } : 403
89
- })
90
- // authenticated user allways has auth access
91
- server.use('GET /profile', 'acl:auth', req => ({ user: req.user }))
92
- // get all users if role='admin'
93
- server.use('GET /admin/users', 'role:admin', userProfile)
94
- // get user by id if has acl 'user/get'
95
- server.use('GET /admin/user/:id', 'acl:user/get', userProfile)
96
- // insert new user if role='admin' and has acl 'user/insert'
97
- server.use('POST /admin/user', 'role:admin', 'acl:user/insert', userProfile)
98
- // update user if has acl 'user/update'
99
- server.use('PUT /admin/user/:id', 'acl:user/update', userProfile)
100
- // delete user if has acl 'user/update'
101
- server.use('DELETE /admin/user/:id', 'acl:user/delete', userProfile)
102
- ```
103
-
104
- Using controller:
105
-
106
- ```ts
107
- const server = new MicroServer({
108
- listen: 8080,
109
- auth: {
110
- users: {
111
- usr: {
112
- password: 'secret',
113
- acl: {user: true}
114
- }
115
- }
116
- }
117
- })
118
-
119
- class RestApi extends Controller {
120
- static acl = '' // default acl
121
-
122
- gethello(id) {
123
- return {message:'Hello ' + id + '!'}
124
- }
125
-
126
- async postlogin() {
127
- const user = await this.auth.login(this.body.user, this.body.password)
128
- return user ? {user} : 403
129
- }
130
-
131
- static 'acl:protected' = 'user'
132
- static 'url:protected' = 'GET /protected'
133
- protected() {
134
- return {message:'Protected'}
135
- }
136
- }
137
-
138
- server.use('/api', RestApi)
139
- ```