diff options
author | Christian Pointner <equinox@spreadspace.org> | 2019-02-13 17:18:45 +0100 |
---|---|---|
committer | Christian Pointner <equinox@spreadspace.org> | 2019-02-13 17:18:45 +0100 |
commit | 7cc799e9d7c416854eb9935f4369d5ac8e1486b7 (patch) | |
tree | c99484ac46a1853f1d78ea98706f13da4d85a148 /cmd/dolmetschctld/web.go | |
parent | more state (diff) |
added minimal web interface
Diffstat (limited to 'cmd/dolmetschctld/web.go')
-rw-r--r-- | cmd/dolmetschctld/web.go | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/cmd/dolmetschctld/web.go b/cmd/dolmetschctld/web.go new file mode 100644 index 0000000..ad42429 --- /dev/null +++ b/cmd/dolmetschctld/web.go @@ -0,0 +1,89 @@ +// +// dolmetschctl +// +// +// Copyright (C) 2019 Christian Pointner <equinox@spreadspace.org> +// +// This file is part of dolmetschctl. +// +// dolmetschctl 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. +// +// dolmetschctl 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 dolmetschctl. 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 { + sm *StateMachine + H func(*StateMachine, http.ResponseWriter, *http.Request) +} + +func (self webHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + self.H(self.sm, 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) +} + +type webError struct { + ResponseCode int `json:"response-code"` + Error string `json:"error"` +} + +func webSendErrorResponse(w http.ResponseWriter, status int, error string) { + webSendResponse(w, status, webError{ResponseCode: status, Error: error}) +} + +type webState struct { + State State `json:"state"` + Ratio float32 `json:"original-to-interpreter-ratio"` + Lang Language `json:"language"` +} + +func webGetStateHandler(sm *StateMachine, w http.ResponseWriter, r *http.Request) { + if r.Method != "GET" { + webSendErrorResponse(w, http.StatusMethodNotAllowed, "only GET method is allowed") + return + } + + var result webState + result.State, result.Ratio, result.Lang = sm.GetState() + webSendResponse(w, http.StatusOK, result) +} + +func (web *WebInterface) Run() error { + return web.server.ListenAndServe() +} + +func NewWebInterface(addr string, sm *StateMachine) (web *WebInterface, err error) { + web = &WebInterface{} + + http.Handle("/api/v1/state", webHandler{sm, webGetStateHandler}) + // TODO: add other handler + + web.server = &http.Server{Addr: addr, ReadTimeout: 2 * time.Hour, WriteTimeout: 2 * time.Hour} + return +} |