From dd53bbd1db2949f3d8b8759eef4d47c942cc416d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Carranza=20V=C3=A9lez?= Date: Fri, 24 Jul 2015 14:26:33 -0300 Subject: [PATCH] Call the gosuper from node via unix socket + http. --- entry.sh | 4 ++++ gosuper/main.go | 15 +++++++++++++-- src/api.coffee | 5 +++++ src/config.coffee | 1 + 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/entry.sh b/entry.sh index ff876fc3..a9952eaf 100755 --- a/entry.sh +++ b/entry.sh @@ -2,6 +2,10 @@ set -e +if [ -z $GOSUPER_SOCKET ]; then + GOSUPER_SOCKET=/var/run/gosuper.sock +fi + [ -d /dev/net ] || mkdir -p /dev/net [ -c /dev/net/tun ] || diff --git a/gosuper/main.go b/gosuper/main.go index be0eb30e..51d5a6f5 100644 --- a/gosuper/main.go +++ b/gosuper/main.go @@ -2,7 +2,9 @@ package main import ( "fmt" + "net" "net/http" + "os" "resin-supervisor/Godeps/_workspace/src/github.com/gorilla/mux" ) @@ -25,6 +27,7 @@ func main() { */ ResinDataPath = "/resin-data/" + laddr := os.Getenv("GOSUPER_SOCKET") r := mux.NewRouter() r.HandleFunc("/ping", pingHandler) @@ -32,6 +35,14 @@ func main() { apiv1.HandleFunc("/purge", PurgeHandler).Methods("POST") - http.ListenAndServe(":8080", r) - + listener, err := net.Listen("unix", laddr) + if err != nil { + fmt.Println("Could not listen on " + laddr) + return + } + err = http.Serve(listener, r) + if err != nil { + fmt.Println("Could not start HTTP server") + return + } } diff --git a/src/api.coffee b/src/api.coffee index 2604263e..eeeaaa5a 100644 --- a/src/api.coffee +++ b/src/api.coffee @@ -6,6 +6,8 @@ tty = require './lib/tty' knex = require './db' express = require 'express' bodyParser = require 'body-parser' +request = require 'request' +config = require './config' module.exports = (secret) -> api = express() @@ -57,4 +59,7 @@ module.exports = (secret) -> .catch (err) -> res.status(503).send(err?.message or err or 'Unknown error') + api.post '/v1/purge', (req, res) -> + request.post(config.gosuperAddress + '/v1/purge', req.body).pipe(res) + return api diff --git a/src/config.coffee b/src/config.coffee index 211e9495..c689cb2e 100644 --- a/src/config.coffee +++ b/src/config.coffee @@ -10,6 +10,7 @@ checkInt = (s) -> module.exports = config = apiEndpoint: process.env.API_ENDPOINT ? 'https://api.resin.io' listenPort: process.env.LISTEN_PORT ? 80 + gosuperAddress: "http://unix:#{process.env.GOSUPER_SOCKET}:" registryEndpoint: process.env.REGISTRY_ENDPOINT ? 'registry.resin.io' pubnub: subscribe_key: process.env.PUBNUB_SUBSCRIBE_KEY ? 'sub-c-bananas'