-
Notifications
You must be signed in to change notification settings - Fork 0
/
swift.go
155 lines (128 loc) · 2.5 KB
/
swift.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package swift
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"strings"
"time"
)
func Init() {
http.HandleFunc("/", HandleRequest)
cmds = make(map[string]Command)
}
func HandleRequest(w http.ResponseWriter, r *http.Request) {
cmd := NewCommand(w, r)
defer cmd.WriteResponse()
err := cmd.ReadRequest()
if err != nil {
return
}
cmds[cmd.Name()].Handle()
}
// Store all cmds info
// r.URL.Path => Name. For example, "/user" => "User"
var cmds map[string]Command
func RegisterCommand(path string, cmd Command) {
cmds[path] = cmd
}
// Every command should implement this interface
type Command interface {
Name() string
NewReq() interface{}
NewResp() interface{}
New(base *BaseCommand) Command
ReadRequest() error
Handle()
WriteResponse()
}
type BaseCommand struct {
r *http.Request
w http.ResponseWriter
Req interface{}
Resp interface{}
Cmd string // interface name, that is, last path in r.URL.Path.
// For time tracking
Start time.Time
}
type UnknownCommand struct {
BaseCommand
}
func (this *UnknownCommand) Handle() {
this.Resp = &struct{
Code int
Msg string
}{1000, "invalid interface"}
}
func (this *UnknownCommand) New(base *BaseCommand) Command {
return &UnknownCommand{
*base,
}
}
func NewCommand(w http.ResponseWriter, r *http.Request) Command {
// Both of path "/test" and "/test/" correspond to "/test" command
path := r.URL.Path
if strings.HasSuffix(path, "/") {
path = strings.TrimSuffix(path, "/")
}
if cmds[path] == nil {
return nil
}
baseCmd := BaseCommand{
r: r,
w: w,
Cmd: path,
Start: time.Now(),
Req: cmds[path].NewReq(),
Resp: cmds[path].NewResp(),
}
var cmd Command
cmdTemplate, ok := cmds[path]
if !ok {
cmd = &UnknownCommand{
BaseCommand{
r: r,
w: w,
},
}
} else {
cmd = cmdTemplate.New(&baseCmd)
}
return cmd
}
func (this *BaseCommand) Name() string {
return ""
}
func (this *BaseCommand) NewReq() interface{} {
return nil
}
func (this *BaseCommand) NewResp() interface{} {
return nil
}
func (this *BaseCommand) ReadRequest() error {
cmd := cmds[this.Cmd].Name()
if cmd == "" {
return errors.New("invalid request")
}
data, err := ioutil.ReadAll(this.r.Body)
if err != nil {
return err
}
err = json.Unmarshal(data, this.Req)
if err != nil {
return err
}
return nil
}
func (this *BaseCommand) Handle() {
}
func (this *BaseCommand) WriteResponse() {
if this.Resp == nil {
return
}
data, err := json.Marshal(this.Resp)
if err != nil {
return
}
this.w.Write(data)
}