-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
153 lines (136 loc) · 3.55 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
package main
import (
"encoding/json"
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"
"log"
"os"
"time"
)
type Conf struct {
Url string
}
var (
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)
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)
//course rivate /api routes
private.GET("/course/all/:email", CourseIndex)
private.POST("/course/new", CourseAdd)
private.PUT("/course/update/:id", CourseUpdate)
private.DELETE("/course/delete/:id", CourseRemove)
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 CourseIndex(ctx *gin.Context) {
courses, err := models.AllCourses(ctx)
if err != nil {
log.Fatal(err)
ctx.JSON(404, gin.H{"error": "No courses found"})
return
}
ctx.JSON(200, courses)
}
func CourseAdd(ctx *gin.Context) {
_, err := models.AddCourse(ctx)
if err != nil {
ctx.JSON(500, gin.H{"error": "Internal error"})
return
}
ctx.JSON(200, gin.H{"success": "New course added"})
}
func CourseUpdate(ctx *gin.Context) {
_, err := models.UpdateCourse(ctx)
if err != nil {
ctx.JSON(500, gin.H{"error": "Error processing request"})
return
}
ctx.JSON(200, gin.H{"success": "Course updated"})
}
func CourseRemove(ctx *gin.Context) {
_, err := models.RemoveCourse(ctx)
if err != nil {
ctx.JSON(500, gin.H{"error": "Internal error"})
return
}
ctx.JSON(200, gin.H{"success": "Course removed"})
}