potato/initialize/server.go

51 lines
1.1 KiB
Go
Raw Normal View History

2021-03-23 00:55:26 +08:00
/*
* @Date: 2021-03-22 17:03:27
* @LastEditors: viletyy
2021-06-10 22:41:58 +08:00
* @LastEditTime: 2021-06-10 22:24:39
2021-03-23 00:55:26 +08:00
* @FilePath: /potato/initialize/server.go
*/
package initialize
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"time"
"github.com/viletyy/potato/global"
2021-06-10 16:01:23 +08:00
"github.com/viletyy/potato/internal/routers"
2021-03-23 00:55:26 +08:00
)
func RunServer() {
router := routers.InitRouter()
server := &http.Server{
2021-03-24 11:28:57 +08:00
Addr: fmt.Sprintf(":%d", global.GO_CONFIG.Server.HttpPort),
2021-03-23 00:55:26 +08:00
Handler: router,
2021-03-24 11:28:57 +08:00
ReadTimeout: time.Duration(global.GO_CONFIG.Server.ReadTimeout) * time.Second,
WriteTimeout: time.Duration(global.GO_CONFIG.Server.ReadTimeout) * time.Second,
2021-03-23 00:55:26 +08:00
MaxHeaderBytes: 1 << 20,
}
go func() {
if err := server.ListenAndServe(); err != nil {
global.GO_LOG.Info(fmt.Sprintf("Listen: %s\n", err))
}
}()
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt)
<-quit
global.GO_LOG.Info("Shutdown Server")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
global.GO_LOG.Fatal(fmt.Sprintf("Server Shutdown: %v", err))
}
global.GO_LOG.Info("Server exiting")
}