-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
89 lines (71 loc) · 1.5 KB
/
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
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
package main
import (
"errors"
"fmt"
"github.com/go-chi/chi"
"log"
"net/http"
"os"
"strconv"
)
var port = os.Getenv("PORT")
type server struct {
router *chi.Mux
}
func main() {
if port == "" {
port = "5000"
}
if err := checkPort(port); err != nil {
log.Fatalln(err)
}
r := chi.NewRouter()
s := server{router: r}
address := fmt.Sprintf(":%s", port)
log.Printf("listening on %s\n", address)
if err := http.ListenAndServe(address, s.router); err != nil {
log.Fatalln(err)
}
}
func (s *server) healthz() http.HandlerFunc {
return func(resp http.ResponseWriter, req *http.Request) {
resp.Header().Set("content-type", "text/plain")
if _, err := resp.Write([]byte("OK")); err != nil {
fmt.Println(err)
}
}
}
func (s *server) putEntry() http.HandlerFunc {
return nil
}
func (s *server) getEntry() http.HandlerFunc {
return nil
}
func (s *server) getMap() http.HandlerFunc {
return nil
}
func (s *server) deleteEntry() http.HandlerFunc {
return nil
}
func (s *server) installRoutes() {
s.router.Get("/healthz", s.healthz())
s.router.Route("/maps/{mapName}", func(r chi.Router) {
r.Get("/", s.getMap())
r.Post("/", s.putEntry())
r.Route("/{key}", func(r chi.Router) {
r.Get("/", s.getEntry())
r.Put("/", s.putEntry())
r.Delete("/", s.deleteEntry())
})
})
}
func checkPort(port string) error {
p, err := strconv.Atoi(port)
if err != nil {
return err
}
if p < 0 || p > 65535 {
return errors.New("PORT is out of acceptable range [0..65535]")
}
return nil
}