-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
42 lines (32 loc) · 921 Bytes
/
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
package main
import (
"fmt"
"log"
"github.com/sampsonbryce/go-webserver/server"
)
const (
connHost = "localhost"
connPort = "8080"
)
type Person struct {
Id int `json"id"`
Name string `json:"name"`
}
func main() {
options := server.ServerOptions{Host: connHost, Port: connPort}
s := server.CreateServer(&options)
s.HandleFunc("/json", func(request server.Request) server.Response {
if request.Method == "GET" {
p := Person{Id: 1, Name: "Dave"}
return server.CreateResponse().SetStatus(200).SetJson(p)
} else if request.Method == "POST" {
return server.CreateResponse().SetStatus(204)
}
return server.CreateResponse().SetStatus(404)
})
s.HandleFunc("/normal", func(request server.Request) server.Response {
return server.CreateResponse().SetStatus(200).SetBody([]byte("helloworld"))
})
fmt.Println("Starting server on " + connHost + ":" + connPort)
log.Fatal(s.Listen())
}