summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Pointner <equinox@helsinki.at>2016-09-24 15:34:22 (GMT)
committerChristian Pointner <equinox@helsinki.at>2016-09-24 15:34:22 (GMT)
commita580e5f3b54889fdd1441be68af44f57a3797807 (patch)
treeb6f87766120c9ed7555019040f26b122f7f0e8a2 /src
parentf83426f19c7f12ea161c45e8bda9e21db22959de (diff)
added basic web interface
Diffstat (limited to 'src')
-rw-r--r--src/rhctl/conf.go3
-rw-r--r--src/rhctl/main.go9
-rw-r--r--src/rhctl/web.go73
3 files changed, 84 insertions, 1 deletions
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 <equinox@helsinki.at>
+//
+// 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 <http://www.gnu.org/licenses/>.
+//
+
+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
+}