44 lines
907 B
Go
44 lines
907 B
Go
package core
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"gitlink.org.cn/JointCloud/pcm-hpc/global"
|
|
"gitlink.org.cn/JointCloud/pcm-hpc/routers"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"time"
|
|
)
|
|
|
|
func RunServer() {
|
|
addr := fmt.Sprintf("%s:%d", global.PCM_CONFIG.System.Host, global.PCM_CONFIG.System.Port)
|
|
router := routers.InitRouter()
|
|
srv := http.Server{
|
|
Addr: addr,
|
|
Handler: router,
|
|
ReadTimeout: 120 * time.Second,
|
|
WriteTimeout: 120 * time.Second,
|
|
MaxHeaderBytes: 1 << 20,
|
|
}
|
|
|
|
go func() {
|
|
// 服务连接
|
|
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
|
|
}
|
|
}()
|
|
|
|
// 等待中断信号以优雅地关闭服务器(设置 5 秒的超时时间)
|
|
quit := make(chan os.Signal)
|
|
signal.Notify(quit, os.Interrupt)
|
|
<-quit
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
if err := srv.Shutdown(ctx); err != nil {
|
|
|
|
}
|
|
|
|
}
|