-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
75 lines (62 loc) · 1.67 KB
/
main.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
67
68
69
70
71
72
73
74
75
// file: main.go
package main
import (
"log"
"net/http"
"os"
"github.com/joho/godotenv"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/cors"
"github.com/go-chi/jwtauth"
_ "github.com/jinzhu/gorm/dialects/sqlite"
api "github.com/paolomangiadev/mailerbeam/app/api"
"github.com/paolomangiadev/mailerbeam/app/models"
auth "github.com/paolomangiadev/mailerbeam/cmd/auth"
)
// Routes definition
func Routes() *chi.Mux {
router := chi.NewRouter()
// Basic CORS
cors := cors.New(cors.Options{
// AllowedOrigins: []string{"https://foo.com"}, // Use this to allow specific origin hosts
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
ExposedHeaders: []string{"Link"},
AllowCredentials: true,
MaxAge: 300, // Maximum value not ignored by any of major browsers
})
login := auth.Init()
// Router Middlewares
router.Use(
cors.Handler,
middleware.Logger,
middleware.DefaultCompress,
middleware.RedirectSlashes,
middleware.Recoverer,
jwtauth.Verifier(login),
)
// Api Routes
router.Route("/v1", func(r chi.Router) {
r.Use(jwtauth.Authenticator)
r.Mount("/api", api.Routes())
})
// Authentication Routes
router.Mount("/auth", auth.Routes())
return router
}
func main() {
err := godotenv.Load(".env")
if err != nil {
log.Fatal("Error loading .env file")
}
// Init DB & Models
models.Init()
defer models.CloseDB()
port := os.Getenv("PORT")
r := Routes()
http.Handle("/", r)
log.Printf("Server Listening on %v\n", port)
log.Fatal(http.ListenAndServe(port, r))
}