/
server.go
156 lines (127 loc) · 5.37 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
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
156
package main
import (
"code.google.com/p/gorest"
"fmt"
//"github.com/mattn/go-sqlite3"
"net"
"net/http/fcgi"
)
func main() {
fmt.Println("Hello!")
listener, err := net.Listen("tcp", ":8080")
if err != nil {
fmt.Println("Error!")
fmt.Println(err)
}
// srv := http.NewServeMux()
// srv.HandleFunc("/", gorest.Handle())
fcgi.Serve(listener, gorest.Handle())
}
// This function will keep the user data synced with the sqlite DB
func sync() {
}
//************************Define Service***************************
type DefaultService struct {
gorest.RestService `root:"/"`
index gorest.EndPoint `method:"GET" path:"/" output:"srting"`
authenticate gorest.EndPoint `method:"POST" path:"/authenticate/" postdata:"authUser" output:"string"`
}
type PokerService struct {
//Service level config
gorest.RestService `root:"/poker/" consumes:"application/json" produces:"application/json"`
//End-Point level configs: Field names must be the same as the corresponding method names,
// but not-exported (starts with lowercase)
tableList gorest.EndPoint `method:"GET" path:"/table/" output:"[]Table"`
tableDetails gorest.EndPoint `method:"GET" path:"/table/{Id:int}" output:"Table"`
playerList gorest.EndPoint `method:"GET" path:"/player/" output:"[]Player"`
playerCreate gorest.EndPoint `method:"POST" path:"/player/" postdata:"Player"`
playerDetails gorest.EndPoint `method:"GET" path:"/player/{Id:int}" output:"Player"`
//playerStats gorest.EndPoint `method:"GET" path:"/player/{Id:int}/" output:"PlayerStats"`
}
type UserService struct {
gorest.RestService `root:"/user/" consumes:"application/json" produces:"application/json"`
userDetails gorest.EndPoint `method:"GET" path:"/{Id:int}" output:"User"`
userCreate gorest.EndPoint `method:"POST" path:"/" postdata:"User"`
}
func (serv DefaultService) Authenticate() string {
fmt.Println(serv)
return "Hello!"
}
//Handler Methods: Method names must be the same as in config, but exported (starts with uppercase)
func (serv PokerService) TableDetails(Id int) (t Table) {
return tableStore[Id]
//serv.ResponseBuilder().SetResponseCode(404).Overide(true) //Overide causes the entity returned by the method to be ignored. Other wise it would send back zeroed object
//return
}
func (serv PokerService) TableList() []Table {
serv.ResponseBuilder().CacheMaxAge(60 * 60 * 24) //List cacheable for a day. More work to come on this, Etag, etc
return tableStore
}
func (serv PokerService) PlayerDetails(Id int) (p Player) {
return playerStore[Id]
// serv.ResponseBuilder().SetResponseCode(404).Overide(true) //Overide causes the entity returned by the method to be ignored. Other wise it would send back zeroed object
// return
}
func (serv PokerService) PlayerList() []Player {
serv.ResponseBuilder().CacheMaxAge(60 * 60 * 24) //List cacheable for a day. More work to come on this, Etag, etc
return playerStore
}
// func(serv OrderService) AddItem(i Item){
// for _,item:=range itemStore{
// if item.Id == i.Id{
// item=i
// serv.ResponseBuilder().SetResponseCode(200) //Updated http 200, or you could just return without setting this. 200 is the default for POST
// return
// }
// }
// //Item Id not in database, so create new
// i.Id = len(itemStore)
// itemStore=append(itemStore,i)
// serv.ResponseBuilder().Created("http://localhost:8787/orders-service/items/"+string(i.Id)) //Created, http 201
// }
// //On the method parameters, the posted data(http-entity) is always first, followed by the URL mapped parameters
// func(serv OrderService) PlaceOrder(order Order,UserId int,AskForDiscount bool){
// order.Id = len(orderStore)
// if user,found:= userStore[UserId];found{
// if item,exists:=findItem(order.ItemId);exists{
// itemStore[item.Id].AvailableStock--
// if AskForDiscount && order.Amount >5{
// order.Discount = 2.5
// }
// order.Id=len(orderStore)
// order.UserId=UserId
// order.Cancelled=false
// orderStore=append(orderStore,order)
// user.OrderIds=append(user.OrderIds,order.Id)
// userStore[user.Id]=user
// serv.ResponseBuilder().SetResponseCode(201).Location("http://localhost:8787/orders-service/orders/"+string(order.Id))//Created
// return
// } else{
// serv.ResponseBuilder().SetResponseCode(404).WriteAndOveride([]byte("Item not found"))//You can still manually place an entity on the response, even on a POST
// return
// }
// }
// serv.ResponseBuilder().SetResponseCode(404).WriteAndOveride([]byte("User not found"))
// return
// }
// func(serv OrderService) ViewOrder(id int) (retOrder Order){
// for _,order:=range orderStore{
// if order.Id == id{
// retOrder = order
// return
// }
// }
// serv.ResponseBuilder().SetResponseCode(404).Overide(true)
// return
// }
// func(serv OrderService) DeleteOrder(id int) {
// for pos,order:=range orderStore{
// if order.Id == id{
// order.Cancelled =true
// orderStore[pos]=order
// return //Default http code for DELETE is 200
// }
// }
// serv.ResponseBuilder().SetResponseCode(404).Overide(true)
// return
// }