-
Notifications
You must be signed in to change notification settings - Fork 55
/
server.go
55 lines (45 loc) · 791 Bytes
/
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
package main
import (
"log"
"net/http"
"github.com/kataras/iris"
)
type user struct {
ID string `json:"id"`
Name string `json:"name"`
}
type application struct {
iris *iris.Application
}
func newApp() *application {
app := iris.Default()
app.Get("/user/{id}", getUser)
if err := app.Build(); err != nil {
panic(err)
}
return &application{
iris: app,
}
}
func (a *application) start() {
log.Fatal(a.iris.Run(iris.Addr("localhost:8080")))
}
func getUser(ctx iris.Context) {
ctx.SetCookie(&http.Cookie{
Name: "TomsFavouriteDrink",
Value: "Beer",
Path: "/",
})
id := ctx.Params().Get("id")
if id == "1234" {
_, e := ctx.JSON(&user{ID: id, Name: "Andy"})
if e != nil {
panic(e)
}
} else {
ctx.NotFound()
}
}
func main() {
newApp().start()
}