Skip to content

Commit 3f8891a

Browse files
author
David Street
committed
Update for sevr 0.2.0
1 parent a6a2f36 commit 3f8891a

File tree

7 files changed

+188
-130
lines changed

7 files changed

+188
-130
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# Sevr Tutorial
2-
---
32

43
These are the source files used for the Sevr blog tutorial.
54

5+
## [View Tutorial](docs)
6+
67
## Running
78

89
```

docs/2_installation.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ You should now have a directory structure that looks something like this:
5454
└── types
5555
```
5656

57-
You can now run your application for the first time:
57+
You can now run your application for the first time (though it won't do much of anything):
5858

5959
```
60-
$ npm start
60+
$ sevr start
6161
```
6262
---
6363

docs/5_cli.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ managing the application's collections. The service starts with the application,
1212
and begins listening to incoming connections.
1313

1414
```
15-
$ npm start
15+
$ sevr start
1616
```
1717

1818
Next, if we open a new terminal, we can run the `manage` command within the CLI.

docs/6_consuming_data.md

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,51 +24,69 @@ Within the `index.js`, add the following code:
2424

2525
const express = require('express')
2626

27-
module.exports = (sevr, _config) => {
28-
const app = express()
27+
class WebServer {
28+
constructor(sevr) {
29+
const app = express()
2930

30-
app.get('/', (req, res) => { res.send('Welcome to the Sevr Blog!') })
31+
app.get('/', (req, res) => {
32+
res.send('Welcome to the Sevr Blog!')
33+
})
3134

32-
// Wait for the DB to be connected
33-
sevr.events.on('db-ready', () => {
35+
this.sevr = sevr
36+
this.app = app
37+
}
3438

35-
// Attach the web app to the sevr server
36-
sevr.server.use(app)
37-
})
39+
willRun() {
40+
// Attach the web app to the Sevr server
41+
this.sevr.server.use(this.app)
42+
}
3843
}
3944
```
4045

4146
We now need to register our plugin with Sevr. We can do this by calling
4247
Sevr's `attach` method in our root `index.js`:
4348

4449
```javascript
45-
const Sevr = require('sevr')
46-
const cli = require('sevr-cli')
47-
const config = require('./config')
48-
const web = require('./web')
49-
50-
const sevr = new Sevr(config)
50+
const Sevr = require('sevr')
51+
const SevrCli = require('sevr-cli')
52+
const WebServer = require('./web')
53+
54+
/**
55+
* Application plugin class
56+
*
57+
* All custom application intialization and logic
58+
* should happen within this class
59+
*
60+
* @class App
61+
*/
62+
class App {
63+
constructor(sevr) {
64+
this.sevr = sevr
65+
}
66+
67+
run() {
68+
this.sevr.startServer()
69+
this.sevr.logger.verbose('Application running...')
70+
}
71+
}
5172

52-
sevr.attach(cli)
73+
// Create a new Sevr instance
74+
const sevr = new Sevr()
5375

54-
// Attach the frontend web plugin
55-
sevr.attach(web)
76+
// Attach the remote CLI plugin
77+
sevr.attach(SevrCli)
5678

57-
sevr.connect()
58-
.then(() => {
59-
sevr.logger.verbose('Initialized database connection')
60-
})
61-
.catch(err => {
62-
sevr.logger.error(err)
63-
})
79+
// Attach the web server
80+
sevr.attach(WebServer)
6481

65-
sevr.startServer()
82+
// Attach the application plugin
83+
sevr.attach(App)
6684

6785
module.exports = sevr
6886
```
6987

7088
We can now run our application and test that the plugin initialized correctly
71-
and attached to the Sevr server by running `npm start` and visiting
89+
and attached to the Sevr server by running `sevr start` and visiting
7290
`http://localhost:3000/` in a browser. You should see message
7391
'Welcome to the Sevr Blog!'
7492

@@ -132,13 +150,13 @@ module.exports = ({ site, posts }) => `
132150
Next we need to import the template file in our plugin file. Add the following
133151
line:
134152

135-
```
153+
```javascript
136154
const template = require('./templates/index.html')
137155
```
138156

139157
## Add Routes
140158

141-
Lastly, we need to add a few routes to our plugin file in order render the pages:
159+
Lastly, we need to add a few routes to our plugin constructor in order render the pages:
142160

143161
```javascript
144162
// Home route

docs/7_authentication_roles.md

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,47 @@ purposes, we'll use the `users` collection.
2121
fields.
2222

2323
```javascript
24-
const Sevr = require('sevr')
25-
const cli = require('sevr-cli')
26-
const config = require('./config')
27-
const web = require('./web')
24+
const Sevr = require('sevr')
25+
const SevrCli = require('sevr-cli')
26+
const WebServer = require('./web')
27+
28+
/**
29+
* Application plugin class
30+
*
31+
* All custom application intialization and logic
32+
* should happen within this class
33+
*
34+
* @class App
35+
*/
36+
class App {
37+
constructor(sevr) {
38+
this.sevr = sevr
39+
}
2840

29-
const sevr = new Sevr(config)
41+
willRun() {
42+
this.sevr.logger.info('Enabling authentication...')
43+
return this.sevr.authentication.enable(this.sevr.collections.users)
44+
}
45+
46+
run() {
47+
this.sevr.startServer()
48+
this.sevr.logger.verbose('Application running...')
49+
}
50+
}
3051

31-
sevr.attach(cli)
52+
// Create a new Sevr instance
53+
const sevr = new Sevr()
3254

33-
// Attach the frontend web plugin
34-
sevr.attach(web)
55+
// Attach the remote CLI plugin
56+
sevr.attach(SevrCli)
3557

36-
sevr.connect()
37-
.then(() => {
38-
sevr.logger.verbose('Initialized database connection')
39-
sevr.logger.verbose('Enabling authentication...')
58+
// Attach the web server
59+
sevr.attach(WebServer)
4060

41-
// Enable authentication with the `users` collection
42-
sevr.authentication.enable(sevr.collections.users)
43-
})
44-
.catch(err => {
45-
sevr.logger.error(err)
46-
})
61+
// Attach the application plugin
62+
sevr.attach(App)
4763

48-
sevr.startServer()
64+
sevr.start()
4965

5066
module.exports = sevr
5167
```
@@ -93,21 +109,24 @@ Now that we have our roles defined, we can enable them and attach them to the
93109
authentication collection.
94110

95111
```javascript
96-
const Sevr = require('sevr')
97-
const cli = require('sevr-cli')
98-
const perm = require('sevr-perm')
99-
const config = require('./config')
100-
const web = require('./web')
112+
const Sevr = require('sevr')
113+
const SevrCli = require('sevr-cli')
114+
const SevrPerm = require('sevr-perm)
115+
const WebServer = require('./web')
116+
const config = require('./config)
101117

102-
const sevr = new Sevr(config)
118+
...
103119

104-
sevr.attach(cli)
120+
const sevr = new Sevr(config)
105121

106-
// Attach the frontend web plugin
107-
sevr.attach(web)
122+
...
108123

109124
// Attach the permissions plugin
110-
sevr.attach(perm, config.permissions)
125+
sevr.attach(SevrPerm, config.permissions)
126+
127+
// Attach the application plugin
128+
sevr.attach(App)
129+
111130
...
112131
```
113132

index.js

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,48 @@
1-
const Sevr = require('sevr')
2-
const cli = require('sevr-cli')
3-
const perm = require('sevr-perm')
4-
5-
const config = require('./config')
6-
const web = require('./web')
7-
1+
const Sevr = require('sevr')
2+
const SevrCli = require('sevr-cli')
3+
const SevrPerm = require('sevr-perm')
4+
const WebServer = require('./web')
5+
const config = require('./config')
6+
7+
/**
8+
* Application plugin class
9+
*
10+
* All custom application intialization and logic
11+
* should happen within this class
12+
*
13+
* @class App
14+
*/
15+
class App {
16+
constructor(sevr) {
17+
this.sevr = sevr
18+
}
19+
20+
willRun() {
21+
this.sevr.logger.info('Enabling authentication...')
22+
return this.sevr.authentication.enable(this.sevr.collections.users)
23+
}
24+
25+
run() {
26+
this.sevr.startServer()
27+
this.sevr.logger.verbose('Application running...')
28+
}
29+
}
30+
31+
// Create a new Sevr instance
832
const sevr = new Sevr(config)
933

10-
sevr.attach(cli)
34+
// Attach the remote CLI plugin
35+
sevr.attach(SevrCli)
1136

12-
// Attach the frontend web plugin
13-
sevr.attach(web)
37+
// Attach the web server
38+
sevr.attach(WebServer)
1439

1540
// Attach the permissions plugin
16-
sevr.attach(perm, config.permissions)
17-
18-
sevr.connect()
19-
.then(() => {
20-
sevr.logger.verbose('Initialized database connection')
21-
sevr.logger.verbose('Enabling authentication...')
41+
sevr.attach(SevrPerm, config.permissions)
2242

23-
// Enable authentication with the `users` collection
24-
sevr.authentication.enable(sevr.collections.users)
25-
})
26-
.catch(err => {
27-
sevr.logger.error(err.stack)
28-
})
43+
// Attach the application plugin
44+
sevr.attach(App)
2945

30-
sevr.startServer()
46+
sevr.start()
3147

3248
module.exports = sevr

0 commit comments

Comments
 (0)