-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
94 lines (73 loc) · 1.89 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
package main
import (
"log"
"net/http"
"html/template"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/amku91/lam-heroku/app/order"
"github.com/amku91/lam-heroku/mongo"
"github.com/rs/cors"
"github.com/amku91/lam-heroku/config"
)
func main() {
var (
err error
)
//Initialize the routes
r := chi.NewRouter()
// CORS config
corsConfig := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"*"},
ExposedHeaders: []string{"Link"},
AllowCredentials: true,
MaxAge: 300,
})
// Initialise the router middlewares
r.Use(corsConfig.Handler)
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
Home(w)
})
r.Get("/ping", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Lam Ping Successful"))
})
r.Mount("/order", order.Controller{}.Routes())
//Init system functions
initialise()
log.Println("Starting Application...")
log.Println("Application will listen at port 8080")
err = http.ListenAndServe(":8080", r)
//http.ListenAndServe(":" + os.Getenv("PORT"), r)
if err != nil {
log.Println("Error while initializing the Application: " + err.Error())
return
}
}
//All initialisation related function calls goes here
func initialise() {
initDB()
}
//Make Mongo DB Connection
func initDB() {
mongo.MaxPool = config.MONGO_MAX_POOL
mongo.PATH = config.MONGO_DSN
mongo.DBNAME = config.MONGO_DATABASE
mongo.CheckAndInitServiceConnection()
}
// Home Loads the home page
func Home(w http.ResponseWriter) {
t, err := template.ParseFiles("index.html")
if err != nil {
log.Print("template parsing error: ", err)
}
err = t.Execute(w, nil)
if err != nil {
log.Print("template executing error: ", err)
}
}