summaryrefslogtreecommitdiff
path: root/cmd/dolmetschctld/web.go
blob: 696169f8076328b24042d51894cc86097d553bb6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//
//  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 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
	}

	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 webStateHandler(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, staticDir string, sm *StateMachine) (web *WebInterface, err error) {
	web = &WebInterface{}

	if staticDir != "" {
		http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir(staticDir))))
	}

	http.Handle("/api/v1/state", webHandler{sm, webStateHandler})
	http.Handle("/api/v1/language", webHandler{sm, webLanguageHandler})

	http.Handle("/api/v1/socket", webHandler{sm, webSocketHandler})

	web.server = &http.Server{Addr: addr, ReadTimeout: 2 * time.Hour, WriteTimeout: 2 * time.Hour}
	return
}