MCPcopy Create free account
hub / github.com/DoNewsCode/core / httpServe

Method httpServe

serve.go:59–118  ·  view source on GitHub ↗
(ctx context.Context, logger logging.LevelLogger)

Source from the content-addressed store, hash-verified

57type runGroupFunc func(ctx context.Context, logger logging.LevelLogger) (func() error, func(err error), error)
58
59func (s serveIn) httpServe(ctx context.Context, logger logging.LevelLogger) (func() error, func(err error), error) {
60 type httpConfig struct {
61 Disable bool `json:"disable" yaml:"disable"`
62 Addr string `json:"addr" yaml:"addr"`
63 ReadTimeout config.Duration `json:"readTimeout" yaml:"readTimeout"`
64 ReadHeaderTimeout config.Duration `json:"readHeaderTimeout" yaml:"readHeaderTimeout"`
65 WriteTimeout config.Duration `json:"writeTimeout" yaml:"writeTimeout"`
66 IdleTimeout config.Duration `json:"idleTimeout" yaml:"idleTimeout"`
67 MaxHeaderBytes int `json:"maxHeaderBytes" yaml:"maxHeaderBytes"`
68 }
69
70 var conf httpConfig
71 s.Config.Unmarshal("http", &conf)
72
73 if conf.Disable {
74 return nil, nil, nil
75 }
76
77 if s.HTTPServer == nil {
78 s.HTTPServer = &http.Server{
79 ReadTimeout: conf.ReadTimeout.Duration,
80 ReadHeaderTimeout: conf.ReadHeaderTimeout.Duration,
81 WriteTimeout: conf.WriteTimeout.Duration,
82 IdleTimeout: conf.IdleTimeout.Duration,
83 MaxHeaderBytes: conf.MaxHeaderBytes,
84 }
85 }
86 router := mux.NewRouter()
87 applyRouter(s.Container, router)
88
89 router.Walk(func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error {
90 tpl, _ := route.GetPathTemplate()
91 level.Debug(logger).Log("tag", "http", "path", tpl)
92 return nil
93 })
94
95 s.HTTPServer.Handler = router
96 httpAddr := conf.Addr
97 ln, err := net.Listen("tcp", httpAddr)
98 if err != nil {
99 return nil, nil, errors.Wrap(err, "failed start http server")
100 }
101 return func() error {
102 logger.Infof("http service is listening at %s", ln.Addr())
103 s.Dispatcher.Dispatch(
104 ctx,
105 OnHTTPServerStart,
106 OnHTTPServerStartPayload{s.HTTPServer, ln},
107 )
108 defer s.Dispatcher.Dispatch(
109 ctx,
110 OnHTTPServerShutdown,
111 OnHTTPServerShutdownPayload{s.HTTPServer, ln},
112 )
113 return s.HTTPServer.Serve(ln)
114 }, func(err error) {
115 _ = s.HTTPServer.Shutdown(context.Background())
116 _ = ln.Close()

Callers

nothing calls this directly

Calls 10

applyRouterFunction · 0.85
LogMethod · 0.80
ServeMethod · 0.80
ShutdownMethod · 0.80
CloseMethod · 0.80
UnmarshalMethod · 0.65
DebugMethod · 0.65
ListenMethod · 0.65
InfofMethod · 0.65
DispatchMethod · 0.65

Tested by

no test coverage detected