1
0
Fork 0

Implement basic server

This commit is contained in:
Gregory Eremin 2015-08-29 16:56:19 +03:00
parent a04a36bf90
commit d12ace7f37
2 changed files with 27 additions and 0 deletions

View File

@ -3,6 +3,7 @@ package secondly
import (
"encoding/json"
"flag"
"fmt"
"log"
"os"
"os/signal"
@ -37,6 +38,11 @@ func Manage(target interface{}) {
bootstrap()
}
// StartServer will start an HTTP server with web interface to edit config.
func StartServer(host string, port int) {
go startServer(fmt.Sprintf("%s:%d", host, port))
}
// HandleSIGHUP waits a SIGHUP system call and reloads configuration when
// receives one.
func HandleSIGHUP() {

21
server.go Normal file
View File

@ -0,0 +1,21 @@
package secondly
import (
"encoding/json"
"log"
"net/http"
)
func startServer(addr string) {
mux := http.NewServeMux()
mux.HandleFunc("/fields.json", fieldsHandler)
log.Println("Starting configuration server on", addr)
go http.ListenAndServe(addr, mux)
}
func fieldsHandler(rw http.ResponseWriter, req *http.Request) {
fields := extractFields(config, "")
body, _ := json.Marshal(fields)
rw.Write(body)
}