/
router.go
61 lines (53 loc) · 1.55 KB
/
router.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
package router
import (
"log"
"net/http"
"github.com/labstack/echo"
mw "github.com/labstack/echo/middleware"
"github.com/phuwn/crawlie/src/handler"
"github.com/phuwn/crawlie/src/handler/keyword"
"github.com/phuwn/crawlie/src/handler/user"
"github.com/phuwn/crawlie/src/middleware"
"github.com/phuwn/crawlie/src/util"
)
// Router - handling routes for incoming request
func Router() *echo.Echo {
r := echo.New()
r.HTTPErrorHandler = errorHandler
r.Use(cors)
r.GET("/healthz", handler.Healthz)
v1 := r.Group("/v1")
r.Pre(mw.RemoveTrailingSlash())
{
v1.Use(middleware.AddTransaction)
v1.POST("/auth", user.SignIn)
v1.Use(middleware.WithAuth)
}
v1.GET("/users/me", user.Get)
v1.GET("/keywords", keyword.ListByUser)
v1.GET("/keywords/:name", keyword.Get)
v1.POST("/keywords", keyword.UploadFile)
return r
}
// CorsConfig - CORS middleware
var cors = mw.CORSWithConfig(mw.CORSConfig{
AllowOrigins: []string{"*"},
AllowHeaders: []string{"Origin", "Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowCredentials: true,
})
func errorHandler(err error, c echo.Context) {
if he, ok := err.(*echo.HTTPError); ok {
code := he.Code
msg := he.Message
if he.Internal != nil || code == http.StatusInternalServerError {
log.Printf("%v. %v\n", msg, he.Internal)
util.JsonError(c, code, "Internal Server Error")
return
}
c.JSON(code, msg)
return
}
log.Println(err.Error())
util.JsonError(c, http.StatusInternalServerError, "Internal Server Error")
}