Skip to content

Commit

Permalink
Replace scaffolding and add some docs
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
cnunciato committed Jun 20, 2018
1 parent 006ed2b commit ec29deb
Show file tree
Hide file tree
Showing 17 changed files with 955 additions and 223 deletions.
101 changes: 49 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,93 +1,90 @@
# Habitat Sample Node App

Welcome to the Habitat Sample Node App!
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.

### Follow the demo instructions
### Follow the Demos!

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.
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.

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

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

### Prerequisites

## Instructions
To package and run this application with Habitat, you'll need to:

To practice packaging/running this app with Habitat
* [Install and configure Habitat](https://www.habitat.sh/docs/install-habitat/)
* [Install Docker](https://www.docker.com/community-edition) (if you're on Mac or Windows)
* Clone this repository:

$ git clone https://github.com/habitat-sh/sample-node-app.git
$ cd sample-node-app

### Workstation Prerequisites
* Install and set up Habitat [(Instructions here)](https://www.habitat.sh/tutorials/download/)
* Install Docker [(Instructions here)](https://www.docker.com/community-edition)
* Clone this repository
```bash
$ git clone https://github.com/habitat-sh/sample-node-app.git
```
* Change directories
```bash
$ cd sample-node-app
```

### Setup for First-time Users

Before you can build the app, you'll need to create an origin and accompanying keys.
The quickest way to do this is by running `hab setup` and following the prompts.
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:

```bash
```
$ hab setup
```

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

### Building the Package
From the `/habitat` directory in this repo, open the habitat/plan.sh file:

Your habitat/plan.sh should look like this:
```sh
pkg_name=sample-node-app
pkg_origin=your_origin
pkg_scaffolding="core/scaffolding-node"
```
First, change the value of `your_origin` to the origin name your created in the previous section.

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

Next, let's add in a version number
```sh
pkg_name=sample-node-app
```
pkg_origin=your_origin
pkg_scaffolding="core/scaffolding-node"
pkg_version="1.0.1"
pkg_name=sample-node-app
pkg_version="1.1.0"
pkg_deps=(core/node)
...
```
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/).

Now save and close the file.
Next, let's change the version number:
```
...
pkg_version="1.1.1"
...
```

Enter the Habitat Studio
Save and close the file, then enter the Habitat Studio:

```bash
```
$ hab studio enter
```

And run build
And run a build:

```bash
(studio) $ build
```
[1][default:/src:0]# build
```

Habitat will produce a package (a `.hart` file) and place it in the `results` directory.

### Running the Package with Docker

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

Then exit out of the studio:
```bash
(studio) $ exit
```
[2][default:/src:0]# source results/last_build.env
[3][default:/src:0]# hab pkg export docker results/$pkg_artifact
```

Now start a Docker container from that image.
Then exit the Studio:

```bash
$ docker run -it -p 8000:8000 your_origin/sample-node-app
```
[4][default:/src:0]# exit
```
And start a Docker container with your newly created image:

```
$ docker run -it -p 3000:3000 <YOUR_ORIGIN>/sample-node-app
```

Now head to http://localhost:8000 and see your running app!
Now head to http://localhost:3000 and see your running app!
35 changes: 0 additions & 35 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,11 @@
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var index = require('./routes/index');
var users = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', index);
app.use('/users', users);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});

// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};

// render the error page
res.status(err.status || 500);
res.render('error');
});

module.exports = app;
90 changes: 0 additions & 90 deletions bin/www

This file was deleted.

4 changes: 0 additions & 4 deletions default_config.json

This file was deleted.

4 changes: 4 additions & 0 deletions dev-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"title": "Habitat - The fastest path from code to cloud native.",
"message": "Welcome to the Habitat Node.js sample app."
}
3 changes: 2 additions & 1 deletion habitat/config/config.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"message": "You are running version {{pkg.version}} of the Habitat Node.js sample app."
"title": "{{ cfg.title }}",
"message": "You are running version {{ pkg.version }} of the Habitat Node.js sample app."
}
11 changes: 6 additions & 5 deletions habitat/default.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Use this file to templatize your application's native configuration files.
# See the docs at https://www.habitat.sh/docs/create-packages-configure/.
# You can safely delete this file if you don't need it.
# When Habitat starts this service, it'll use the values we define here
# to render the templates we define in our plan's `config` directory.
#
# To learn more about Habitat's configuration features, see the docs at
# https://www.habitat.sh/docs/developing-packages/#add-configuration.

# Message of the Day
message = "Hello, World!"
title = "Habitat - The fastest path from code to cloud native."
14 changes: 14 additions & 0 deletions habitat/hooks/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh

# Habitat uses "hooks" to manage the application lifecycle. The run hook
# is used for starting (and restarting) a service. Here, we launch the Node
# app in the usual way, passing the path to the Habitat-managed config file
# as a command-line argument.
#
# To learn more about the run hook, its conventions and other available lifecycle
# hooks, see the docs at https://www.habitat.sh/docs/reference/#available-hooks.

exec node \
{{ pkg.path }}/app/index.js \
{{ pkg.svc_config_path }}/config.json \
2>&1
56 changes: 51 additions & 5 deletions habitat/plan.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,55 @@
# The plan file tells Habitat how to build a package.
#
# In this plan, we're asking Habitat to provide us with Node.js and NPM
# (by declaring a dependency on the core/node package) so we can install our
# application's JavaScript dependencies (and ultimately run our app). Then we
# copy the files we'll need to run the package into a directory in the Habitat
# Studio that will become the resulting package.
#
# To learn more about writing Habitat plans, see Developing Packages
# in the Habitat docs at https://www.habitat.sh/docs/developing-packages.
#
# To explore all Habitat-maintained and community-contributed packages,
# visit the Habitat Builder depot at https://bldr.habitat.sh/#/pkgs.

pkg_name=sample-node-app
pkg_origin=your_origin
pkg_scaffolding="core/scaffolding-node"
pkg_version="1.0.1"
pkg_version="1.1.0"
pkg_deps=(core/node)

# Habitat provides you with a number of built-in "callbacks" to use
# in the course of your build, all of which are explained in the docs
# at https://habitat.sh/docs/reference/#reference-callbacks.
#
# Here, we're implementing the do_build and do_install callbacks
# to install dependencies and assemble the application package.

do_build() {

# By default, we're in the directory in which the Studio was entered
# (in this case, presumably the project root), so we can run commands
# as though we were in that same directory. By the time we reach this
# callback, `npm` will have been installed for us.
npm install
}

do_install() {

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

# Define path to config file
scaffolding_env[APP_CONFIG]="{{pkg.svc_config_path}}/config.json"
cp -R \
node_modules \
public \
routes \
views \
package.json \
app.js \
index.js \
$app_path
}
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var app = require('./app');
var http = require('http');
var port = process.env.PORT || 3000;

app.set('port', port);

var server = http.createServer(app);
server.listen(port);
Loading

0 comments on commit ec29deb

Please sign in to comment.