/
server.go
127 lines (96 loc) · 2.63 KB
/
server.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strconv"
"github.com/go-martini/martini"
"github.com/justone/go-minibus"
"github.com/martini-contrib/cors"
// "github.com/martini-contrib/staticbin"
)
type Payment struct {
Location string `json:"location"`
Amount float64 `json:"amount"`
}
func (this Payment) Filter() interface{} {
return this
}
type Data struct {
StartAmount float64 `json:"start-amount"`
Payments []Payment `json:"previous-payments"`
}
func (this Data) Filter() interface{} {
return this
}
type IncomingMessage struct {
Type string `json:"type"`
Args map[string]string `json:"args"`
}
func main() {
m := martini.New()
route := martini.NewRouter()
m.Use(cors.Allow(&cors.Options{
AllowOrigins: []string{"*"},
AllowMethods: []string{"GET", "PATCH"},
AllowHeaders: []string{"Origin"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
}))
// generated with "go-bindata -o ../../static.go public/..."
// m.Use(staticbin.Static("public", Asset))
mb := minibus.Init()
payments := make([]Payment, 0)
initialAmount := 513.22
route.Get("/conn/:cust/:conn", func(res http.ResponseWriter, params martini.Params) {
cust := params["cust"]
conn := params["conn"]
message, err := mb.Receive(cust, conn)
if err != nil {
fmt.Fprintln(res, "{}")
} else {
fmt.Fprintln(res, message.Contents)
}
})
route.Post("/conn/:cust", func(res http.ResponseWriter, req *http.Request, params martini.Params) {
cust := params["cust"]
body, err := ioutil.ReadAll(req.Body)
if err != nil {
http.Error(res, "Unable to read request body: "+err.Error(), http.StatusInternalServerError)
return
}
fmt.Println("Got: ", string(body))
var incoming IncomingMessage
err = json.Unmarshal(body, &incoming)
if err != nil {
http.Error(res, "Bad request: "+err.Error(), http.StatusBadRequest)
return
}
if incoming.Type == "add-payment" {
fmt.Println("Location : ", incoming.Args["location"])
fmt.Println("Amount : ", incoming.Args["amount"])
amount, _ := strconv.ParseFloat(incoming.Args["amount"], 64)
payments = append(payments, Payment{incoming.Args["location"], amount})
coredata := Data{
initialAmount,
payments,
}
json, _ := json.Marshal(coredata)
fmt.Println(string(json))
mb.Send(cust, string(json))
} else if incoming.Type == "load-info" {
coredata := Data{
initialAmount,
payments,
}
json, _ := json.Marshal(coredata)
fmt.Println(string(json))
mb.Send(cust, string(json))
}
})
m.Action(route.Handle)
log.Println("Waiting for connections...")
m.Run()
}