This repository has been archived by the owner on Feb 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
/
http.go
169 lines (158 loc) · 5 KB
/
http.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2019 The bigfile Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// Package http is used to provide http service entry and other tools
package http
import (
ctx "context"
"fmt"
libHTTP "net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/bigfile/bigfile/config"
"github.com/bigfile/bigfile/databases"
"github.com/bigfile/bigfile/databases/migrate"
"github.com/bigfile/bigfile/http"
"github.com/bigfile/bigfile/log"
"github.com/gin-gonic/gin"
"github.com/olekukonko/tablewriter"
"gopkg.in/urfave/cli.v2"
// import migration
_ "github.com/bigfile/bigfile/databases/migrate/migrations"
)
var (
category = "http"
logger = log.MustNewLogger(nil)
// Commands http service command
Commands = []*cli.Command{
{
Name: "http:routes",
Category: category,
Usage: "list http routes",
UsageText: "http:routes",
Action: func(context *cli.Context) error {
gin.SetMode(gin.ReleaseMode)
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"method", "path", "handler"})
for _, route := range http.Routers().Routes() {
table.Append([]string{route.Method, route.Path, route.Handler})
}
table.Render()
return nil
},
},
{
Name: "http:start",
Category: category,
Usage: "start http service",
UsageText: "http:start [command options]",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "host",
Aliases: []string{"H"},
Usage: "http service listen ip",
Value: "0.0.0.0",
},
&cli.Int64Flag{
Name: "port",
Aliases: []string{"P"},
Usage: "http service listen port",
Value: 10985,
},
&cli.DurationFlag{
Name: "read-timeout",
Usage: "read-timeout is the maximum duration for reading " +
"the entire request, including the body",
Value: 0,
},
&cli.DurationFlag{
Name: "read-header-timeout",
Usage: "read-header-timeout is the amount of time allowed " +
"to read request headers",
Value: 0,
},
&cli.DurationFlag{
Name: "write-timeout",
Usage: "writer-timeout is the maximum duration before timing " +
"out writes of the response",
Value: 0,
},
&cli.DurationFlag{
Name: "idle-timeout",
Usage: "idle-timeout is the maximum amount of time to wait for " +
"the next request when keep-alives are enabled",
Value: 0,
},
&cli.DurationFlag{
Name: "wait-shutdown",
Usage: "wait time before timeout for closing server",
Value: 5 * time.Second,
},
&cli.IntFlag{
Name: "max-header-bytes",
Usage: "max-header-bytes controls the maximum number of bytes the " +
"server will read parsing the request header's keys and values, " +
"including the request line. It does not limit the size of the request body",
Value: 0,
},
&cli.StringFlag{
Name: "cert-file",
Usage: "certificate file for starting https service",
},
&cli.StringFlag{
Name: "cert-key",
Usage: "certificate key file for starting https service",
},
},
Action: func(context *cli.Context) error {
addr := fmt.Sprintf("%s:%d", context.String("host"), context.Int64("port"))
server := libHTTP.Server{
Addr: addr,
Handler: http.Routers(),
ReadTimeout: context.Duration("read-timeout"),
ReadHeaderTimeout: context.Duration("read-header-timeout"),
WriteTimeout: context.Duration("write-timeout"),
IdleTimeout: context.Duration("idle-timeout"),
MaxHeaderBytes: context.Int("max-header-bytes"),
}
certFile := context.String("cert-file")
certKey := context.String("cert-key")
go func() {
if certFile != "" && certKey != "" {
logger.Infof("bigfile http service listening on: https://%s", addr)
if err := server.ListenAndServeTLS(certFile, certKey); err != nil && err != libHTTP.ErrServerClosed {
logger.Errorf("https server error: %s", err)
}
} else {
logger.Infof("bigfile http service listening on: http://%s", addr)
if err := server.ListenAndServe(); err != nil && err != libHTTP.ErrServerClosed {
logger.Errorf("https server error: %s", err)
}
}
}()
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
logger.Debug("Shutdown Server ...")
ctx, cancel := ctx.WithTimeout(ctx.Background(), context.Duration("wait-shutdown"))
defer cancel()
if err := server.Shutdown(ctx); err != nil {
logger.Fatal("Server Shutdown:", err)
}
<-ctx.Done()
logger.Debugf("Shutdown timeout of %s", context.Duration("wait-shutdown"))
logger.Debug("Server exiting")
return nil
},
Before: func(context *cli.Context) (err error) {
gin.SetMode(gin.ReleaseMode)
db := databases.MustNewConnection(&config.DefaultConfig.Database)
migrate.DefaultMC.SetConnection(db)
migrate.DefaultMC.Upgrade()
return nil
},
},
}
)