/
main.go
75 lines (61 loc) · 1.93 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
73
74
75
package main
import (
"os"
"time"
"github.com/AlexandrGurkin/common/consts"
"github.com/AlexandrGurkin/common/middlewares"
"github.com/AlexandrGurkin/common/xlog"
"github.com/AlexandrGurkin/common/xlog/xzerolog"
"github.com/AlexandrGurkin/http_template/cmd"
"github.com/AlexandrGurkin/http_template/internal/handlers"
"github.com/AlexandrGurkin/http_template/restapi"
"github.com/AlexandrGurkin/http_template/restapi/operations"
"github.com/go-openapi/loads"
"github.com/spf13/cobra"
_ "net/http/pprof"
)
var runCmd = &cobra.Command{
Use: "run",
Short: "run callback",
Long: `run callback`,
Run: runMain,
}
func init() {
cmd.RootCmd.AddCommand(runCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// runCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// runCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
func runMain(cmd *cobra.Command, args []string) {
logger := xzerolog.NewXZerolog(xlog.LoggerCfg{
Level: "trace",
Out: os.Stdout,
})
var err error
swaggerSpec, err := loads.Embedded(restapi.SwaggerJSON, restapi.FlatSwaggerJSON)
if err != nil {
logger.Fatal(err.Error())
}
api := operations.NewTemplateForHTTPServerAPI(swaggerSpec)
server := restapi.NewServer(api)
server.Host = "0.0.0.0"
server.Port = 8099
restapi.SetMiddlewareConfig(middlewares.MiddlewareConfig{Logger: logger, Pprof: true})
api.Logger = func(s string, i ...interface{}) {
logger.WithXFields(xlog.Fields{consts.FieldModule: "swagger_api_logger"}).
Infof(s, i...)
}
api.VersionGetVersionHandler = handlers.VersionHandler{}
server.ConfigureAPI()
server.KeepAlive = 10 * time.Second
if err = server.Serve(); err != nil {
logger.Fatal(err.Error())
}
}
func main() {
cmd.Execute()
}