/
iching_api.go
52 lines (42 loc) · 1.25 KB
/
iching_api.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
package main
import (
"github.com/hoisie/web"
"github.com/abrookins/go_iching"
"fmt"
"encoding/json"
"strconv"
)
func reading(ctx *web.Context) string {
ctx.Request.ParseForm()
question := ctx.Request.Form.Get("question")
ctx.ContentType("json")
if question == "" {
question = "No question provided"
}
reading := iching.GetReading(question)
js, _ := json.Marshal(reading)
return string(js)
}
func hexagram(ctx *web.Context, num string) string {
n, _ := strconv.Atoi(num)
hexagram, found := iching.GetHexagramByNum(n)
ctx.ContentType("json")
if found {
js, _ := json.Marshal(hexagram)
return string(js)
}
return "Hexagram not found"
}
func allHexagrams(ctx *web.Context) string {
hexagrams := iching.GetAllHexagrams()
ctx.ContentType("json")
js, _ := json.Marshal(hexagrams)
return string(js)
}
func main() {
fmt.Printf("Starting server ... ")
web.Get("/iching/hexagrams/(.*)", hexagram)
web.Get("/iching/hexagrams", allHexagrams)
web.Get("/iching/reading", reading)
web.Run("0.0.0.0:9999")
}