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)
|
||||||
|
}
|
||||||
8
interval/api/handler/health.go
Normal file
8
interval/api/handler/health.go
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package handler
|
||||||
|
|
||||||
|
import "net/http"
|
||||||
|
|
||||||
|
func Health(w http.ResponseWriter, _ *http.Request) {
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
w.Write([]byte("OK"))
|
||||||
|
}
|
||||||
16
interval/api/router.go
Normal file
16
interval/api/router.go
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"git.jinshen.cn/remilia/push-server/interval/api/handler"
|
||||||
|
"github.com/go-chi/chi/v5"
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewRouter() http.Handler {
|
||||||
|
r := chi.NewRouter()
|
||||||
|
|
||||||
|
r.Post("/health", handler.Health)
|
||||||
|
|
||||||
|
return r
|
||||||
|
}
|
||||||
27
interval/server/http.go
Normal file
27
interval/server/http.go
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type HTTPServer struct {
|
||||||
|
server *http.Server
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHTTPServer(addr string, handler http.Handler) *HTTPServer {
|
||||||
|
return &HTTPServer{
|
||||||
|
server: &http.Server{
|
||||||
|
Addr: addr,
|
||||||
|
Handler: handler,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *HTTPServer) Start() error {
|
||||||
|
return s.server.ListenAndServe()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *HTTPServer) Shutdown(ctx context.Context) error {
|
||||||
|
return s.server.Shutdown(ctx)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user