1
0
dwelling-upload/internal/http/server.go

52 lines
842 B
Go
Raw Normal View History

2023-04-24 00:49:53 +04:00
package http
2022-02-06 02:22:23 +04:00
import (
"context"
"log"
"net"
"net/http"
2022-03-07 23:55:00 +04:00
"os"
2022-02-06 02:22:23 +04:00
"time"
)
type HttpServer struct {
s http.Server
}
func NewHttpServer(r http.Handler) *HttpServer {
return &HttpServer{s: http.Server{
ReadTimeout: 3 * time.Second,
WriteTimeout: 3 * time.Second,
Handler: r}}
2022-02-06 02:22:23 +04:00
}
func (s *HttpServer) Start(network, address string) error {
listener, err := net.Listen(network, address)
if err != nil {
return err
}
2022-03-07 23:55:00 +04:00
if listener.Addr().Network() == "unix" {
os.Chmod(address, 0777)
}
2022-02-06 02:22:23 +04:00
go func() {
if err = s.s.Serve(listener); err != nil && err != http.ErrServerClosed {
2022-02-06 02:22:23 +04:00
log.Fatalln(err)
}
}()
return nil
}
func (s *HttpServer) Stop() error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := s.s.Shutdown(ctx); err != nil {
2022-02-06 02:22:23 +04:00
return err
}
return nil
}