Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] feat: expressjs framework #784

Draft
wants to merge 64 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
1bf14da
feat: expressjs framework extension
yanksyoon Jan 2, 2025
5d5b8ba
test: expressjs framework extension unit tests
yanksyoon Jan 2, 2025
8afa87e
fix: lint
yanksyoon Jan 2, 2025
a887f00
fix: user prime app prefixing
yanksyoon Jan 2, 2025
be56040
test: spread test for express js
yanksyoon Jan 2, 2025
91e87e9
fix: linting
yanksyoon Jan 2, 2025
9d9be67
feat: add template
yanksyoon Jan 2, 2025
cb84f17
fix: app project dir
yanksyoon Jan 2, 2025
0af6e6a
chore: comment runtime debs
yanksyoon Jan 3, 2025
eacf0a1
chore: rename organise to organize
yanksyoon Jan 3, 2025
dc787d0
test: fix tests
yanksyoon Jan 3, 2025
98fbdd9
chore: move ca-certificates_data to slice def
yanksyoon Jan 3, 2025
d7c51cd
chore: move node slice
yanksyoon Jan 3, 2025
b7a21a5
chore: retrigger ci
yanksyoon Jan 3, 2025
9233492
fix: add node build dependency
yanksyoon Jan 3, 2025
acc3c05
fix: use nodejs package
yanksyoon Jan 3, 2025
b6b9ca2
fix: add npm plugin dependency
yanksyoon Jan 3, 2025
a1b5209
fix: add plugin dependencies
yanksyoon Jan 3, 2025
9a69970
fix: nodejs as deb
yanksyoon Jan 3, 2025
f55e87b
fix: nodejs package/slice separation
yanksyoon Jan 6, 2025
544434b
ci: retrigger (LAUCHPAD DOWN)
yanksyoon Jan 6, 2025
6ba9c5a
fix: prime default after deletion
yanksyoon Jan 6, 2025
82bbf94
feat: node version option fallthrough
yanksyoon Jan 9, 2025
582811b
chore: add rockcraft to supported extensions
yanksyoon Jan 10, 2025
650cab0
docs: expressjs tutorial
yanksyoon Jan 10, 2025
a25ea2a
chore: separate out dependencies part
yanksyoon Jan 10, 2025
dc3b61b
feat: add generation template
yanksyoon Jan 10, 2025
6e68078
docs: expressjs reference
yanksyoon Jan 10, 2025
024ea8e
docs: fix linting line too long
yanksyoon Jan 10, 2025
685d9be
fix: 24.04 package clash fix
yanksyoon Jan 10, 2025
b900591
fix: move stage packages
yanksyoon Jan 10, 2025
cea6fe4
fix: npmrc install
yanksyoon Jan 13, 2025
b20ebea
fix: tutorial code fix
yanksyoon Jan 13, 2025
a504403
fix: pebble lint
yanksyoon Jan 13, 2025
f6d31a2
test: add back build base
yanksyoon Jan 13, 2025
2a46cab
fix: base change
yanksyoon Jan 13, 2025
63b914d
docs: documentation improvements
yanksyoon Jan 14, 2025
c8d9ce5
test: add corner cases
yanksyoon Jan 14, 2025
c319aa2
chore: inline constants & match service def
yanksyoon Jan 14, 2025
a3c080a
chore: update template guildeline
yanksyoon Jan 14, 2025
dee32bf
docs: tutorial docs wording improvements & code highlighting
yanksyoon Jan 15, 2025
24ae419
feat: expressjs user override node with different bases
yanksyoon Jan 20, 2025
908581a
docs: expressjs user-override node version template
yanksyoon Jan 20, 2025
a5423c6
docs: reference to node-version override
yanksyoon Jan 20, 2025
f33666f
docs: update expressjs runtime codeblock to match template
yanksyoon Jan 20, 2025
37fa47c
docs: expressjs tutorial specify run directory
yanksyoon Jan 20, 2025
c1c8e28
docs: expressjs tutorial fix sumaary
yanksyoon Jan 20, 2025
02675c7
chore: organize expressjs extension code
yanksyoon Jan 20, 2025
c3af5c2
test: set node env to production
yanksyoon Jan 20, 2025
fd934b9
chore: remove user custom packages part
yanksyoon Jan 21, 2025
e3ea01b
chore: remove empty parts from rock gen
yanksyoon Jan 21, 2025
3589757
docs: re-order framework extensions list
yanksyoon Jan 22, 2025
752ce93
docs: remove customizing runtime deps part
yanksyoon Jan 22, 2025
c145919
docs: explain unsupported runtime dependencies issue
yanksyoon Jan 22, 2025
a90e80b
docs: expressjs explaination of different combinations
yanksyoon Jan 22, 2025
81c4abb
docs: pin tutorial express generator version
yanksyoon Jan 23, 2025
eca73e3
docs: reword parts
yanksyoon Jan 23, 2025
da5b5fe
docs: formatting & spelling
yanksyoon Jan 23, 2025
4128d2d
docs: fix linting
yanksyoon Jan 23, 2025
0531361
docs: update tutorial documentation
yanksyoon Jan 24, 2025
b62355e
docs: tutorial curl --fail flag
yanksyoon Jan 24, 2025
9c2dfb9
docs: lint fixes for tutorial
yanksyoon Jan 24, 2025
7f9aeb7
chore: clean up code
yanksyoon Jan 27, 2025
2ca47b2
chore: trigger CI
yanksyoon Jan 31, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions docs/reference/extensions/expressjs-framework.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
.. _expressjs-framework-reference:

