-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.go
84 lines (71 loc) · 1.53 KB
/
example.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"github.com/gabibotos/go-srv/middleware"
"github.com/gabibotos/go-srv/srv"
"github.com/gabibotos/go-srv/srv/schema"
"go.uber.org/zap"
"net/http"
"time"
)
func Example() {
lc := zap.NewProductionConfig()
lc.Development = true
zlg, err := lc.Build(zap.AddCallerSkip(1))
if err != nil {
panic(err)
}
ll := &zapLogger{zlg}
ss := New(ll,
WithHTTPOption(
srv.WithListeners(&schema.HTTPFlg{
Prefix: "app",
Host: "localhost",
Port: 8081,
ListenLimit: 10,
KeepAlive: 5*time.Second,
ReadTimeout: 3*time.Second,
WriteTimeout: 3*time.Second,
}),
srv.OnShutdown(func() {
ll.Printf("OnShutdown - I'm Done")
}),
),
WithSystemHTTPOption(
srv.WithSystemListeners(
&schema.HTTPFlg{
Prefix: "metrics",
Host: "localhost",
Port: 10240,
}),
),
)
ss.App().HandleFunc("/test1", handleTest1)
ss.App().HandleFunc("/test2", handleTest2)
// Register HTTP API middleware here
ss.App().Use(
middleware.LogRequests(ll),
)
err = ss.Init()
if err != nil {
panic(err)
}
err = ss.Start()
if err != nil {
panic(err)
}
}
type zapLogger struct {
zlg *zap.Logger
}
func (z *zapLogger) Printf(format string, args ...interface{}) {
z.zlg.Sugar().Infof(format, args...)
}
func (z *zapLogger) Fatalf(format string, args ...interface{}) {
z.zlg.Sugar().Fatalf(format, args...)
}
func handleTest1(rw http.ResponseWriter, r *http.Request) {
rw.Write([]byte("test1"))
}
func handleTest2(rw http.ResponseWriter, r *http.Request) {
rw.Write([]byte("test2"))
}