-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |