forked from hehexianshi/guuid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
72 lines (61 loc) · 1.65 KB
/
Copy pathserver.go
File metadata and controls
72 lines (61 loc) · 1.65 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
68
69
70
71
72
package guuid
import (
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"net/http"
"time"
"os"
)
const (
_ = iota
GET
POST
)
type webServer struct {
addr string
readTimeout time.Duration
writeTimeout time.Duration
echo *echo.Echo
}
func NewServer(addr string, readTimeout time.Duration, writeTimeout time.Duration) *webServer {
s := &webServer{
addr: addr,
readTimeout: readTimeout,
writeTimeout: writeTimeout,
echo: echo.New(),
}
return s
}
func (s *webServer) Start() {
hs := &http.Server{
Addr: s.addr,
ReadTimeout: s.readTimeout,
WriteTimeout: s.writeTimeout,
}
s.echo.HideBanner = true
// echo middleware setting
s.echo.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
Format: `[${time_rfc3339}] {"time":${time_unix},"id":"${id}","remote_ip":"${remote_ip}","host":"${host}",` +
`"method":"${method}","uri":"${uri}","status":${status}, "latency":${latency},` +
`"latency_human":"${latency_human}","bytes_in":${bytes_in},` +
`"bytes_out":${bytes_out}}` + "\n",
Output: os.Stdout,
}))
s.echo.Use(middleware.GzipWithConfig(middleware.GzipConfig{
Level: 5,
}))
s.echo.Logger.Fatal(s.echo.StartServer(hs))
}
func (s *webServer) Handler(router string, method int, h func(echo.Context) error) {
switch method {
case GET:
s.echo.GET(router, h)
case POST:
s.echo.POST(router, h)
default:
s.echo.GET(router, h)
}
}
func (s *webServer) HandlerError(h func(error, echo.Context)) {
s.echo.HTTPErrorHandler = h
}