Skip to content

Commit c59bbd9

Browse files
authored
add a debugger example (#7)
1 parent 13f5cf2 commit c59bbd9

13 files changed

+1243
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
**/node_modules
22
**/start-time.txt
3+
*/.idea

101-debugger/.dockerignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

101-debugger/Dockerfile

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
FROM node:10
2+
3+
# Default value; will be overridden by build_args, if passed
4+
ARG node_env=production
5+
6+
ENV NODE_ENV $node_env
7+
8+
WORKDIR /app
9+
10+
ADD package.json .
11+
ADD yarn.lock .
12+
RUN yarn install
13+
14+
ADD . .
15+
16+
ENTRYPOINT [ "node", "/app/index.js" ]

101-debugger/Tiltfile

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# -*- mode: Python -*
2+
3+
k8s_yaml('kubernetes.yaml')
4+
5+
k8s_resource('example-nodejs', port_forwards=[
6+
8000, # app itself
7+
9229 # debugger
8+
], resource_deps=['deploy']
9+
)
10+
11+
# Records the current time, then kicks off a server update.
12+
# Normally, you would let Tilt do deploys automatically, but this
13+
# shows you how to set up a custom workflow that measures it.
14+
local_resource(
15+
'deploy',
16+
'python now.py > start-time.txt',
17+
)
18+
19+
# Add a live_update rule to our docker_build
20+
congrats = "🎉 Congrats, you ran a live_update! 🎉"
21+
docker_build('example-nodejs-image', '.',
22+
build_args={'node_env': 'development'},
23+
entrypoint='yarn run nodemon --inspect=0.0.0.0:9229 /app/index.js',
24+
live_update=[
25+
sync('.', '/app'),
26+
run('cd /app && yarn install', trigger=['./package.json', './yarn.lock']),
27+
28+
# if all that changed was start-time.txt, make sure the server
29+
# reloads so that it will reflect the new startup time
30+
run('touch /app/index.js', trigger='./start-time.txt'),
31+
32+
# add a congrats message!
33+
run('sed -i "s/Hello cats!/{}/g" /app/views/index.mustache'.
34+
format(congrats)),
35+
])

101-debugger/index.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Gratefully adapted from https://github.com/HemingwayLee/sample-mustache-express
3+
*/
4+
const fs = require('fs');
5+
6+
const express = require('express');
7+
const app = express();
8+
const mustacheExpress = require('mustache-express');
9+
10+
let timeSince = 'N/A';
11+
12+
app.engine('mustache', mustacheExpress());
13+
14+
app.set('view engine', 'mustache');
15+
app.set('views', __dirname + '/views');
16+
app.use(express.static('public'));
17+
18+
app.get('/', (req, res) => {
19+
res.render('index.mustache', {
20+
time: timeSince,
21+
});
22+
});
23+
24+
app.listen(8000, () => {
25+
timeSince = getSecsSinceDeploy();
26+
console.log('Server running at http://localhost:8000/');
27+
});
28+
29+
function getSecsSinceDeploy() {
30+
let curTimeMs = new Date().getTime();
31+
let contents = fs.readFileSync('/app/start-time.txt', 'utf8');
32+
let startTimeMs = parseInt(contents.trim()) / 10**6;
33+
return ((curTimeMs - startTimeMs) / 10**3).toFixed(2)
34+
}

101-debugger/kubernetes.yaml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: example-nodejs
5+
labels:
6+
app: example-nodejs
7+
spec:
8+
selector:
9+
matchLabels:
10+
app: example-nodejs
11+
template:
12+
metadata:
13+
labels:
14+
app: example-nodejs
15+
spec:
16+
containers:
17+
- name: example-nodejs
18+
image: example-nodejs-image
19+
ports:
20+
- name: http
21+
containerPort: 8000
22+
- name: debug
23+
containerPort: 9229

101-debugger/now.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import time
2+
3+
print("%d" % (float(time.time()) * 1000 * 1000 * 1000))

101-debugger/package.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "tilt-example-nodejs",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"dependencies": {
7+
"express": "^4.17.1",
8+
"immutable": "^3.8.2",
9+
"mustache-express": "^1.3.0"
10+
},
11+
"devDependencies": {
12+
"nodemon": "^2.0.2"
13+
}
14+
}

101-debugger/public/pets.png

182 KB
Loading

101-debugger/views/index.mustache

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!doctype html>
2+
<html>
3+
<body style="font-size: 30px; font-family: sans-serif; margin: 0;">
4+
<div style="display: flex; flex-direction: column; width: 100vw; height: 100vh; align-items: center; justify-content: center;">
5+
<img src="pets.png" style="max-width: 30vw; max-height: 30vh;">
6+
<div>Hello cats!</div>
7+
<div>Time from "deploy" button pressed → server up: {{time}}s</div>
8+
</div>
9+
</body>
10+
</html>

0 commit comments

Comments
 (0)