expressjs-framework
-------------------

The ExpressJS extension streamlines the process of building ExpressJS
application rocks.

It facilitates the installation of ExpressJS application dependencies, including
Node and NPM, inside the rock. Additionally, it transfers your project files to
``/app`` within the rock.

.. note::
The ExpressJS extension is compatible with the ``bare`` and ``[email protected]``
bases.

Project requirements
====================

There is 1 requirement to be able to use the ``expressjs-framework`` extension:

1. There must be a ``package.json`` file in the ``app`` directory of the project
yanksyoon marked this conversation as resolved.
Show resolved Hide resolved
with ``start`` script defined.

``parts`` > ``expressjs-framework/dependencies:`` > ``stage-packages``
======================================================================

You can use this key to specify any dependencies required for your ExpressJS
application. In the following example we use it to specify ``libpq-dev``:

.. code-block:: yaml

parts:
expressjs-framework/dependencies:
stage-packages:
# list required packages or slices for your ExpressJS application below.
- libpq-dev

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a bug with installing any runtime dependencies (conflicting lib directory). See Matrix thread: https://matrix.to/#/!NPPCseDHKRvSBMUEXN:ubuntu.com/$upouJFvo8bjWS08ZNTe9yDtELNuVPh4qbs-ue3vEbbM?via=ubuntu.com&via=matrix.org


``parts`` > ``expressjs-framework/install-app`` > ``prime``
===========================================================

You can use this field to specify the files to be included or excluded from
your rock upon ``rockcraft pack``. Follow the ``app/<filename>`` notation. For
example:

.. code-block:: yaml

parts:
expressjs-framework/install-app:
prime:
- app/.env
- app/script.js
- app/templates
- app/static

Some files/directories, if they exist, are included by default. These include:
``<rock name>``, ``app.js``, ``migrate``, ``migrate.sh``, ``migrate.py``,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check migrate related files.

``bin``, ``public``, ``routes``, ``views``, ``package.json``,
``package-lock.json``.
yanksyoon marked this conversation as resolved.
Show resolved Hide resolved

Useful links
============

- :ref:`build-a-rock-for-an-expressjs-application`
1 change: 1 addition & 0 deletions docs/reference/extensions/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ initiating a new rock.
.. toctree::
:maxdepth: 1

expressjs-framework
yanksyoon marked this conversation as resolved.
Show resolved Hide resolved
flask-framework
django-framework
fastapi-framework
Expand Down
1 change: 1 addition & 0 deletions docs/reference/rockcraft.yaml.rst
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ Extensions to enable when building the ROCK.

Currently supported extensions:

- ``expressjs-framework``
- ``flask-framework``
- ``django-framework``
- ``go-framework``
Expand Down
43 changes: 43 additions & 0 deletions docs/tutorial/code/expressjs/app.js
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is autogenerated by express-generator with the addition of the timeRouter for tutorial's sake.

yanksyoon marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var timeRouter = require('./routes/time');

var app = express();

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

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/time', timeRouter);

// catch 404 and forward to error handler
app.use(function (req, res, next) {
next(createError(404));
});

// 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;
16 changes: 16 additions & 0 deletions docs/tutorial/code/expressjs/package.json
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is autogenerated by express-generator.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove this? It should get auto generated

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "app",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"cookie-parser": "~1.4.4",
"debug": "~2.6.9",
"express": "~4.16.1",
"http-errors": "~1.6.3",
"jade": "~1.11.0",
"morgan": "~1.9.1"
}
}
136 changes: 136 additions & 0 deletions docs/tutorial/code/expressjs/task.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
###########################################
# IMPORTANT
# Comments matter!
# The docs use the wrapping comments as
# markers for including said instructions
# as snippets in the docs.
###########################################
summary: Getting started with FastAPI tutorial

environment:

execute: |
# [docs:init-app]
sudo apt-get update -y && sudo apt-get install npm -y
npm install -g express-generator
echo "y" | npx express-generator app
yanksyoon marked this conversation as resolved.
Show resolved Hide resolved
cd app && npm install
# [docs:init-app-end]

npm start &
cd ..
retry -n 5 --wait 2 curl --fail localhost:3000

# [docs:curl-expressjs]
curl localhost:3000
# [docs:curl-expressjs-end]

kill $(lsof -t -i:3000)
yanksyoon marked this conversation as resolved.
Show resolved Hide resolved

# [docs:create-rockcraft-yaml]
rockcraft init --profile expressjs-framework
# [docs:create-rockcraft-yaml-end]
sed -i "s/name: .*/name: expressjs-hello-world/g" rockcraft.yaml

