|
1 | 1 | #!/usr/bin/env node |
2 | 2 | /* |
3 | | - * The MIT License (MIT) |
4 | | - * Copyright (c) 2019. Wise Wild Web |
5 | | - * |
6 | | - * Permission is hereby granted, free of charge, to any person obtaining a copy |
7 | | - * of this software and associated documentation files (the "Software"), to deal |
8 | | - * in the Software without restriction, including without limitation the rights |
9 | | - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
10 | | - * copies of the Software, and to permit persons to whom the Software is |
11 | | - * furnished to do so, subject to the following conditions: |
| 3 | + * Copyright (c) 2019. MIT License. |
| 4 | + * @author Nathanael Braun <n8tz.js@gmail.com> |
| 5 | + */ |
| 6 | + |
| 7 | +/** |
| 8 | + * @file start.js |
12 | 9 | * |
13 | | - * The above copyright notice and this permission notice shall be included in all |
14 | | - * copies or substantial portions of the Software. |
| 10 | + * Build control server for lpack-react. Manages profile lifecycle and |
| 11 | + * exposes a simple HTTP API for monitoring and control: |
15 | 12 | * |
16 | | - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17 | | - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
18 | | - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
19 | | - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
20 | | - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
21 | | - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
22 | | - * SOFTWARE. |
| 13 | + * GET /status — current build status (running commands, logs) |
| 14 | + * GET /restart — restart the current profile |
| 15 | + * GET /switch?to=prod — stop current profile, switch to another |
| 16 | + * GET /kill — stop all processes and exit |
23 | 17 | * |
24 | | - * @author : Nathanael Braun |
25 | | - * @contact : n8tz.js@gmail.com |
| 18 | + * Usage: |
| 19 | + * lpack-run :dev start # start dev profile |
| 20 | + * lpack-run :prod start -p 9090 # start prod with control server on port 9090 |
26 | 21 | */ |
27 | 22 | 'use strict'; |
28 | 23 |
|
29 | 24 | const program = require('commander'), |
30 | 25 | express = require("express"), |
31 | 26 | server = express(), |
32 | 27 | http = require('http').Server(server), |
33 | | - exec = require('child_process').exec, |
34 | 28 | argz = process.argv.slice(2), |
35 | 29 | Profile = require('../utils/Profile'); |
36 | 30 |
|
37 | 31 | let profileId = process.env.__LPACK_PROFILE__ || "default"; |
38 | 32 |
|
39 | | -if ( argz[ 0 ] && /^\:.*$/.test(argz[ 0 ]) ) |
40 | | - profileId = argz.shift().replace(/^\:(.*)$/, '$1'); |
| 33 | +// Parse :profileId from CLI args (e.g. "lpack-run :dev start") |
| 34 | +if ( argz[0] && /^\:.*$/.test(argz[0]) ) |
| 35 | + profileId = argz.shift().replace(/^\:(.*)$/, '$1'); |
41 | 36 |
|
42 | 37 | program |
43 | | - .option('-l, --local', 'Limit Build control web api to localhost') |
44 | | - .option('-p, --port [port=9090]', 'Build control') |
45 | | - .parse(process.argv); |
| 38 | + .option('-l, --local', 'Limit build control API to localhost') |
| 39 | + .option('-p, --port [port]', 'Build control server port (default: 9090)') |
| 40 | + .parse(process.argv); |
46 | 41 |
|
47 | | -let port = program.port === true ? 9090 : program.port, |
48 | | - pDir = program.source || process.cwd(); |
| 42 | +let port = program.port === true ? 9090 : program.port, |
| 43 | + profile = new Profile(profileId); |
49 | 44 |
|
50 | | -let profile = new Profile(profileId); |
51 | 45 | profile.start(); |
52 | | -profile.onComplete(e => process.exit()); |
| 46 | +profile.onComplete(() => process.exit()); |
53 | 47 |
|
54 | | -process.on('SIGINT', e => profile.stop().then(e => process.exit())); // catch ctrl-c |
55 | | -process.on('SIGTERM', e => profile.stop().then(e => process.exit())); // catch kill |
| 48 | +process.on('SIGINT', () => profile.stop().then(() => process.exit())); |
| 49 | +process.on('SIGTERM', () => profile.stop().then(() => process.exit())); |
56 | 50 |
|
| 51 | +// Start the control server if a port is configured |
57 | 52 | if ( port ) { |
58 | | - server.use(express.json()); // to support JSON-encoded bodies |
59 | | - server.use(express.urlencoded()); // to support URL-encoded bodies |
60 | | - |
61 | | - server.use( |
62 | | - "/status", |
63 | | - ( req, res ) => { |
64 | | - res.header("Access-Control-Allow-Origin", "*"); |
65 | | - res.json({ status: profile.getStatus() }) |
66 | | - } |
67 | | - ); |
68 | | - |
69 | | - server.use( |
70 | | - "/restart", |
71 | | - ( req, res ) => { |
72 | | - res.header("Access-Control-Allow-Origin", "*"); |
73 | | - profile.start(); |
74 | | - |
75 | | - res.json({ success: true }) |
76 | | - } |
77 | | - ); |
78 | | - |
79 | | - server.use( |
80 | | - "/switch", |
81 | | - ( req, res ) => { |
82 | | - res.header("Access-Control-Allow-Origin", "*"); |
83 | | - profileId = req.query.to || "prod"; |
84 | | - profile.stop().then( |
85 | | - e => { |
86 | | - profile = new Profile(profileId); |
87 | | - profile.start(); |
88 | | - res.json({ success: true, profileId }) |
89 | | - } |
90 | | - ); |
91 | | - |
92 | | - |
93 | | - } |
94 | | - ); |
95 | | - |
96 | | - server.use( |
97 | | - "/kill", |
98 | | - ( req, res ) => { |
99 | | - res.header("Access-Control-Allow-Origin", "*"); |
100 | | - res.json({ success: true }); |
101 | | - |
102 | | - profile.stop().then(e => process.exit()); |
103 | | - } |
104 | | - ); |
105 | | - |
106 | | - let server_instance = http.listen(parseInt(port), function () { |
107 | | - console.info('Build manager running on ', server_instance.address(), server_instance.address().port) |
108 | | - }); |
| 53 | + server.use(express.json()); |
| 54 | + server.use(express.urlencoded({ extended: true })); |
| 55 | + |
| 56 | + server.get("/status", ( req, res ) => { |
| 57 | + res.header("Access-Control-Allow-Origin", "*"); |
| 58 | + res.json({ status: profile.getStatus() }); |
| 59 | + }); |
| 60 | + |
| 61 | + server.get("/restart", ( req, res ) => { |
| 62 | + res.header("Access-Control-Allow-Origin", "*"); |
| 63 | + profile.start(); |
| 64 | + res.json({ success: true }); |
| 65 | + }); |
| 66 | + |
| 67 | + server.get("/switch", ( req, res ) => { |
| 68 | + res.header("Access-Control-Allow-Origin", "*"); |
| 69 | + profileId = req.query.to || "prod"; |
| 70 | + profile.stop().then(() => { |
| 71 | + profile = new Profile(profileId); |
| 72 | + profile.start(); |
| 73 | + res.json({ success: true, profileId }); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + server.get("/kill", ( req, res ) => { |
| 78 | + res.header("Access-Control-Allow-Origin", "*"); |
| 79 | + res.json({ success: true }); |
| 80 | + profile.stop().then(() => process.exit()); |
| 81 | + }); |
| 82 | + |
| 83 | + http.listen(parseInt(port), function () { |
| 84 | + console.info('Build manager running on port', this.address().port); |
| 85 | + }); |
109 | 86 | } |
0 commit comments