-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefaults.go
More file actions
67 lines (51 loc) · 1.43 KB
/
defaults.go
File metadata and controls
67 lines (51 loc) · 1.43 KB
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
package emir
import (
"time"
"go.uber.org/zap"
)
const (
//DefaultNetwork is the default listen network
DefaultNetwork = "tcp4"
//DefaultAddress is the default listen address
DefaultAddress = "localhost:8080"
//DefaultServerName is the default server name
DefaultServerName = "emir"
//DefaultReadTimeout is the default read timeout
DefaultReadTimeout = 20 * time.Second
)
// DefaultLogger creates a empty development logger
func DefaultLogger() *zap.Logger {
logger, err := zap.NewDevelopment()
if err != nil {
panic(err)
}
return logger
}
// DefaultNotFoundHandler is the default not found handler
func DefaultNotFoundHandler(c *Context) error {
return c.PlainString("Not Found", StatusNotFound)
}
// DefaultMethodNotAllowed is the default method not allowed handler
func DefaultMethodNotAllowed(c *Context) error {
return c.PlainString("Method Not Allowed", StatusMethodNotAllowed)
}
// DefaultPanicHandler is the default panic handler
func DefaultPanicHandler(c *Context, v interface{}) {
c.Logger().Error("panic recovered", zap.Any("v", v))
c.PlainString("Internal Server Error", StatusInternalServerError)
}
// DefaultErrorHandler is the default error handler
func DefaultErrorHandler(ctx *Context, err error) {
basicError, ok := err.(*BasicError)
if !ok {
ctx.SetStatusCode(500)
return
}
err = ctx.JSON(basicError)
if err != nil {
ctx.SetStatusCode(500)
return
}
ReleaseBasicError(basicError)
return
}