summaryrefslogtreecommitdiff
path: root/src/rhctl/web.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/rhctl/web.go')
-rw-r--r--src/rhctl/web.go73
1 files changed, 73 insertions, 0 deletions
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
+}