/
main.go
145 lines (128 loc) · 3.29 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
package main
import (
"encoding/json"
"log"
"os"
"time"
jwtLib "github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/contrib/jwt"
"github.com/gin-gonic/gin"
"github.com/jebrial/learnlink/models"
"golang.org/x/crypto/bcrypt"
)
type conf struct {
URL string
}
var (
//SECRET -
SECRET = "SECRETHERE"
)
func dbWare() gin.HandlerFunc {
//load the config
file, err := os.Open("config.json")
if err != nil {
log.Panic(err)
}
decoder := json.NewDecoder(file)
var conf = conf{}
err = decoder.Decode(&conf)
if err != nil {
log.Panic(err)
}
// connect to the database
db, err := models.NewDB(conf.URL)
if err != nil {
log.Panic(err)
}
return func(c *gin.Context) {
c.Set("db", db)
c.Next()
}
}
func main() {
// Set up server
ginServer := gin.Default()
ginServer.Use(dbWare())
//login public routes
ginServer.POST("/login", userLogin)
ginServer.POST("/signup", userAdd)
//set up private routes
private := ginServer.Group("/api")
private.Use(jwt.Auth(SECRET))
// user private /api routes
private.DELETE("/user/delete/:email", userRemove)
//link private /api routes
private.GET("/link/all/:email", linkIndex)
private.POST("/link/new", linkAdd)
private.PUT("/link/update/:id", linkUpdate)
ginServer.Run(":3001")
}
func userLogin(ctx *gin.Context) {
user, err := models.FindUser(ctx)
if err != nil {
ctx.JSON(404, gin.H{"error": "error loging in"})
return
}
if bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(ctx.PostForm("password")+"my secret pepper")) != nil {
ctx.JSON(401, gin.H{"error": "User not Authorized"})
return
}
token := jwtLib.New(jwtLib.GetSigningMethod("HS256"))
token.Claims["ID"] = user.Email
token.Claims["exp"] = time.Now().Add(time.Hour * 24 * 7).Unix() // expires in a week
tokenString, err2 := token.SignedString([]byte(SECRET))
if err2 != nil {
ctx.JSON(500, gin.H{"error": "Problem generating token"})
}
user.Password = ""
ctx.JSON(200, gin.H{"user": user, "token": tokenString})
}
func userAdd(ctx *gin.Context) {
user, err := models.AddUser(ctx)
if err != nil {
ctx.JSON(500, gin.H{"error": "Internal error"})
return
}
token := jwtLib.New(jwtLib.GetSigningMethod("HS256"))
token.Claims["ID"] = user.Email
token.Claims["exp"] = time.Now().Add(time.Hour * 24 * 7).Unix() // expires in a week
tokenString, err2 := token.SignedString([]byte(SECRET))
if err2 != nil {
ctx.JSON(500, gin.H{"error": "Problem generating token"})
}
user.Password = ""
ctx.JSON(200, gin.H{"user": user, "token": tokenString, "success": "New user added"})
}
func userRemove(ctx *gin.Context) {
_, err := models.RemoveUser(ctx)
if err != nil {
ctx.JSON(500, gin.H{"error": "Internal error"})
return
}
ctx.JSON(200, gin.H{"success": "User removed"})
}
func linkIndex(ctx *gin.Context) {
courses, err := models.AllLinks(ctx)
if err != nil {
log.Fatal(err)
ctx.JSON(404, gin.H{"error": "No courses found"})
return
}
ctx.JSON(200, courses)
}
func linkAdd(ctx *gin.Context) {
_, err := models.AddLink(ctx)
if err != nil {
ctx.JSON(500, gin.H{"error": "Internal error"})
return
}
ctx.JSON(200, gin.H{"success": "New course added"})
}
func linkUpdate(ctx *gin.Context) {
_, err := models.UpdateLink(ctx)
if err != nil {
ctx.JSON(500, gin.H{"error": "Error processing request"})
return
}
ctx.JSON(200, gin.H{"success": "Link updated"})
}