-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
49 lines (41 loc) · 1.08 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
package main
import (
"fmt"
"log"
"net/http"
"github.com/pierreprinetti/apimock/store"
)
func newRouter(resources router) http.Handler {
get := getHandler(resources)
put := putHandler(resources)
del := deleteHandler(resources)
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
switch req.Method {
case http.MethodGet:
get(rw, req)
case http.MethodPut:
put(rw, req)
case http.MethodDelete:
del(rw, req)
case http.MethodOptions:
optionsHandler(rw, req)
default:
msg := fmt.Sprintf("HTTP %s handler not implemented.", req.Method)
log.Println(msg)
http.Error(rw, msg, http.StatusNotImplemented)
}
})
}
func main() {
resources := store.New(
store.WithDefaultContentType(getenv("DEFAULT_CONTENT_TYPE", "text/plain")),
store.WithContentTypeOverride(getenv("FORCED_CONTENT_TYPE", "")),
)
apimock := newRouter(resources)
withCorsHeaders := newCors(apimock)
withLogging := newLogger(withCorsHeaders)
if err := http.ListenAndServe(
getenv("HOST", ":"+getenv("PORT", "8800")), withLogging); err != nil {
log.Fatal(err)
}
}