-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
37 lines (30 loc) · 1011 Bytes
/
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
package handler
import (
"net/http"
"github.com/go-chi/chi"
"github.com/go-chi/render"
"github.com/guilhermecostam/sys-finance/database"
)
// dbInstance is a instance for database
var dbInstance database.Database
// NewHandler is a method for create a new handle
func NewHandler(db database.Database) http.Handler {
router := chi.NewRouter()
dbInstance = db
router.MethodNotAllowed(methodNotAllowedHandler)
router.NotFound(notFoundHandler)
router.Route("/transactions", transactions)
return router
}
// methodNotAllowedHandler is a method indicate if handle as not allowed
func methodNotAllowedHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-type", "application/json")
w.WriteHeader(405)
render.Render(w, r, ErrMethodNotAllowed)
}
// notFoundHandler is a method indicate if handle as not found
func notFoundHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-type", "application/json")
w.WriteHeader(400)
render.Render(w, r, ErrNotFound)
}