-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
66 lines (49 loc) · 1.24 KB
/
main.go
File metadata and controls
66 lines (49 loc) · 1.24 KB
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 main
import (
"os"
"log"
"net/http"
"github.com/sharizzle/go-api-template/db"
"github.com/gorilla/mux"
"github.com/sharizzle/go-api-template/service/user"
customRouter "github.com/sharizzle/go-api-template/router"
"github.com/sharizzle/go-api-template/middleware"
"github.com/joho/godotenv"
)
func NewRouter() *mux.Router {
//init router
router := mux.NewRouter()
//append user routes
customRouter.AppRoutes = append(customRouter.AppRoutes, user.Routes)
for _, route := range customRouter.AppRoutes {
//create subroute
routePrefix := router.PathPrefix(route.Prefix).Subrouter()
//loop through each sub route
for _, r := range route.SubRoutes {
var handler http.Handler
handler = r.HandlerFunc
//check to see if route should be protected with jwt
if r.Protected {
handler = middleware.JWTMiddleware(r.HandlerFunc)
}
//attach sub route
routePrefix.
Path(r.Pattern).
Handler(handler).
Methods(r.Method).
Name(r.Name)
}
}
return router
}
func main(){
//init router
godotenv.Load(".env")
port := os.Getenv("PORT")
router := NewRouter()
//Setup database
db.DB = db.SetupDB()
db.DB.AutoMigrate(&user.User{})
//create http server
log.Fatal(http.ListenAndServe(":"+port, router))
}