-
Notifications
You must be signed in to change notification settings - Fork 0
/
mddlwr.go
126 lines (121 loc) · 3.78 KB
/
mddlwr.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
package main
import (
"context"
"fmt"
"net/http"
"os"
"time"
"github.com/eensymachines-in/errx/httperr"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"github.com/streadway/amqp"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
// CORS : this allows all cross origin requests
func CORS(c *gin.Context) {
// First, we add the headers with need to enable CORS
// Make sure to adjust these headers to your needs
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Methods", "*")
c.Header("Access-Control-Allow-Headers", "*")
c.Header("Content-Type", "application/json")
// Second, we handle the OPTIONS problem
if c.Request.Method != "OPTIONS" {
c.Next()
} else {
// Everytime we receive an OPTIONS request,
// we just return an HTTP 200 Status Code
// Like this, Angular can now do the real
// request using any other method than OPTIONS
c.AbortWithStatus(http.StatusOK)
}
}
func RabbitConnectWithChn(connString, xname string) gin.HandlerFunc {
return func(c *gin.Context) {
conn, err := amqp.Dial(connString)
if err != nil {
httperr.HttpErrOrOkDispatch(c, httperr.ErrGatewayConnect(err), log.WithFields(log.Fields{
"stack": "RabbitConnectWithChn",
"conn_string": connString,
}))
return
}
// defer conn.Close()
ch, err := conn.Channel()
if err != nil {
httperr.HttpErrOrOkDispatch(c, httperr.ErrGatewayConnect(err), log.WithFields(log.Fields{
"stack": "RabbitConnectWithChn",
"login": os.Getenv("AMQP_LOGIN"),
"server": os.Getenv("AMQP_SERVER"),
}))
conn.Close() // incase no channel, we close the channel before we exit the stack
return
}
// NOTE: we shall be using a direct exchange with mac id specific routing key
err = ch.ExchangeDeclare(
xname, // name
"direct", // exhange type
true, // durable
false, //auto deleted
false, //internal
false, // nowait
nil, //amqp.table
)
if err != nil {
httperr.HttpErrOrOkDispatch(c, httperr.ErrGatewayConnect(err), log.WithFields(log.Fields{
"stack": "RabbitConnectWithChn",
"login": os.Getenv("AMQP_LOGIN"),
"server": os.Getenv("AMQP_SERVER"),
}))
// incase declaring the exchange fails we close the channel and connection on our way out
ch.Close()
conn.Close()
return
}
c.Set("amqp-ch", ch)
c.Set("amqp-conn", conn)
c.Next()
}
}
func MongoConnectURI(uri, dbname string) gin.HandlerFunc {
return func(c *gin.Context) {
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
if err != nil || client == nil {
httperr.HttpErrOrOkDispatch(c, httperr.ErrGatewayConnect(err), log.WithFields(log.Fields{
"stack": "MongoConnect",
"uri": uri,
}))
return
}
c.Set("mongo-client", client)
c.Set("mongo-database", client.Database(dbname))
}
}
/* NOTE: Archived was when we werent using i */
func MongoConnect(server, user, passwd, dbname string) gin.HandlerFunc {
return func(c *gin.Context) {
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
client, err := mongo.Connect(ctx, options.Client().ApplyURI(fmt.Sprintf("mongodb://%s:%s@%s:27017", user, passwd, server)))
if err != nil {
httperr.HttpErrOrOkDispatch(c, httperr.ErrGatewayConnect(err), log.WithFields(log.Fields{
"stack": "MongoConnect",
"login": user,
"server": server,
}))
return
}
if client.Ping(ctx, readpref.Primary()) != nil {
httperr.HttpErrOrOkDispatch(c, httperr.ErrGatewayConnect(err), log.WithFields(log.Fields{
"stack": "MongoConnect",
"login": user,
"server": server,
}))
return
}
c.Set("mongo-client", client)
c.Set("mongo-database", client.Database(dbname))
}
}