-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
63 lines (55 loc) · 2.39 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
package main
import (
"github.com/codehand/echo-restful-crud-api-example/config"
"github.com/codehand/echo-restful-crud-api-example/db"
"github.com/codehand/echo-restful-crud-api-example/handler"
"github.com/codehand/echo-restful-crud-api-example/middlewares"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"github.com/sirupsen/logrus"
)
func main() {
e := echo.New()
e.Validator = middlewares.InitCustomValidator()
e.Use(middleware.RecoverWithConfig(middleware.RecoverConfig{
StackSize: 1 << 10, // 1 KB
}))
e.Use(middleware.Logger())
e.Use(middleware.RequestID())
api := e.Group("/api/v1", serverHeader)
api.GET("/products", handler.GetProducts) // Returns all resources of this product
api.POST("/products", handler.CreateProduct) // Creates a resource of this product and stores the data you posted, then returns the ID
api.GET("/products/:id", handler.GetProduct) // Returns the resource of this product with that ID
api.PUT("/products/:id", handler.UpdateProduct) // Updates the resource of this product with that ID
api.DELETE("/products/:id", handler.DeleteProduct) // Deletes the resource of this product with that ID
apiV2 := e.Group("/api/v2", serverHeaderVersion2)
apiV2.Use(middleware.KeyAuth(func(key string, c echo.Context) (bool, error) {
return key == config.Config.API.Token, nil
}))
apiV2.GET("/products", handler.GetProducts) // Returns all resources of this product
apiV2.POST("/products", handler.CreateProduct) // Creates a resource of this product and stores the data you posted, then returns the ID
apiV2.GET("/products/:id", handler.GetProduct) // Returns the resource of this product with that ID
apiV2.PUT("/products/:id", handler.UpdateProduct) // Updates the resource of this product with that ID
apiV2.DELETE("/products/:id", handler.DeleteProduct) // Deletes the resource of this product with that ID
err := db.Ping()
if err != nil {
logrus.Fatal(err)
}
// service start at port :9090
err = e.Start(":9090")
if err != nil {
logrus.Fatal(err)
}
}
func serverHeader(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
c.Response().Header().Set("x-version", "Test/v1.0")
return next(c)
}
}
func serverHeaderVersion2(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
c.Response().Header().Set("x-version", "Test/v2.0")
return next(c)
}
}