-
Notifications
You must be signed in to change notification settings - Fork 10
/
app.go
77 lines (64 loc) · 1.88 KB
/
app.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
package main
import (
// Log items to the terminal
"log"
// Import gin for route definition
"github.com/gin-gonic/gin"
// Import godotenv for .env variables
"github.com/joho/godotenv"
// Import our app controllers
"github.com/tesh254/golang_todo_api/controllers"
"github.com/tesh254/golang_todo_api/middlewares"
)
// init gets called before the main function
func init() {
// Log error if .env file does not exist
if err := godotenv.Load(); err != nil {
log.Printf("No .env file found")
}
}
func main() {
// Init gin router
router := gin.Default()
// Its great to version your API's
v1 := router.Group("/api/v1")
{
// Define the hello controller
hello := new(controllers.HelloWorldController)
// Define a GET request to call the Default
// method in controllers/hello.go
v1.GET("/hello", hello.Default)
// Define the user controller
user := new(controllers.UserController)
// Define the link controller
link := new(controllers.BookmarkController)
// Create the signup endpoint
v1.POST("/signup", user.Signup)
// Create the login endpoint
v1.POST("/login", user.Login)
// Password reset
v1.PUT("/password-reset", user.PasswordReset)
// Send reset link
v1.PUT("/reset-link", user.ResetLink)
// Send verify link
v1.PUT("/verify-link", user.VerifyLink)
// Verify account
v1.PUT("/verify-account", user.VerifyAccount)
// Refresh token
v1.GET("/refresh", user.RefreshToken)
bookmarks := v1.Group("/bookmarks")
bookmarks.Use(middlewares.Authenticate())
{
bookmarks.GET("/all", link.FetchBookmarks)
bookmarks.POST("/create", link.CreateBookmak)
bookmarks.DELETE("/delete", link.DeleteBookmark)
}
}
// Handle error response when a route is not defined
router.NoRoute(func(c *gin.Context) {
// In gin this is how you return a JSON response
c.JSON(404, gin.H{"message": "Not found"})
})
// Init our server
router.Run(":5000")
}