-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
84 lines (76 loc) · 2.6 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
package main
/* Microservice for user registration, authorization, authentication.
This also has auxilliary function that lead to deletion and updation of user
uses http rest over json to define endpoints that can be called to modiufy users
author :[email protected]
*/
import (
"encoding/json"
"net/http"
"os"
"github.com/eensymachines-in/utilities"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
// AppEnviron : Object defined for containing all the environment variables.
type AppEnviron struct {
MongoSrvr string `json:"MONGO_SRVR"`
MongoUsr string `json:"MONGO_USER"`
MongoPass string `json:"MONGO_PASS"`
}
var (
environ = AppEnviron{} // instance of the app environment, gets populated in the init functio
)
func init() {
log.SetFormatter(&log.TextFormatter{
DisableColors: false,
FullTimestamp: false,
PadLevelText: true,
})
log.SetReportCaller(false)
log.SetOutput(os.Stdout)
log.SetLevel(log.InfoLevel) // default is info level, if verbose then trace
/* ------------------- Reading in the environment */
// and then give it a spin to test
tempEnviron := map[string]string{}
for _, v := range []string{"MONGO_SRVR", "MONGO_USER", "MONGO_PASS"} {
// checking for all the environment variables, incase missing will panic
if os.Getenv(v) == "" {
log.Fatalf("One or more of the environment variables is missing %s", v)
} else {
tempEnviron[v] = os.Getenv(v)
}
}
byt, _ := json.Marshal(tempEnviron)
if err := json.Unmarshal(byt, &environ); err != nil {
log.Fatalf("failed to read in the environment variables %s", err)
}
log.Info("All environment vars as expected...")
/* ----------------- Ping test for the database or go burst */
if err := utilities.MongoPingTest(environ.MongoSrvr, environ.MongoUsr, environ.MongoPass); err != nil {
log.Fatal(err)
}
}
func main() {
log.Info("Starting the userauth service")
defer log.Warn("Closing the userauth service")
gin.SetMode(gin.DebugMode)
r := gin.Default()
api := r.Group("/api").Use(utilities.CORS)
api.GET("/ping", func(ctx *gin.Context) {
ctx.AbortWithStatusJSON(http.StatusOK, gin.H{
"data": "If you can see this the webapi-userauth service is running",
})
})
/* Login authentication for user, sends back a jwt token */
// ?action=login
// ?action=create
users := api.Use(utilities.MongoConnect(environ.MongoSrvr, environ.MongoUsr, environ.MongoPass, "aquaponics"))
users.POST("/users", HndlLstUsers)
users.GET("/users", HndlLstUsers)
/* Single user operations */
users.GET("/users/:id", HndlAUser)
users.DELETE("/users/:id", HndlAUser)
users.PATCH("/users/:id", HndlAUser)
log.Fatal(r.Run(":8080"))
}