Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
quan-xie committed Jan 27, 2024
1 parent 52e5216 commit b333f18
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions transport/httpserver/http_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package httpserver

import (
"context"
"net/http"
"time"

"github.com/quan-xie/tuba/log"
"github.com/quan-xie/tuba/util/xtime"
)

type Config struct {
Addr string
Handler http.Handler
ReadTimeout xtime.Duration
WriteTimeout xtime.Duration
}

var httpServer *http.Server

func Run(c *Config) {
httpServer = &http.Server{
Addr: c.Addr,
Handler: c.Handler,
ReadTimeout: time.Duration(c.ReadTimeout),
WriteTimeout: time.Duration(c.WriteTimeout),
}

go func() {
log.Infof("http server succeed listening at %v", httpServer.Addr)
if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}()
}

func Stop(ctx context.Context) {
if err := httpServer.Shutdown(ctx); err != nil {
log.Errorf("http server stop error %v", err)
}
}

0 comments on commit b333f18

Please sign in to comment.