forked from goproxy/goproxy.cn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
72 lines (59 loc) · 1.38 KB
/
main.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
package main
import (
"context"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/air-gases/defibrillator"
"github.com/air-gases/limiter"
"github.com/air-gases/logger"
"github.com/air-gases/redirector"
"github.com/aofei/air"
_ "github.com/goproxy/goproxy.cn/handler"
"github.com/rs/zerolog/log"
)
// a is the `air.Default`.
var a = air.Default
func main() {
a.ErrorHandler = errorHandler
a.Pregases = []air.Gas{
logger.Gas(logger.GasConfig{
IncludeClientAddress: true,
}),
defibrillator.Gas(defibrillator.GasConfig{}),
redirector.WWW2NonWWWGas(redirector.WWW2NonWWWGasConfig{}),
limiter.BodySizeGas(limiter.BodySizeGasConfig{
MaxBytes: 1 << 20,
}),
}
shutdownChan := make(chan os.Signal, 1)
signal.Notify(shutdownChan, os.Interrupt, syscall.SIGTERM)
go func() {
if err := a.Serve(); err != nil {
log.Error().Err(err).
Str("app_name", a.AppName).
Msg("server error")
}
}()
<-shutdownChan
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
a.Shutdown(ctx)
}
// errorHandler is the error handler.
func errorHandler(err error, req *air.Request, res *air.Response) {
if res.Written {
return
}
m := ""
if !req.Air.DebugMode && res.Status == http.StatusInternalServerError {
m = http.StatusText(res.Status)
} else {
m = err.Error()
}
res.WriteJSON(map[string]interface{}{
"Error": m,
})
}