2021-06-18 08:48:36 +00:00
|
|
|
package httpserver
|
|
|
|
|
|
|
|
import (
|
2021-08-02 05:07:28 +00:00
|
|
|
"crypto/tls"
|
2021-06-18 08:48:36 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2022-08-28 17:02:53 +00:00
|
|
|
"time"
|
2021-06-18 08:48:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
|
|
|
bindAddr string
|
|
|
|
bindPort int
|
2021-08-03 14:58:03 +00:00
|
|
|
handler http.Handler
|
2021-06-18 08:48:36 +00:00
|
|
|
|
2021-08-02 05:07:28 +00:00
|
|
|
l net.Listener
|
|
|
|
tlsConfig *tls.Config
|
|
|
|
hs *http.Server
|
2021-06-18 08:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Option func(*Server) *Server
|
|
|
|
|
|
|
|
func New(options ...Option) *Server {
|
|
|
|
s := &Server{
|
|
|
|
bindAddr: "127.0.0.1",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, option := range options {
|
|
|
|
s = option(s)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithBindAddr(addr string) Option {
|
|
|
|
return func(s *Server) *Server {
|
|
|
|
s.bindAddr = addr
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithBindPort(port int) Option {
|
|
|
|
return func(s *Server) *Server {
|
|
|
|
s.bindPort = port
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-28 17:02:53 +00:00
|
|
|
func WithTLSConfig(tlsConfig *tls.Config) Option {
|
2021-08-02 05:07:28 +00:00
|
|
|
return func(s *Server) *Server {
|
|
|
|
s.tlsConfig = tlsConfig
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-18 08:48:36 +00:00
|
|
|
func WithHandler(h http.Handler) Option {
|
|
|
|
return func(s *Server) *Server {
|
2021-08-03 14:58:03 +00:00
|
|
|
s.handler = h
|
2021-06-18 08:48:36 +00:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-02 05:07:28 +00:00
|
|
|
func WithResponse(resp []byte) Option {
|
|
|
|
return func(s *Server) *Server {
|
2021-08-03 14:58:03 +00:00
|
|
|
s.handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2022-08-28 17:02:53 +00:00
|
|
|
_, _ = w.Write(resp)
|
2021-08-02 05:07:28 +00:00
|
|
|
})
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-18 08:48:36 +00:00
|
|
|
func (s *Server) Run() error {
|
|
|
|
if err := s.initListener(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
addr := net.JoinHostPort(s.bindAddr, strconv.Itoa(s.bindPort))
|
|
|
|
hs := &http.Server{
|
2022-08-28 17:02:53 +00:00
|
|
|
Addr: addr,
|
|
|
|
Handler: s.handler,
|
|
|
|
TLSConfig: s.tlsConfig,
|
|
|
|
ReadHeaderTimeout: time.Minute,
|
2021-06-18 08:48:36 +00:00
|
|
|
}
|
2021-08-02 05:07:28 +00:00
|
|
|
|
2021-06-18 08:48:36 +00:00
|
|
|
s.hs = hs
|
2021-08-02 05:07:28 +00:00
|
|
|
if s.tlsConfig == nil {
|
2022-08-28 17:02:53 +00:00
|
|
|
go func() {
|
|
|
|
_ = hs.Serve(s.l)
|
|
|
|
}()
|
2021-08-02 05:07:28 +00:00
|
|
|
} else {
|
2022-08-28 17:02:53 +00:00
|
|
|
go func() {
|
|
|
|
_ = hs.ServeTLS(s.l, "", "")
|
|
|
|
}()
|
2021-08-02 05:07:28 +00:00
|
|
|
}
|
2021-06-18 08:48:36 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Close() error {
|
|
|
|
if s.hs != nil {
|
|
|
|
return s.hs.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) initListener() (err error) {
|
2022-02-09 07:19:35 +00:00
|
|
|
s.l, err = net.Listen("tcp", net.JoinHostPort(s.bindAddr, strconv.Itoa(s.bindPort)))
|
2021-06-18 08:48:36 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) BindAddr() string {
|
|
|
|
return s.bindAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) BindPort() int {
|
|
|
|
return s.bindPort
|
|
|
|
}
|