-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
98 lines (91 loc) · 2.52 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
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
package main
import (
"encoding/gob"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strconv"
"github.com/yuta-yoshinaga/reversigo/model"
"github.com/gorilla/securecookie"
"github.com/gorilla/sessions"
)
var (
store *sessions.FilesystemStore = sessions.NewFilesystemStore("", securecookie.GenerateRandomKey(64))
sessionName = "cookie-name"
)
func main() {
// ディレクトリを指定する
fs := http.FileServer(http.Dir("static"))
// ルーティング設定。"/"というアクセスがきたらstaticディレクトリのコンテンツを表示させる
http.Handle("/", fs)
// アクセスのルーティングを設定します。
http.HandleFunc("/FrontController", frontController)
log.Println("Listening...")
// サーバーを立ち上げる
port := os.Getenv("PORT")
if port == "" {
port = "80"
}
http.ListenAndServe(":"+port, nil)
}
func frontController(w http.ResponseWriter, r *http.Request) {
store.MaxLength(4 * 1024 * 1024)
gob.Register(model.ReversiPlay{})
gob.Register(model.ReversiSetting{})
gob.Register(model.ReversiPlayInterfaceImpl{})
session, err := store.Get(r, sessionName)
if err != nil {
// 不正なセッションだった場合は作り直す
session, err = store.New(r, sessionName)
}
rpi, ok := session.Values["reversiplay"].(model.ReversiPlay)
fmt.Println(ok)
if ok == false {
rpi = *model.NewReversiPlay()
session.Values["reversiplay"] = rpi
err = session.Save(r, w)
if err != nil {
panic(err)
}
}
rpi.SetmCallbacks(model.NewCallbacksJson())
rpi.SetmDelegate(model.NewReversiPlayDelegate(model.NewReversiPlayInterfaceImpl()))
r.ParseForm()
fmt.Println(r.Form)
function := r.FormValue("func")
res := model.NewResJson()
if function == "setSetting" {
s := model.NewReversiSetting()
jsonText := r.FormValue("para")
if err := json.Unmarshal([]byte(jsonText), &s); err != nil {
log.Fatal(err)
}
rpi.GetmSetting().Copy(s)
rpi.Reset()
res.SetAuth("[SUCCESS]")
} else if function == "reset" {
rpi.Reset()
res.SetAuth("[SUCCESS]")
} else if function == "reversiPlay" {
y, _ := strconv.Atoi(r.FormValue("y"))
x, _ := strconv.Atoi(r.FormValue("x"))
rpi.ReversiPlay(y, x)
res.SetAuth("[SUCCESS]")
}
err = session.Save(r, w)
if err != nil {
panic(err)
}
// jsonヘッダーを出力
w.Header().Set("Content-Type", "application/json")
// jsonエンコード
res.SetCallbacks(rpi.GetmCallbacks())
outputJSONgo, err := json.Marshal(res)
if err != nil {
panic(err)
}
// jsonデータを出力
w.Write(outputJSONgo)
}