/
server.go
66 lines (52 loc) · 1.74 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
61
62
63
64
65
66
package server
import (
"log"
"net/http"
"github.com/julienschmidt/httprouter"
"github.com/pysf/special-umbrella/internal/config"
"github.com/pysf/special-umbrella/internal/scooter"
"github.com/pysf/special-umbrella/internal/scooter/scooteriface"
"go.mongodb.org/mongo-driver/mongo"
)
type httpHandlerFunc func(http.ResponseWriter, *http.Request, httprouter.Params) error
type Server struct {
ScooterReserver scooteriface.ScooterReserver
StatusUpdater scooteriface.StatusUpdater
ScooterFinder scooteriface.ScooterFinder
HttpHandler http.Handler
jwtTokenKey string
}
func NewServer(DB *mongo.Database) (*Server, error) {
scooterReserver, err := scooter.NewScooterReserver(DB)
if err != nil {
panic(err)
}
statusUpdater, err := scooter.NewStatusUpdater(scooterReserver, DB)
if err != nil {
panic(err)
}
scooterFinder, err := scooter.NewScooterFinder(DB)
if err != nil {
panic(err)
}
server := &Server{
StatusUpdater: statusUpdater,
ScooterFinder: scooterFinder,
ScooterReserver: scooterReserver,
jwtTokenKey: config.AppConf().JWTTokenKey,
}
server.addRoutes()
return server, nil
}
func (s *Server) addRoutes() {
httpRouter := httprouter.New()
httpRouter.POST("/api/scooter/status", s.wrapWithErrorHandler(s.wrapWithAuthenticator(s.AddScooterStatus)))
httpRouter.PUT("/api/scooter/reserve", s.wrapWithErrorHandler(s.wrapWithAuthenticator(s.ReserveScooter)))
httpRouter.PUT("/api/scooter/release", s.wrapWithErrorHandler(s.wrapWithAuthenticator(s.ReleaseScooter)))
httpRouter.GET("/api/scooter/search", s.wrapWithErrorHandler(s.wrapWithAuthenticator(s.FindScooter)))
s.HttpHandler = httpRouter
}
func (s Server) Start() error {
log.Println("Starting...")
return http.ListenAndServe(":8080", s.HttpHandler)
}