From aad1518ceb0ae6ff180c2bf286c83407e0921b13 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Wed, 13 Feb 2019 17:32:23 +0100 Subject: added web handler for language --- cmd/dolmetschctld/statemachine.go | 3 +++ cmd/dolmetschctld/telnet.go | 6 +----- cmd/dolmetschctld/web.go | 34 +++++++++++++++++++++++++++++++--- 3 files changed, 35 insertions(+), 8 deletions(-) (limited to 'cmd') diff --git a/cmd/dolmetschctld/statemachine.go b/cmd/dolmetschctld/statemachine.go index 7082460..9ae5ff3 100644 --- a/cmd/dolmetschctld/statemachine.go +++ b/cmd/dolmetschctld/statemachine.go @@ -178,6 +178,9 @@ func (sm *StateMachine) initMixer() { // the "current language" is what is currently spoken on stage func (sm *StateMachine) setLanguage(l Language) error { + if l == "none" { + l = "" + } if l != "" { if _, exists := sm.languages[l]; !exists { return fmt.Errorf("language '%s' does not exist", l) diff --git a/cmd/dolmetschctld/telnet.go b/cmd/dolmetschctld/telnet.go index 8ea6a61..7bbf552 100644 --- a/cmd/dolmetschctld/telnet.go +++ b/cmd/dolmetschctld/telnet.go @@ -42,11 +42,7 @@ func telnetCmdLang(c *telgo.Client, args []string, sm *StateMachine) bool { return false } - lang := args[1] - if lang == "none" { - lang = "" - } - if err := sm.SetLanguage(Language(lang)); err != nil { + if err := sm.SetLanguage(Language(args[1])); err != nil { c.Sayln("selecting language failed: %v", err) } return false 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 -- cgit v1.2.3