forked from shadowshot-x/micro-product-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
132 lines (110 loc) · 4.52 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
gohandlers "github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/joho/godotenv"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/shadowshot-x/micro-product-go/authservice"
"github.com/shadowshot-x/micro-product-go/authservice/middleware"
"github.com/shadowshot-x/micro-product-go/clientclaims"
"github.com/shadowshot-x/micro-product-go/couponservice"
"github.com/shadowshot-x/micro-product-go/monitormodule"
"github.com/shadowshot-x/micro-product-go/ordertransformerservice"
"github.com/shadowshot-x/micro-product-go/productservice"
"go.uber.org/zap"
)
func PingHandler(rw http.ResponseWriter, r *http.Request) {
rw.Write([]byte("Your App seems Healthy"))
}
func simplePostHandler(rw http.ResponseWriter, r *http.Request) {
fileName, err := os.OpenFile("./metricDetails.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal("error in file ops", zap.Error(err))
}
defer fileName.Close()
reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Fatal(err)
}
fileName.Write([]byte(reqBody))
fileName.Write([]byte("\n"))
rw.Write([]byte("Post Request Recieved for the Success"))
}
func main() {
log, _ := zap.NewProduction()
defer log.Sync()
log.Info("Starting...")
err := godotenv.Load(".env")
if err != nil {
log.Error("Error loading .env file", zap.Error(err))
}
err = monitormodule.MonitorBinder(log)
if err != nil {
fmt.Println(err)
return
}
mainRouter := mux.NewRouter()
suc := authservice.NewSignupController(log)
sic := authservice.NewSigninController(log)
uc := clientclaims.NewUploadController(log)
dc := clientclaims.NewDownloadController(log)
tm := middleware.NewTokenMiddleware(log)
pc := productservice.NewProductController(log)
transc := ordertransformerservice.NewTransformerController(log)
redisInstance := couponservice.RedisInstanceGenerator(log)
cc := couponservice.NewCouponStreamController(log, redisInstance)
// ping function
mainRouter.HandleFunc("/ping", PingHandler)
mainRouter.HandleFunc("/checkRoutine", simplePostHandler).Methods("POST")
// We will create a Subrouter for Authentication service
// route for sign up and signin. The Function will come from auth-service package
// checks if given header params already exists. If not,it adds the user
authRouter := mainRouter.PathPrefix("/auth").Subrouter()
authRouter.HandleFunc("/signup", suc.SignupHandler).Methods("POST")
// The Signin will send the JWT Token back as we are making microservices.
// JWT token will make sure that other services are protected.
// So, ultimately, we would need a middleware
authRouter.HandleFunc("/signin", sic.SigninHandler).Methods("GET")
// File Upload SubRouter
claimsRouter := mainRouter.PathPrefix("/claims").Subrouter()
claimsRouter.HandleFunc("/upload", uc.UploadFile)
claimsRouter.HandleFunc("/download", dc.DownloadFile)
claimsRouter.Use(tm.TokenValidationMiddleware)
//Initialize the Gorm connection
// pc.InitGormConnection()
productRouter := mainRouter.PathPrefix("/product").Subrouter()
productRouter.HandleFunc("/getprods", pc.GetAllProductsHandler).Methods("GET")
productRouter.HandleFunc("/addprod", pc.AddProductHandler).Methods("POST")
productRouter.HandleFunc("/getprodbyid", pc.GetAllProductByIdHandler).Methods("GET")
productRouter.HandleFunc("/deletebyid", pc.DeleteProductHandler).Methods("DELETE")
productRouter.HandleFunc("/customquery", pc.CustomQueryHandler).Methods("GET", "POST")
//Coupon Service SubRouter
couponRouter := mainRouter.PathPrefix("/coupon").Subrouter()
couponRouter.HandleFunc("/addcoupon", cc.AddCouponList).Methods("POST")
couponRouter.HandleFunc("/getvendorcoupons", cc.GetCouponForInternalValidation).Methods("GET")
couponRouter.HandleFunc("/delregionstream", cc.PurgeStream).Methods("DELETE")
// Transformer Service SubRouter
transformerOrderRouter := mainRouter.PathPrefix("/transformer").Subrouter()
transformerOrderRouter.HandleFunc("/transform", transc.TransformerHandler).Methods("GET")
// CORS Header
cors := gohandlers.CORS(gohandlers.AllowedOrigins([]string{"http://localhost:3000"}))
// Adding Prometheus http handler to expose the metrics
// this will display our metrics as well as some standard metrics
mainRouter.Path("/metrics").Handler(promhttp.Handler())
// Add the Middleware to different subrouter
// HTTP Server
// Add Time outs
server := &http.Server{
Addr: ":9090",
Handler: cors(mainRouter),
}
err = server.ListenAndServe()
if err != nil {
log.Error("Error Booting the Server", zap.Error(err))
}
}