# [docs:pack]
ROCKCRAFT_ENABLE_EXPERIMENTAL_EXTENSIONS=true rockcraft pack
# [docs:pack-end]

# [docs:ls-rock]
ls *.rock -l --block-size=MB
# [docs:ls-rock-end]

# [docs:skopeo-copy]
sudo rockcraft.skopeo --insecure-policy \
copy oci-archive:expressjs-hello-world_0.1_amd64.rock \
docker-daemon:expressjs-hello-world:0.1
# [docs:skopeo-copy-end]

# [docs:docker-images]
sudo docker images expressjs-hello-world:0.1
# [docs:docker-images-end]

# [docs:docker-run]
sudo docker run --rm -d -p 3000:3000 \
--name expressjs-hello-world expressjs-hello-world:0.1
# [docs:docker-run-end]
retry -n 5 --wait 2 curl --fail localhost:3000

# [docs:curl-expressjs-rock]
curl localhost:3000
# [docs:curl-expressjs-rock-end]

# [docs:get-logs]
sudo docker exec expressjs-hello-world pebble logs expressjs
# [docs:get-logs-end]

# [docs:stop-docker]
sudo docker stop expressjs-hello-world
sudo docker rmi expressjs-hello-world:0.1
# [docs:stop-docker-end]

# [docs:change-base]
sed -i "s/^base: [email protected]/base: bare/g" rockcraft.yaml
# [docs:change-base-end]
sed -i "s/version: .*/version: 0.1-chiselled/g" rockcraft.yaml

# [docs:chisel-pack]
ROCKCRAFT_ENABLE_EXPERIMENTAL_EXTENSIONS=true rockcraft pack
# [docs:chisel-pack-end]

# [docs:ls-bare-rock]
ls *.rock -l --block-size=MB
# [docs:ls-bare-rock-end]

# [docs:docker-run-chisel]
sudo rockcraft.skopeo --insecure-policy \
copy oci-archive:expressjs-hello-world_0.1-chiselled_amd64.rock \
docker-daemon:expressjs-hello-world:0.1-chiselled
sudo docker images expressjs-hello-world:0.1-chiselled
sudo docker run --rm -d -p 3000:3000 \
--name expressjs-hello-world expressjs-hello-world:0.1-chiselled
# [docs:docker-run-chisel-end]
retry -n 5 --wait 2 curl --fail localhost:3000

# [docs:curl-expressjs-bare-rock]
curl localhost:3000
# [docs:curl-expressjs-bare-rock-end]

# [docs:stop-docker-chisel]
sudo docker stop expressjs-hello-world
sudo docker rmi expressjs-hello-world:0.1-chiselled
# [docs:stop-docker-chisel-end]

ROCKCRAFT_ENABLE_EXPERIMENTAL_EXTENSIONS=True rockcraft clean
yanksyoon marked this conversation as resolved.
Show resolved Hide resolved
mv time.js app/routes/
mv app.js app/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's change this to inject the new things that are required in the existing file

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's have files with the snippets and include that in the tutorial and tell them to open the file with a code editor. Here, we will need to copy the contents into the existing file

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps call the files time_app_require.js and time_app_route.js

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've kept the file names time.js to match the route filenames of other frameworks and called the app file time_app.js. Would this approach sound reasonable?

sed -i "s/version: .*/version: 0.2/g" rockcraft.yaml

# [docs:docker-run-update]
ROCKCRAFT_ENABLE_EXPERIMENTAL_EXTENSIONS=true rockcraft pack
sudo rockcraft.skopeo --insecure-policy \
copy oci-archive:expressjs-hello-world_0.2_amd64.rock \
docker-daemon:expressjs-hello-world:0.2
sudo docker images expressjs-hello-world:0.2
sudo docker run --rm -d -p 3000:3000 \
--name expressjs-hello-world expressjs-hello-world:0.2
# [docs:docker-run-update-end]
retry -n 5 --wait 2 curl --fail localhost:3000/time

# [docs:curl-time]
curl localhost:3000/time
yanksyoon marked this conversation as resolved.
Show resolved Hide resolved
# [docs:curl-time-end]

# [docs:stop-docker-updated]
sudo docker stop expressjs-hello-world
sudo docker rmi expressjs-hello-world:0.2
# [docs:stop-docker-updated-end]

# [docs:cleanup]
# exit and delete the virtual environment
yanksyoon marked this conversation as resolved.
Show resolved Hide resolved
# delete all the files created during the tutorial
rm expressjs-hello-world_0.1_amd64.rock \
yanksyoon marked this conversation as resolved.
Show resolved Hide resolved
expressjs-hello-world_0.1-chiselled_amd64.rock \
expressjs-hello-world_0.2_amd64.rock \
rockcraft.yaml
# [docs:cleanup-end]
8 changes: 8 additions & 0 deletions docs/tutorial/code/expressjs/time.js
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simple time route tutorial file.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var express = require('express');
var router = express.Router();

router.get('/', function (req, res, next) {
res.send(Date());
});

module.exports = router;
Loading
Loading