summaryrefslogtreecommitdiff
path: root/cmd/dolmetschctld/web.go
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2019-02-13 17:32:23 +0100
committerChristian Pointner <equinox@spreadspace.org>2019-02-13 17:32:23 +0100
commitaad1518ceb0ae6ff180c2bf286c83407e0921b13 (patch)
tree5686b40db4d71c569d6e68af32cede15dedb268b /cmd/dolmetschctld/web.go
parentadded minimal web interface (diff)
added web handler for language
Diffstat (limited to 'cmd/dolmetschctld/web.go')
-rw-r--r--cmd/dolmetschctld/web.go34
1 files changed, 31 insertions, 3 deletions
diff --git a/cmd/dolmetschctld/web.go b/cmd/dolmetschctld/web.go
index ad42429..ade1e82 100644
--- a/cmd/dolmetschctld/web.go
+++ b/cmd/dolmetschctld/web.go
@@ -24,6 +24,7 @@ package main
import (
"encoding/json"
+ "log"
"net/http"
"time"
)
@@ -57,13 +58,40 @@ func webSendErrorResponse(w http.ResponseWriter, status int, error string) {
webSendResponse(w, status, webError{ResponseCode: status, Error: error})
}
+type webLanguage struct {
+ Lang Language `json:"language"`
+}
+
+func webLanguageHandler(sm *StateMachine, w http.ResponseWriter, r *http.Request) {
+ if r.Method != "PUT" {
+ webSendErrorResponse(w, http.StatusMethodNotAllowed, "only PUT method is allowed")
+ return
+ }
+
+ var result webLanguage
+ dec := json.NewDecoder(r.Body)
+ dec.DisallowUnknownFields()
+ if err := dec.Decode(&result); err != nil {
+ webSendErrorResponse(w, http.StatusBadRequest, err.Error())
+ return
+ }
+
+ log.Printf("web: got request %v", result)
+
+ if err := sm.SetLanguage(result.Lang); err != nil {
+ webSendErrorResponse(w, http.StatusBadRequest, err.Error())
+ return
+ }
+ webSendResponse(w, http.StatusOK, result)
+}
+
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) {
+func webStateHandler(sm *StateMachine, w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
webSendErrorResponse(w, http.StatusMethodNotAllowed, "only GET method is allowed")
return
@@ -81,8 +109,8 @@ func (web *WebInterface) Run() error {
func NewWebInterface(addr string, sm *StateMachine) (web *WebInterface, err error) {
web = &WebInterface{}
- http.Handle("/api/v1/state", webHandler{sm, webGetStateHandler})
- // TODO: add other handler
+ http.Handle("/api/v1/state", webHandler{sm, webStateHandler})
+ http.Handle("/api/v1/language", webHandler{sm, webLanguageHandler})
web.server = &http.Server{Addr: addr, ReadTimeout: 2 * time.Hour, WriteTimeout: 2 * time.Hour}
return