Skip to content

Commit ec29deb

Browse files
committed
Replace scaffolding and add some docs
This change replaces the use of core/scaffolding-node with a standard plan with comments and docs links. It also pares back the Node app itself to remove superfluous generated code. Signed-off-by: Christian Nunciato <[email protected]>
1 parent 006ed2b commit ec29deb

17 files changed

+955
-223
lines changed

README.md

+49-52
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,90 @@
11
# Habitat Sample Node App
22

3-
Welcome to the Habitat Sample Node App!
3+
This simple [Express](https://expressjs.com/) app is an example of how to package a Node.js application with Habitat. It's intended to accompany the walkthroughs on the Habitat website, but it can also be run locally on its own.
44

5-
### Follow the demo instructions
5+
### Follow the Demos!
66

7-
For the full experience, check out the [Habitat Demo instructions](https://www.habitat.sh/demo/) on the habitat.sh website. This will walk you through getting setup on the Habitat Builder web app and publishing this sample app to Docker Hub.
7+
For the full experience, we recommend [following the demos](https://www.habitat.sh/learn/) on the Habitat website. They walk you through getting set up with Habitat, [Builder](https://bldr.habitat.sh/), automated builds and publishing to Docker Hub.
88

9-
Alternatively, you can use the instructions below if you simply want to build and run the app locally.
9+
Alternatively, you can follow the instructions below if you simply want to build and run the app locally.
1010

1111
![habitat-sample-node-app](https://user-images.githubusercontent.com/274700/39158589-d1170792-4715-11e8-8e2a-1a2696944500.png)
1212

13+
### Prerequisites
1314

14-
## Instructions
15+
To package and run this application with Habitat, you'll need to:
1516

16-
To practice packaging/running this app with Habitat
17+
* [Install and configure Habitat](https://www.habitat.sh/docs/install-habitat/)
18+
* [Install Docker](https://www.docker.com/community-edition) (if you're on Mac or Windows)
19+
* Clone this repository:
20+
21+
$ git clone https://github.com/habitat-sh/sample-node-app.git
22+
$ cd sample-node-app
1723

18-
### Workstation Prerequisites
19-
* Install and set up Habitat [(Instructions here)](https://www.habitat.sh/tutorials/download/)
20-
* Install Docker [(Instructions here)](https://www.docker.com/community-edition)
21-
* Clone this repository
22-
```bash
23-
$ git clone https://github.com/habitat-sh/sample-node-app.git
24-
```
25-
* Change directories
26-
```bash
27-
$ cd sample-node-app
28-
```
2924

3025
### Setup for First-time Users
26+
3127
Before you can build the app, you'll need to create an origin and accompanying keys.
32-
The quickest way to do this is by running `hab setup` and following the prompts.
28+
The quickest way to do this is by running `hab setup` [as described in the Habitat docs](https://www.habitat.sh/docs/install-habitat/#configure-workstation) and following the prompts:
3329

34-
```bash
30+
```
3531
$ hab setup
3632
```
3733

38-
**Note** - the origin name you enter during setup will need to be added to the plan.sh file mentioned in the next section.
34+
**Note**: The origin name you use during setup will need to be specified in the plan.sh file mentioned in the next section.
3935

4036
### Building the Package
41-
From the `/habitat` directory in this repo, open the habitat/plan.sh file:
42-
43-
Your habitat/plan.sh should look like this:
44-
```sh
45-
pkg_name=sample-node-app
46-
pkg_origin=your_origin
47-
pkg_scaffolding="core/scaffolding-node"
48-
```
49-
First, change the value of `your_origin` to the origin name your created in the previous section.
5037

51-
If you're following the [demo instructions](https://www.habitat.sh/demo/), then use the origin name you created in the Builder web app.
38+
From the `habitat` directory in this repository, open the `plan.sh` file. It should look like this:
5239

53-
Next, let's add in a version number
54-
```sh
55-
pkg_name=sample-node-app
40+
```
5641
pkg_origin=your_origin
57-
pkg_scaffolding="core/scaffolding-node"
58-
pkg_version="1.0.1"
42+
pkg_name=sample-node-app
43+
pkg_version="1.1.0"
44+
pkg_deps=(core/node)
45+
...
5946
```
47+
First, change the value of `pkg_origin` from `your_origin` to the origin you created in the previous section. If you're following the [demo](https://www.habitat.sh/learn/), use the origin you created in [Habitat Builder](http://bldr.habitat.sh/).
6048

61-
Now save and close the file.
49+
Next, let's change the version number:
50+
```
51+
...
52+
pkg_version="1.1.1"
53+
...
54+
```
6255

63-
Enter the Habitat Studio
56+
Save and close the file, then enter the Habitat Studio:
6457

65-
```bash
58+
```
6659
$ hab studio enter
6760
```
6861

69-
And run build
62+
And run a build:
7063

71-
```bash
72-
(studio) $ build
7364
```
65+
[1][default:/src:0]# build
66+
```
67+
68+
Habitat will produce a package (a `.hart` file) and place it in the `results` directory.
7469

7570
### Running the Package with Docker
7671

77-
Still in your studio, right after the build, export that package to a docker image
78-
```bash
79-
(studio) $ hab pkg export docker ./results/<habitat artifact>.hart
80-
```
72+
Still in the Studio, right after the build, export that package as a Docker image:
8173

82-
Then exit out of the studio:
83-
```bash
84-
(studio) $ exit
74+
```
75+
[2][default:/src:0]# source results/last_build.env
76+
[3][default:/src:0]# hab pkg export docker results/$pkg_artifact
8577
```
8678

87-
Now start a Docker container from that image.
79+
Then exit the Studio:
8880

89-
```bash
90-
$ docker run -it -p 8000:8000 your_origin/sample-node-app
81+
```
82+
[4][default:/src:0]# exit
83+
```
84+
And start a Docker container with your newly created image:
85+
86+
```
87+
$ docker run -it -p 3000:3000 <YOUR_ORIGIN>/sample-node-app
9188
```
9289

93-
Now head to http://localhost:8000 and see your running app!
90+
Now head to http://localhost:3000 and see your running app!

app.js

-35
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,11 @@
11
var express = require('express');
22
var path = require('path');
3-
var favicon = require('serve-favicon');
4-
var logger = require('morgan');
5-
var cookieParser = require('cookie-parser');
6-
var bodyParser = require('body-parser');
7-
83
var index = require('./routes/index');
9-
var users = require('./routes/users');
104

115
var app = express();
12-
13-
// view engine setup
146
app.set('views', path.join(__dirname, 'views'));
157
app.set('view engine', 'jade');
16-
17-
// uncomment after placing your favicon in /public
18-
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
19-
app.use(logger('dev'));
20-
app.use(bodyParser.json());
21-
app.use(bodyParser.urlencoded({ extended: false }));
22-
app.use(cookieParser());
238
app.use(express.static(path.join(__dirname, 'public')));
24-
259
app.use('/', index);
26-
app.use('/users', users);
27-
28-
// catch 404 and forward to error handler
29-
app.use(function(req, res, next) {
30-
var err = new Error('Not Found');
31-
err.status = 404;
32-
next(err);
33-
});
34-
35-
// error handler
36-
app.use(function(err, req, res, next) {
37-
// set locals, only providing error in development
38-
res.locals.message = err.message;
39-
res.locals.error = req.app.get('env') === 'development' ? err : {};
40-
41-
// render the error page
42-
res.status(err.status || 500);
43-
res.render('error');
44-
});
4510

4611
module.exports = app;

bin/www

-90
This file was deleted.

default_config.json

-4
This file was deleted.

dev-config.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"title": "Habitat - The fastest path from code to cloud native.",
3+
"message": "Welcome to the Habitat Node.js sample app."
4+
}

habitat/config/config.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"message": "You are running version {{pkg.version}} of the Habitat Node.js sample app."
2+
"title": "{{ cfg.title }}",
3+
"message": "You are running version {{ pkg.version }} of the Habitat Node.js sample app."
34
}

habitat/default.toml

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
# Use this file to templatize your application's native configuration files.
2-
# See the docs at https://www.habitat.sh/docs/create-packages-configure/.
3-
# You can safely delete this file if you don't need it.
1+
# When Habitat starts this service, it'll use the values we define here
2+
# to render the templates we define in our plan's `config` directory.
3+
#
4+
# To learn more about Habitat's configuration features, see the docs at
5+
# https://www.habitat.sh/docs/developing-packages/#add-configuration.
46

5-
# Message of the Day
6-
message = "Hello, World!"
7+
title = "Habitat - The fastest path from code to cloud native."

habitat/hooks/run

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/sh
2+
3+
# Habitat uses "hooks" to manage the application lifecycle. The run hook
4+
# is used for starting (and restarting) a service. Here, we launch the Node
5+
# app in the usual way, passing the path to the Habitat-managed config file
6+
# as a command-line argument.
7+
#
8+
# To learn more about the run hook, its conventions and other available lifecycle
9+
# hooks, see the docs at https://www.habitat.sh/docs/reference/#available-hooks.
10+
11+
exec node \
12+
{{ pkg.path }}/app/index.js \
13+
{{ pkg.svc_config_path }}/config.json \
14+
2>&1

habitat/plan.sh

+51-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,55 @@
1+
# The plan file tells Habitat how to build a package.
2+
#
3+
# In this plan, we're asking Habitat to provide us with Node.js and NPM
4+
# (by declaring a dependency on the core/node package) so we can install our
5+
# application's JavaScript dependencies (and ultimately run our app). Then we
6+
# copy the files we'll need to run the package into a directory in the Habitat
7+
# Studio that will become the resulting package.
8+
#
9+
# To learn more about writing Habitat plans, see Developing Packages
10+
# in the Habitat docs at https://www.habitat.sh/docs/developing-packages.
11+
#
12+
# To explore all Habitat-maintained and community-contributed packages,
13+
# visit the Habitat Builder depot at https://bldr.habitat.sh/#/pkgs.
14+
115
pkg_name=sample-node-app
216
pkg_origin=your_origin
3-
pkg_scaffolding="core/scaffolding-node"
4-
pkg_version="1.0.1"
17+
pkg_version="1.1.0"
18+
pkg_deps=(core/node)
19+
20+
# Habitat provides you with a number of built-in "callbacks" to use
21+
# in the course of your build, all of which are explained in the docs
22+
# at https://habitat.sh/docs/reference/#reference-callbacks.
23+
#
24+
# Here, we're implementing the do_build and do_install callbacks
25+
# to install dependencies and assemble the application package.
26+
27+
do_build() {
28+
29+
# By default, we're in the directory in which the Studio was entered
30+
# (in this case, presumably the project root), so we can run commands
31+
# as though we were in that same directory. By the time we reach this
32+
# callback, `npm` will have been installed for us.
33+
npm install
34+
}
35+
36+
do_install() {
537

6-
declare -A scaffolding_env
38+
# The `pkg_prefix` variable contains the fully-qualified Studio-relative path to
39+
# a specific build run (e.g., /hab/pkgs/<YOUR_ORIGIN>/sample-node-app/1.1.0/20180620174915).
40+
# In this callback, we copy the files that our application requires at runtime
41+
# into that directory, and once this step completes, Habitat will take
42+
# over to produce the finished package as a .hart file.
43+
local app_path="$pkg_prefix/app"
44+
mkdir -p $app_path
745

8-
# Define path to config file
9-
scaffolding_env[APP_CONFIG]="{{pkg.svc_config_path}}/config.json"
46+
cp -R \
47+
node_modules \
48+
public \
49+
routes \
50+
views \
51+
package.json \
52+
app.js \
53+
index.js \
54+
$app_path
55+
}

index.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var app = require('./app');
2+
var http = require('http');
3+
var port = process.env.PORT || 3000;
4+
5+
app.set('port', port);
6+
7+
var server = http.createServer(app);
8+
server.listen(port);

0 commit comments

Comments
 (0)