-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
123 lines (117 loc) · 3.75 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
package main
/* ==============================================
Copyright (c) Eensymachines
Developed by : kneerunjun@gmail.com
Developed on : OCT'22
Eensymachines accounts need to be maintained over an api endpoint
containerized application can help do that
============================================== */
import (
"context"
"flag"
"os"
"time"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var (
FVerbose, FLogF bool
logFile string
)
const (
// getting connected to a container that runs mongo
CONNSTR = "mongodb://srvmongo:27017"
// for testing purposes we need to have connection via the local host
// CONNSTR = "mongodb://localhost:27017"
DBNAME = "eensydb"
)
func init() {
flag.BoolVar(&FVerbose, "verbose", false, "Level of log messages")
flag.BoolVar(&FLogF, "flog", false, "Log output direction")
// Setting up log configuration for the api
log.SetFormatter(&log.TextFormatter{
DisableColors: false,
FullTimestamp: true,
})
log.SetReportCaller(false)
// By default the log output is stdout and the level is info
log.SetOutput(os.Stdout) // FLogF will set it main, but dfault is stdout
log.SetLevel(log.DebugLevel) // default level info debug but FVerbose will set it main
logFile = os.Getenv("LOGF")
}
func main() {
/*Log setup : level of logging and direction of logging*/
flag.Parse() // command line flags are parsed
log.WithFields(log.Fields{
"verbose": FVerbose,
"flog": FLogF,
}).Info("Log configuration..")
if FVerbose {
log.SetLevel(log.DebugLevel)
}
if FLogF {
lf, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0664)
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("Failed to connect to log file, kindly check the privileges")
} else {
log.Infof("Check log file for entries @ %s", logFile)
log.SetOutput(lf)
}
}
log.Info("Now starting account services..")
defer log.Warn("Now shutting down account services..")
// ------------Setting up the database connections
ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(CONNSTR))
if err != nil {
log.Panicf("failed to connect to database %s", err)
}
collAccs := client.Database(DBNAME).Collection("accounts")
collAccs.Indexes().CreateMany(context.Background(), []mongo.IndexModel{
{Keys: bson.D{primitive.E{Key: "email", Value: 1}}, Options: options.Index().SetUnique(true)},
})
log.Info("Database setup done...")
// ------------- Database connection setup
defer func() {
log.Warn("Smart bill: Now diconnecting database connection")
if err := client.Disconnect(ctx); err != nil {
panic(err)
}
}()
gin.SetMode(gin.DebugMode)
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"app": "acccountservices",
"logs": logFile,
"verblog": FVerbose,
"logtofile": FLogF,
})
})
/* ++++++++++++
/api/accounts : group of accounts
+++++++===*/
api := r.Group("/api") // all the json endpoints grouped
api.Use(CORS)
accounts := api.Group("/accounts")
accounts.Use(DBCollection(client, "eensydb", "accounts"))
accounts.POST("", AccountPayload, Accounts) // creates new account
/* ++++++++++++
/api/accounts/:accid : single account details
+++++++===*/
// incase of get and delete the AccountPayload middleware will be in action
// incase of put, patch the account details need to be read back
account := accounts.Group("/:accid", AccountPayload)
// CRUD on single account
account.GET("", AccountDetails)
account.PUT("", AccountDetails)
account.DELETE("", AccountDetails)
log.Fatal(r.Run(":8080"))
}