-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
54 lines (48 loc) · 1.22 KB
/
main.go
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
/**
* ウェブベースの電卓プログラムを書きなさい。
*/
package main
import (
"./eval"
"encoding/json"
"fmt"
"html/template"
"log"
"net/http"
"os"
)
func calcHandler(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
strExpr := q.Get("expr")
if strExpr == "" {
fmt.Fprintf(w, "Please enter the formula in the URL\nEx: http://localhost:8000/?expr=a*b&env={\"a\":2,\"b\":4} (Ex means 2 * 4.)")
return
}
expr, err := eval.Parse(strExpr)
if err != nil {
fmt.Fprintf(w, "Invalid expr(%s). [%v]\n", strExpr, err)
return
}
vars := make(map[eval.Var]bool)
if err = expr.Check(vars); err != nil {
fmt.Fprintf(w, "expr check() error. [%v]\n", err)
return
}
env := make(eval.Env)
strEnv := q.Get("env")
if strEnv != "" { // env is optional.
if err = json.Unmarshal([]byte(strEnv), &env); err != nil {
fmt.Fprintf(w, "Invalid env expression. err[%v]\n", err)
return
}
}
result := expr.Eval(env)
tpl := template.Must(template.ParseFiles("./template/index.html.tpl"))
if err := tpl.Execute(w, result); err != nil {
fmt.Fprintf(os.Stderr, "tpl.Execute error [%v]\n", err)
}
}
func main() {
http.HandleFunc("/", calcHandler)
log.Fatal(http.ListenAndServe("localhost:8000", nil))
}