-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
114 lines (91 loc) · 2.74 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"fmt"
"net/http"
"os"
"os/signal"
"strconv"
"syscall"
api "github.com/paoloposso/bands-auth-api/api"
"github.com/paoloposso/bands-auth-api/infrastructure/tokenization"
"github.com/paoloposso/bands-auth-api/user"
repositorymongodb "github.com/paoloposso/bands-auth-api/infrastructure/repository/mongodb"
_ "github.com/paoloposso/bands-auth-api/docs"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/cors"
"github.com/golobby/container"
"github.com/joho/godotenv"
httpSwagger "github.com/swaggo/http-swagger"
)
// @title Bands Auth API
// @version 1.0
// @description Authentication and Registration API.
// @BasePath /api/v1
func main() {
_ = godotenv.Load()
router := chi.NewRouter()
router.Use(middleware.RequestID)
router.Use(middleware.RealIP)
router.Use(middleware.Logger)
router.Use(middleware.Recoverer)
router.Use(cors.Handler(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
ExposedHeaders: []string{"Link"},
AllowCredentials: false,
MaxAge: 300, // Maximum value not ignored by any of major browsers
}))
router.Get("/swagger/*", httpSwagger.Handler(
httpSwagger.URL("/swagger/doc.json"), //The url pointing to API definition"
))
injectDependencies(router)
errs := make(chan error, 2)
port := getHttpPort()
go func() {
fmt.Println("Listening on port ", port)
errs <- http.ListenAndServe(port, router)
}()
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT)
errs <- fmt.Errorf("%s", <-c)
}()
fmt.Println("Terminated ", <-errs)
}
func getHttpPort() string {
port := "8000"
if os.Getenv("PORT") != "" {
port = os.Getenv("PORT")
}
return fmt.Sprintf(":%s", port)
}
func injectDependencies(router *chi.Mux) {
container.Transient(func() user.Repository {
mongoURL := os.Getenv("MONGO_URL")
mongoDB := os.Getenv("MONGO_DB")
mongoTimeout, _ := strconv.Atoi(os.Getenv("MONGO_TIMEOUT"))
repo, err := repositorymongodb.NewMongoUserRepository(mongoURL, mongoDB, mongoTimeout)
if err != nil {
panic(err)
}
return repo
})
container.Transient(func() user.TokenizationService {
service := tokenization.NewUserLoginTokenizationService()
return service
})
container.Transient(func() user.Service {
var repo user.Repository
container.Make(&repo)
var tokenizationService user.TokenizationService
container.Make(&tokenizationService)
service := user.NewUserService(repo, tokenizationService)
return service
})
var service user.Service
container.Make(&service)
api.RegisterUserHandler(service, router)
api.RegisterHealthCheckHandler(router)
}