-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.go
64 lines (51 loc) · 1.5 KB
/
server.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
package main
import (
"errors"
"fmt"
"net/http"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)
// IndexRequest returns INDEX
func IndexRequest(c echo.Context) error {
return c.String(http.StatusOK, "INDEX")
}
// Server gets the server instance.
func Server() *echo.Echo {
serverOnce.Do(func() {
server = echo.New()
server.Use(middleware.CORSWithConfig(middleware.CORSConfig{
Skipper: middleware.DefaultSkipper,
AllowOrigins: []string{"*"},
AllowMethods: []string{http.MethodGet, http.MethodHead, http.MethodPut, http.MethodPatch, http.MethodPost, http.MethodDelete},
}))
server.HTTPErrorHandler = func(err error, c echo.Context) {
log.Warnf("Request failed: %s", err)
var statusCode int
var message string
switch errors.Unwrap(err) {
case echo.ErrUnauthorized:
statusCode = http.StatusUnauthorized
message = "unauthorized"
case echo.ErrForbidden:
statusCode = http.StatusForbidden
message = "access forbidden"
case echo.ErrInternalServerError:
statusCode = http.StatusInternalServerError
message = "internal server error"
case echo.ErrNotFound:
statusCode = http.StatusNotFound
message = "not found"
case echo.ErrBadRequest:
statusCode = http.StatusBadRequest
message = "bad request"
default:
statusCode = http.StatusInternalServerError
message = "internal server error"
}
message = fmt.Sprintf("%s, error: %+v", message, err)
c.String(statusCode, message)
}
})
return server
}