gohttpserver/internal/server/http.go

35 lines
748 B
Go
Raw Normal View History

package server
2020-09-30 06:35:07 +00:00
import (
"fmt"
"net/http"
"time"
)
2022-11-28 03:59:20 +00:00
func (s *Server) serveUntilShutdown() {
2020-09-30 06:35:07 +00:00
listenAddr := fmt.Sprintf(":%d", s.port)
s.httpServer = &http.Server{
Addr: listenAddr,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
Handler: s,
}
// add routes
// this does any necessary setup in each handler
s.routes()
2020-09-30 07:48:56 +00:00
s.log.Info().Str("listenaddr", listenAddr).Msg("http begin listen")
2020-09-30 06:35:07 +00:00
if err := s.httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
2020-09-30 07:48:56 +00:00
s.log.Error().Msgf("listen:%+s\n", err)
2020-09-30 06:35:07 +00:00
if s.cancelFunc != nil {
s.cancelFunc()
}
}
}
2022-11-28 03:59:20 +00:00
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
2020-09-30 06:35:07 +00:00
s.router.ServeHTTP(w, r)
}