/
handler.go
59 lines (47 loc) · 1.76 KB
/
handler.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
package gofr
import (
"gofr.dev/pkg/gofr/container"
gofrHTTP "gofr.dev/pkg/gofr/http"
"gofr.dev/pkg/gofr/http/response"
"gofr.dev/pkg/gofr/static"
"net/http"
)
type Handler func(c *Context) (interface{}, error)
/*
Developer Note: There is an implementation where we do not need this internal handler struct
and directly use Handler. However, in that case the container dependency is not injected and
has to be created inside ServeHTTP method, which will result in multiple unnecessary calls.
This is what we implemented first.
There is another possibility where we write our own Router implementation and let httpServer
use that router which will return a Handler and httpServer will then create the context with
injecting container and call that Handler with the new context. A similar implementation is
done in CMD. Since this will require us to write our own router - we are not taking that path
for now. In the future, this can be considered as well if we are writing our own http router.
*/
type handler struct {
function Handler
container *container.Container
}
func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
c := newContext(gofrHTTP.NewResponder(w, r.Method), gofrHTTP.NewRequest(r), h.container)
defer c.Trace("gofr-handler").End()
c.responder.Respond(h.function(c))
}
func healthHandler(c *Context) (interface{}, error) {
return c.Health(c), nil
}
func liveHandler(*Context) (interface{}, error) {
return struct {
Status string `json:"status"`
}{Status: "UP"}, nil
}
func faviconHandler(*Context) (interface{}, error) {
data, err := static.Files.ReadFile("favicon.ico")
return response.File{
Content: data,
ContentType: "image/x-icon",
}, err
}
func catchAllHandler(*Context) (interface{}, error) {
return nil, http.ErrMissingFile
}