feat: 基本HTTP服务器
- REST API: health用于检测服务器REST API正常运行
This commit is contained in:
43
cmd/server/main.go
Normal file
43
cmd/server/main.go
Normal file
@ -0,0 +1,43 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"git.jinshen.cn/remilia/push-server/interval/api"
|
||||
"git.jinshen.cn/remilia/push-server/interval/server"
|
||||
)
|
||||
|
||||
func main() {
|
||||
_, serverCancel := context.WithCancel(context.Background())
|
||||
defer func() {
|
||||
serverCancel()
|
||||
}()
|
||||
|
||||
httpServer := server.NewHTTPServer(":8080", api.NewRouter())
|
||||
|
||||
go func() {
|
||||
log.Println("Starting HTTP server on :8080")
|
||||
if err := httpServer.Start(); err != nil && err != http.ErrServerClosed {
|
||||
log.Fatalf("HTTP server error: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
sig := make(chan os.Signal, 1)
|
||||
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
|
||||
<-sig
|
||||
|
||||
log.Println("Shutting down server...")
|
||||
|
||||
serverCancel()
|
||||
|
||||
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), time.Second*10)
|
||||
defer shutdownCancel()
|
||||
|
||||
httpServer.Shutdown(shutdownCtx)
|
||||
}
|
||||
Reference in New Issue
Block a user