-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
164 lines (148 loc) · 4.33 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"log"
"net/http"
"os"
"time"
"github.com/Trirandom/basic-listener/pkg/apitools"
"github.com/Trirandom/capstone-server/pkg/mongo"
jwt "github.com/appleboy/gin-jwt"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"gopkg.in/mgo.v2/bson"
)
type EntryDto struct {
DataString string `form:"dataString" json:"dataString" binding:"required"`
Key string `form:"key" json:"key" binding:"required"`
}
type Entry struct {
DataString string `json:"data" binding:"required"`
Date string `json:"key" binding:"required"`
}
var identityKey = "id"
func registerHandler(c *gin.Context) {
var entry EntryDto
if err := c.ShouldBind(&entry); err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
ms, err := mongo.NewSession()
if err != nil {
c.AbortWithStatusJSON(http.StatusConflict, gin.H{
"status": http.StatusInternalServerError,
"message": "Unable to open a mongo session",
"resourceId": entry.DataString,
})
return
}
var row []Entry = nil
ms.GetCollection("entries").Find(bson.M{"dataString": entry.DataString}).All(&row)
if row != nil {
defer ms.Close()
c.AbortWithStatusJSON(http.StatusConflict, gin.H{
"status": http.StatusConflict,
"message": "Already exist",
"resourceId": entry.DataString,
})
return
}
dbEntry := Entry{
DataString: entry.DataString,
}
err = ms.GetCollection("entries").Insert(dbEntry)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
"status": http.StatusInternalServerError,
"message": "Cannot insert into database",
"resourceId": entry.DataString,
})
return
} else {
c.JSON(http.StatusCreated, gin.H{
"status": http.StatusCreated,
"message": "Entry created",
"resourceId": entry.DataString,
})
}
defer ms.Close()
return
}
func main() {
port := os.Getenv("PORT")
r := gin.New()
r.Use(cors.New(cors.Config{
AllowMethods: []string{"POST"},
AllowHeaders: []string{"Origin, X-Requested-With, Content-Type, Accept, Authorization"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
AllowAllOrigins: true,
MaxAge: 12 * time.Hour,
}))
r.Use(gin.Logger())
r.Use(gin.Recovery())
if port == "" {
port = "8080"
}
// the jwt middleware
authMiddleware, err := jwt.New(&jwt.GinJWTMiddleware{
Realm: "test zone",
Key: []byte(apitools.GoDotEnvVariable("MIDDLEWARE_KEY")),
Timeout: time.Hour,
MaxRefresh: time.Hour,
IdentityKey: identityKey,
PayloadFunc: func(data interface{}) jwt.MapClaims {
return nil
},
IdentityHandler: func(c *gin.Context) interface{} {
return nil
},
Authenticator: func(c *gin.Context) (interface{}, error) {
return nil, nil
},
Authorizator: func(data interface{}, c *gin.Context) bool {
// fmt.Println("Authorizator data ", data.(*User).FirstName)
// if v, ok := data.(*User); ok && v.FirstName == "admin" {
// fmt.Println("Authorizator v %#v", v.FirstName)
// return true
// }
// fmt.Println("Authorizator failed v %#v", data.(*User))
return true
},
Unauthorized: func(c *gin.Context, code int, message string) {
c.JSON(code, gin.H{
"status": code,
"message": message,
})
},
// TokenLookup is a string in the form of "<source>:<name>" that is used
// to extract token from the request.
// Optional. Default value "header:Authorization".
// Possible values:
// - "header:<name>"
// - "query:<name>"
// - "cookie:<name>"
// - "param:<name>"
TokenLookup: "header: Authorization, query: token, cookie: jwt",
// TokenLookup: "query:token",
// TokenLookup: "cookie:token",
// TokenHeadName is a string in the header. Default value is "Bearer"
TokenHeadName: "Bearer",
// TimeFunc provides the current time. You can override it to use another time value. This is useful for testing or if your server uses a different time zone than your tokens.
TimeFunc: time.Now,
})
if err != nil {
log.Fatal("JWT Error:" + err.Error())
}
r.POST("/register", registerHandler)
r.NoRoute(authMiddleware.MiddlewareFunc(), func(c *gin.Context) {
claims := jwt.ExtractClaims(c)
log.Printf("NoRoute claims: %#v \n", claims)
c.JSON(http.StatusNotFound, gin.H{
"status": http.StatusNotFound,
"message": "Page not found",
})
})
if err := http.ListenAndServe(":"+port, r); err != nil {
log.Fatal(err)
}
}