-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
44 lines (39 loc) · 886 Bytes
/
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
package main
import (
"fmt"
"net/http"
"github.com/TheCureliestWalk/go-api/middleware"
"github.com/TheCureliestWalk/go-api/routes/users"
"github.com/TheCureliestWalk/go-api/services"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
fmt.Println("Server starting...")
_, sql, err := services.Connect()
defer sql.Close()
if err != nil {
fmt.Println("error", err.Error())
} else {
fmt.Println("Connected to DB.")
}
//middlewares
r.Use(middleware.Cors())
//activate routes
r.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "API is up and running!",
"success": true,
})
})
r.GET("/user", users.Read)
r.GET("/user/:id", users.ReadByID)
r.POST("/user", users.Create)
r.DELETE("/user/:id", users.DeleteByID)
r.PUT("/user/:id", users.UpdateByID)
//Run the server
err = r.Run(":5000")
if err != nil {
return
}
}