-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
128 lines (98 loc) · 3.15 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
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
package main
import (
"encoding/json"
"log"
"net/http"
"os"
"time"
"github.com/julienschmidt/httprouter"
"github.com/peter-mueller/sit-o-mat/httperror"
"github.com/peter-mueller/sit-o-mat/sitomat"
"github.com/peter-mueller/sit-o-mat/user"
"github.com/peter-mueller/sit-o-mat/workplace"
"fmt"
_ "gocloud.dev/docstore/gcpfirestore"
_ "gocloud.dev/docstore/memdocstore"
"errors"
)
func main() {
userService := user.Service{}
userController := user.Controller{Service: &userService}
workplaceService := workplace.Service{}
workplaceController := workplace.Controller{Service: &workplaceService}
sitomatService := sitomat.Service{
UserService: &userService,
WorkplaceService: &workplaceService,
}
sitomatController := sitomat.Controller{Service: &sitomatService}
r := httprouter.New()
r.HandleOPTIONS = true
r.HandleMethodNotAllowed = true
r.GlobalOPTIONS = http.HandlerFunc(corsHandler)
r.POST("/user", userController.RegisterUser)
r.GET("/user/:name", userController.GetUser)
r.DELETE("/user/:name", userController.DeleteUser)
r.PATCH("/user/:name", userController.PatchUser)
r.POST("/workplace", workplaceController.CreateWorkplace)
r.DELETE("/workplace/:name", workplaceController.DeleteWorkplace)
r.GET("/workplace", workplaceController.GetAllWorkplaces)
r.GET("/sitomat", sitomatController.ManualAssign)
r.GET("/health", healthHandler)
r.PanicHandler = panicHandler
fmt.Println("Starting Server")
srv := &http.Server{
Handler: corsDecorator{r},
Addr: lookupEnv("SITOMAT_ADDR", "127.0.0.1:8080"),
// Good practice: enforce timeouts for servers you create!
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
fmt.Println(" Addr: ", srv.Addr)
log.Fatal(srv.ListenAndServe())
}
func panicHandler(w http.ResponseWriter, r *http.Request, data interface{}) {
err, ok := data.(error)
if !ok {
w.WriteHeader(http.StatusInternalServerError)
return
}
var httperr httperror.Error
if errors.As(err, &httperr) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(httperr.Status)
json.NewEncoder(w).Encode(httperr)
return
}
w.WriteHeader(http.StatusInternalServerError)
}
func corsHandler(w http.ResponseWriter, r *http.Request) {
// Set CORS headers
header := w.Header()
header.Set("Access-Control-Allow-Methods", "GET, POST, DELETE, PATCH, PUT")
header.Set("Access-Control-Allow-Headers", "authorization")
header.Set("Access-Control-Allow-Origin", "*")
// Adjust status code to 204
w.WriteHeader(http.StatusNoContent)
}
type corsDecorator struct {
router *httprouter.Router
}
func healthHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode("UP")
}
func (c corsDecorator) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if origin := r.Header.Get("Origin"); origin != "" {
w.Header().Set("Access-Control-Allow-Origin", origin)
}
c.router.ServeHTTP(w, r)
}
func lookupEnv(env string, alternative string) string {
value, ok := os.LookupEnv(env)
if !ok {
log.Printf("Using default for %v: %v", env, alternative)
return alternative
}
return value
}