-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
60 lines (49 loc) · 1.58 KB
/
server.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
package main
import (
"log"
"net/http"
"github.com/gorilla/mux"
"github.com/ramajd/events-api/handlers"
"github.com/ramajd/events-api/store"
)
type Args struct {
// postgres connection string, of the form,
// e.g "postgres://user:password@localhost:5432/database?sslmode=disable
conn string
// port for the server of the form,
// e.g ":8080"
port string
}
func Run(args Args) error {
router := mux.NewRouter().
PathPrefix("/api/v1/").
Subrouter()
st := store.NewPostgresEventStore(args.conn)
hnd := handlers.NewEventHandler(st)
RegisterAllRoutes(router, hnd)
log.Println("Starting server at port: " + args.port)
return http.ListenAndServe(args.port, router)
}
func RegisterAllRoutes(router *mux.Router, hnd handlers.IEventHandler) {
// set content type
router.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
next.ServeHTTP(w, r)
})
})
// get events
router.HandleFunc("/event", hnd.Get).Methods(http.MethodGet)
// create events
router.HandleFunc("/event", hnd.Create).Methods(http.MethodPost)
// delete event
router.HandleFunc("/event", hnd.Delete).Methods(http.MethodDelete)
// cancel event
router.HandleFunc("/event/cancel", hnd.Cancel).Methods(http.MethodPatch)
// update event details
router.HandleFunc("/event/details", hnd.UpdateDetails).Methods(http.MethodPut)
// reschedule event
router.HandleFunc("/event/reschedule", hnd.Reschedule).Methods(http.MethodPatch)
// list event
router.HandleFunc("/events", hnd.List).Methods(http.MethodGet)
}