Skip to content

Commit

Permalink
fix: http server port illgal.
Browse files Browse the repository at this point in the history
  • Loading branch information
auula committed Sep 11, 2023
1 parent 6a90dcc commit f5d9b4a
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,23 @@ var (
)

type HttpServer struct {
http.Server
s *http.Server
shutdown chan struct{}
closed int32
Port int32
port int32
}

// New 创建一个新的 HTTP 服务器
func New(opt *conf.ServerConfig) *HttpServer {

hs := HttpServer{
Server: http.Server{
s: &http.Server{
Handler: router.Root,
Addr: net.JoinHostPort(IPv4, strconv.Itoa(opt.Port)),
WriteTimeout: 3 * time.Second,
ReadTimeout: 3 * time.Second,
},
Port: int32(opt.Port),
port: int32(opt.Port),
shutdown: make(chan struct{}),
}

Expand All @@ -49,7 +49,7 @@ func New(opt *conf.ServerConfig) *HttpServer {

func (hs *HttpServer) Startup() {

if !(hs.Port >= 1024 && hs.Port <= 2^16-1) {
if !(hs.port >= 1024 && hs.port <= 2^16-1) {
clog.Failed("HTTP server port illegal")
}

Expand All @@ -59,14 +59,14 @@ func (hs *HttpServer) Startup() {

go func() {
clog.Info("Receiving client connections")
if err := hs.ListenAndServe(); err != nil {
if err := hs.s.ListenAndServe(); err != nil {
clog.Failed(err)
}
}()

// 防止 HTTP 端口占用,延迟输出启动信息
time.Sleep(500 * time.Millisecond)
clog.Info(fmt.Sprintf("HTTP server started %s:%d 🚀", IPv4, hs.Port))
clog.Info(fmt.Sprintf("HTTP server started %s:%d 🚀", IPv4, hs.port))
atomic.StoreInt32(&hs.closed, 1)

<-hs.shutdown
Expand All @@ -78,7 +78,7 @@ func (hs *HttpServer) Shutdown() {
panic("HTTP server not started")
}

if err := hs.Server.Shutdown(context.Background()); err != nil {
if err := hs.s.Shutdown(context.Background()); err != nil {
clog.Failed(err)
}

Expand Down

0 comments on commit f5d9b4a

Please sign in to comment.