Skip to content
This repository has been archived by the owner on Nov 15, 2021. It is now read-only.

Commit

Permalink
now you can display syslog
Browse files Browse the repository at this point in the history
  • Loading branch information
ChiChou committed Aug 7, 2018
1 parent 0e7cf46 commit 2636531
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 28 deletions.
39 changes: 19 additions & 20 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ router
ctx.body = { status: 'ok', pid }
})

const port = parseInt(process.env.PORT, 10) || 31337
const host = process.env.HOST || 'localhost'


app
.use(compress({
filter(contentType) {
Expand Down Expand Up @@ -97,21 +93,24 @@ if (process.env.NODE_ENV === 'development') {
app.use(logger())
}

console.info(`listening on http://${host}:${port}`.green)
const server = http.createServer(app.callback())
channels.attach(server)
server.listen(port, host)


process.on('unhandledRejection', (err) => {
console.error('An unhandledRejection occurred: ')
console.error(`Rejection: ${err}`)
console.error(err.stack)

channels.broadcast('unhandledRejection', {
err: err.toString(),
stack: err.stack,
function start({ host, port }) {
console.info(`listening on http://${host}:${port}`.green)
const server = http.createServer(app.callback())
channels.attach(server)
server.listen(port, host)
process.on('unhandledRejection', (err) => {
console.error('An unhandledRejection occurred: '.red)
console.error(`Rejection: ${err}`.red)
console.error(err.stack)

channels.broadcast('unhandledRejection', {
err: err.toString(),
stack: err.stack,
})
})
})
}

module.exports = app
module.exports = {
app,
start,
}
32 changes: 31 additions & 1 deletion bin/cli.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
#!/usr/bin/env node
const net = require('net')

require('../app.js')
const app = require('../app')
const config = require('../lib/config')

function usage() {
console.log('usage: passionfruit {server|syslog}')
process.exit(-1)
}

function syslog() {
const port = parseInt(process.argv[3], 10)
if (Number.isNaN(port))
console.log('usage: passionfruit console [port]')

net.connect({ host: 'localhost', port })
.on('end', () => process.exit(0))
.pipe(process.stdout)
}

function main() {
if (process.argv.length > 2) {
const action = process.argv[2].toLowerCase()
if (action === 'syslog')
return syslog()
if (action !== 'server')
return usage()
}
return app.start(config)
}

main()
3 changes: 3 additions & 0 deletions gui/src/views/Inspect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ import { mapGetters, mapActions, mapMutations } from 'vuex'
import { AsyncSearch, debounce } from '~/lib/utils'
import {
GET_SOCKET, STORE_SOCKET,
STORE_SYSLOG_SERVER_PORT,
CONSOLE_UNREAD, CONSOLE_APPEND, CONSOLE_CLEAR,
DOWNLOADING, PROGRESS,
ALL_HOOKS, DELETE_HOOK,
Expand Down Expand Up @@ -202,6 +203,7 @@ export default {
this.$toast.open(`an exception has occured: ${err}`)
}
})
.on('syslog-port', this.setSyslogServerPort)
.on('console', this.consoleAppend)
.on('app', ({ app, device }) => {
this.device = device
Expand Down Expand Up @@ -241,6 +243,7 @@ export default {
}),
...mapMutations({
storeSocket: STORE_SOCKET,
setSyslogServerPort: STORE_SYSLOG_SERVER_PORT,
consoleAppend: CONSOLE_APPEND,
consoleClear: CONSOLE_CLEAR,
})
Expand Down
6 changes: 5 additions & 1 deletion gui/src/views/tabs/Console.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
<span>Clear</span>
</button>
</p>
<p class="control">Use
<code>nc localhost {{ port }}</code> or
<code>passionfruit syslog {{ port }}</code> in terminal to view NSLog</p>
</b-field>
<ul class="console">
<li v-for="(item, i) in list" :key="i">
Expand Down Expand Up @@ -51,7 +54,7 @@
<script>
import { mapGetters, mapMutations } from 'vuex'
import {
CONSOLE_ACTIVE, CONSOLE_CLEAR,
CONSOLE_ACTIVE, CONSOLE_CLEAR, GET_SYSLOG_SERVER_PORT,
CONSOLE_LIST, CONSOLE_UNREAD, CONSOLE_RUNNING
} from '~/vuex/types'
Expand All @@ -66,6 +69,7 @@ export default {
}
},
...mapGetters({
port: GET_SYSLOG_SERVER_PORT,
list: CONSOLE_LIST,
unread: CONSOLE_UNREAD,
})
Expand Down
5 changes: 5 additions & 0 deletions gui/src/vuex/mods/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ export const state = {
unread: 0,
active: false,
logging: true,
syslogServerPort: 0,
}

export const mutations = {
[types.STORE_SYSLOG_SERVER_PORT](state, port) {
state.syslogServerPort = port
},
[types.CONSOLE_RUNNING](state, on) {
state.logging = on
},
Expand Down Expand Up @@ -40,4 +44,5 @@ export const getters = {
[types.CONSOLE_LIST]: state => state.list,
[types.CONSOLE_UNREAD]: state => state.unread,
[types.CONSOLE_RUNNING]: state => state.loggine,
[types.GET_SYSLOG_SERVER_PORT]: state => state.syslogServerPort,
}
3 changes: 3 additions & 0 deletions gui/src/vuex/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,6 @@ export const SET_DOWNLOAD_TOTAL = 'SET_DOWNLOAD_TOTAL'
export const UPDATE_BYTES = 'UPDATE_BYTES'
export const DOWNLOAD_TOTAL_SIZE = 'DOWNLOAD_TOTAL_SIZE'
export const DOWNLOADED_SIZE = 'DOWNLOADED_SIZE'

export const STORE_SYSLOG_SERVER_PORT = 'STORE_SYSLOG_SERVER_PORT'
export const GET_SYSLOG_SERVER_PORT = 'GET_SYSLOG_SERVER_PORT'
4 changes: 4 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
port: parseInt(process.env.PORT, 10) || 31337,
host: process.env.HOST || 'localhost',
}
12 changes: 7 additions & 5 deletions lib/rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class RpcHandler extends Handler {
this.syslogClients.add(client)
console.log(`new client ${ip}`)
const writable = new BannerStream({ socket: client })
fs.createReadStream(this.syslogFile).pipe(writable)
fs.createReadStream(this.syslogFile).on('error', () => {}).pipe(writable)
client.on('end', () => {
console.log(`client ${ip} disconnected`)
this.syslogClients.delete(client)
Expand All @@ -100,8 +100,9 @@ class RpcHandler extends Handler {
this.syslogServer = server
return new Promise((resolve, reject) => {
server.listen({ host: 'localhost', port: 0 }, () => {
console.log(`nc localhost ${server.address().port}`.yellow)
resolve()
const serverPort = server.address().port
console.log(`nc localhost ${serverPort}`.yellow)
resolve(serverPort)
}).on('error', reject)
})
}
Expand All @@ -112,7 +113,8 @@ class RpcHandler extends Handler {
script.destroyed.connect(() => {
socket.emit('script_destroyed')
socket.disconnect(true)
this.syslogClients.forEach(client => client.close())
this.syslogClients.forEach(client => client.end('process has been terminated'.gray))
this.syslogServer.close()
})
script.message.connect((message, data) => {
const mapping = {
Expand All @@ -130,7 +132,7 @@ class RpcHandler extends Handler {
this.script = script
this.agent = script.exports

await this.createSysLogServer()
this.socket.emit('syslog-port', await this.createSysLogServer())
}

handleSend({ payload }, data) {
Expand Down
2 changes: 1 addition & 1 deletion scripts/dev-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ compile.run(true)
const TAG_SERVER = '[Server]'.magenta

nodemon({
script: 'app.js',
script: 'bin/cli.js',
ext: 'js json',
watch: ['lib', 'app.js', 'scripts'],
})
Expand Down

0 comments on commit 2636531

Please sign in to comment.