summaryrefslogtreecommitdiff
path: root/cmd/dolmetschctld/web.go
diff options
context:
space:
mode:
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