forked from ypankaj007/golang-mongodb-restful-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
110 lines (86 loc) · 2.8 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
package main
import (
api "golang-mongodb-restful-starter-kit/app/handlers"
"golang-mongodb-restful-starter-kit/app/middleware"
"golang-mongodb-restful-starter-kit/app/services/auth"
"golang-mongodb-restful-starter-kit/app/services/jwt"
"golang-mongodb-restful-starter-kit/app/services/user"
"golang-mongodb-restful-starter-kit/config"
"golang-mongodb-restful-starter-kit/db"
_ "golang-mongodb-restful-starter-kit/docs"
"golang-mongodb-restful-starter-kit/utility"
httpSwagger "github.com/swaggo/http-swagger"
"log"
"net/http"
"time"
"github.com/gorilla/mux"
mgo "gopkg.in/mgo.v2"
)
// @title Application API
// @version 1.0
// @description Auth apis (signup/login) and user apis
// @contact.name API Support
// @contact.email [email protected]
// @license.name Apache 2.0
// @host localhost:8080
// @securityDefinitions.apikey ApiKeyAuth
// @in header
// @name Authorization
// @BasePath /api/v1
func main() {
// Initialize config
c := config.NewConfig()
// Make connection with db and get instance
dbSession := db.GetInstance(c)
//
dbSession.SetSafe(&mgo.Safe{})
// UserService
userService := user.New(dbSession, c)
// AuthService
authService := auth.New(dbSession, c)
// Router
router := mux.NewRouter()
// UserRouter
api.UserRouter(userService, router)
// AuthRouter
api.AuthRouter(authService, c, router)
// JWT services
jwtService := jwt.JwtToken{C: c}
// Added middleware over all request to authenticate
router.Use(middleware.Cors, jwtService.ProtectedEndpoint)
// Swagger
router.PathPrefix("/swagger").Handler(httpSwagger.WrapHandler)
// Server configuration
srv := &http.Server{
Handler: utility.Headers(router), // Set header to routes
Addr: c.Address,
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
log.Println("Application is running at ", c.Address)
// Serving application at specified port
log.Fatal(srv.ListenAndServe())
// // Run our server in a goroutine so that it doesn't block.
// go func() {
// if err := srv.ListenAndServe(); err != nil {
// log.Println(err)
// }
// }()
// c := make(chan os.Signal, 1)
// // We'll accept graceful shutdowns when quit via SIGINT (Ctrl+C)
// // SIGKILL, SIGQUIT or SIGTERM (Ctrl+/) will not be caught.
// signal.Notify(c, os.Interrupt)
// // Block until we receive our signal.
// <-c
// // Create a deadline to wait for.
// ctx, cancel := context.WithTimeout(context.Background(), wait)
// defer cancel()
// // Doesn't block if no connections, but will otherwise wait
// // until the timeout deadline.
// srv.Shutdown(ctx)
// // Optionally, you could run srv.Shutdown in a goroutine and block on
// // <-ctx.Done() if your application should wait for other services
// // to finalize based on context cancellation.
// log.Println("shutting down")
// os.Exit(0)
}