diff options
author | Christian Pointner <equinox@spreadspace.org> | 2019-02-13 17:32:23 +0100 |
---|---|---|
committer | Christian Pointner <equinox@spreadspace.org> | 2019-02-13 17:32:23 +0100 |
commit | aad1518ceb0ae6ff180c2bf286c83407e0921b13 (patch) | |
tree | 5686b40db4d71c569d6e68af32cede15dedb268b /cmd/dolmetschctld | |
parent | added minimal web interface (diff) |
added web handler for language
Diffstat (limited to 'cmd/dolmetschctld')
-rw-r--r-- | cmd/dolmetschctld/statemachine.go | 3 | ||||
-rw-r--r-- | cmd/dolmetschctld/telnet.go | 6 | ||||
-rw-r--r-- | cmd/dolmetschctld/web.go | 34 |
3 files changed, 35 insertions, 8 deletions
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 |