You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.3 KiB
Go
54 lines
1.3 KiB
Go
package internal
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"github.com/zeromicro/go-zero/core/proc"
|
|
)
|
|
|
|
// StartOption defines the method to customize http.Server.
|
|
type StartOption func(svr *http.Server)
|
|
|
|
// StartHttp starts a http server.
|
|
func StartHttp(host string, port int, handler http.Handler, opts ...StartOption) error {
|
|
return start(host, port, handler, func(svr *http.Server) error {
|
|
return svr.ListenAndServe()
|
|
}, opts...)
|
|
}
|
|
|
|
// StartHttps starts a https server.
|
|
func StartHttps(host string, port int, certFile, keyFile string, handler http.Handler,
|
|
opts ...StartOption) error {
|
|
return start(host, port, handler, func(svr *http.Server) error {
|
|
// certFile and keyFile are set in buildHttpsServer
|
|
return svr.ListenAndServeTLS(certFile, keyFile)
|
|
}, opts...)
|
|
}
|
|
|
|
func start(host string, port int, handler http.Handler, run func(svr *http.Server) error,
|
|
opts ...StartOption) (err error) {
|
|
server := &http.Server{
|
|
Addr: fmt.Sprintf("%s:%d", host, port),
|
|
Handler: handler,
|
|
}
|
|
for _, opt := range opts {
|
|
opt(server)
|
|
}
|
|
|
|
waitForCalled := proc.AddWrapUpListener(func() {
|
|
if e := server.Shutdown(context.Background()); e != nil {
|
|
logx.Error(e)
|
|
}
|
|
})
|
|
defer func() {
|
|
if err == http.ErrServerClosed {
|
|
waitForCalled()
|
|
}
|
|
}()
|
|
|
|
return run(server)
|
|
}
|