-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.go
42 lines (34 loc) · 1.05 KB
/
app.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
package main
import (
"database/sql"
"log"
"net/http"
"github.com/jcprz/jwtapp/controllers"
"github.com/jcprz/jwtapp/database"
"github.com/go-redis/redis"
"github.com/gorilla/mux"
)
type App struct {
DB *sql.DB
RDS *redis.Client
Router *mux.Router
}
func (a *App) Initialize() {
a.DB = database.ConnectDB()
a.RDS = database.ConnectRedis()
database.EnsureTableExists(a.DB)
a.Router = mux.NewRouter()
a.initializeRoutes()
}
func (a *App) initializeRoutes() {
controller := controllers.Controller{}
a.Router.HandleFunc("/signup", controller.Signup(a.DB)).Methods("POST")
a.Router.HandleFunc("/login", controller.Login(a.DB, a.RDS)).Methods("POST")
a.Router.HandleFunc("/delete", controller.Delete(a.DB, a.RDS)).Methods("DELETE")
a.Router.HandleFunc("/protected", controller.TokenVerifyMiddleware(controller.ProtectedEndpoint())).Methods("GET")
a.Router.HandleFunc("/healthz", controller.HealthZ()).Methods("GET")
}
func (a *App) Run(port string) {
log.Printf("Listening on port: %s\n", port[1:])
log.Fatal(http.ListenAndServe(port, a.Router))
}