From a580e5f3b54889fdd1441be68af44f57a3797807 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Sat, 24 Sep 2016 17:34:22 +0200 Subject: added basic web interface diff --git a/sample-config.toml b/sample-config.toml index 1d50503..d0dc81f 100644 --- a/sample-config.toml +++ b/sample-config.toml @@ -36,6 +36,7 @@ inputs = [ { number = 1, server = "master", channel = "main" }, [clients.web] addr = ":4080" + static_files = "/usr/share/rhctl/static/" [clients.telnet] addr = "localhost:4023" diff --git a/src/rhctl/conf.go b/src/rhctl/conf.go index 3e35abb..c163b60 100644 --- a/src/rhctl/conf.go +++ b/src/rhctl/conf.go @@ -88,7 +88,8 @@ type Config struct { Clients struct { Web struct { - Address string `toml:"addr"` + Address string `toml:"addr"` + StaticDir string `toml:"static_files"` } Telnet struct { Address string `toml:"addr"` diff --git a/src/rhctl/main.go b/src/rhctl/main.go index 86e7aec..b0b57e6 100644 --- a/src/rhctl/main.go +++ b/src/rhctl/main.go @@ -104,6 +104,7 @@ func main() { // initializing non-essential parts, like control-interfaces aka clients telnet := TelnetInit(conf, ctrl) + web := WebInit(conf, ctrl) //************************************************** // running essential parts @@ -141,6 +142,14 @@ func main() { }() } + if conf.Clients.Web.Address != "" { + go func() { + rhl.Printf("starting web interface") + web.Run() + rhl.Printf("web interface just stopped") + }() + } + //************************************************** <-stop rhl.Printf("at least one essential part has stopped - bringing down the whole process") diff --git a/src/rhctl/web.go b/src/rhctl/web.go new file mode 100644 index 0000000..b3b35d7 --- /dev/null +++ b/src/rhctl/web.go @@ -0,0 +1,73 @@ +// +// rhctl +// +// Copyright (C) 2009-2016 Christian Pointner +// +// This file is part of rhctl. +// +// rhctl is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// any later version. +// +// rhctl is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with rhctl. If not, see . +// + +package main + +import ( + "encoding/json" + "net/http" + "time" +) + +type WebInterface struct { + server *http.Server +} + +type webHandler struct { + ctrl *SwitchControl + H func(*SwitchControl, http.ResponseWriter, *http.Request) +} + +func (self webHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + self.H(self.ctrl, w, r) +} + +func webSendResponse(w http.ResponseWriter, status int, respdata interface{}) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(status) + encoder := json.NewEncoder(w) + encoder.Encode(respdata) +} + +func webPingHandler(ctrl *SwitchControl, w http.ResponseWriter, r *http.Request) { + webSendResponse(w, http.StatusOK, "pong") +} + +func (web *WebInterface) Run() { + rhdl.Printf("Web: handler running...") + if err := web.server.ListenAndServe(); err != nil { + rhl.Printf("Web: server returned: %s", err) + } +} + +func WebInit(conf *Config, ctrl *SwitchControl) (web *WebInterface) { + web = &WebInterface{} + + if conf.Clients.Web.StaticDir != "" { + rhdl.Printf("Web: will serving static files from '%s'", conf.Clients.Web.StaticDir) + http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir(conf.Clients.Web.StaticDir)))) + } + http.Handle("/ping", webHandler{ctrl, webPingHandler}) + // TODO: add other handler + + web.server = &http.Server{Addr: conf.Clients.Web.Address, ReadTimeout: 2 * time.Hour, WriteTimeout: 2 * time.Hour} + return +} diff --git a/web-static/index.html b/web-static/index.html new file mode 100644 index 0000000..79b83cb --- /dev/null +++ b/web-static/index.html @@ -0,0 +1,8 @@ + + + rhctl web interface + + +

Hello world

+ + -- cgit v0.10.2