diff --git a/main.go b/main.go index 563dfec..ab915a0 100644 --- a/main.go +++ b/main.go @@ -2,7 +2,9 @@ package main import ( "log" + "net/http" "strconv" + "time" "toutoukan/controllers/search" "toutoukan/init/bleveInit" "toutoukan/init/config" @@ -28,9 +30,15 @@ func main() { panic(err) } r := router.SetupRouter() - err := r.Run(":" + strconv.Itoa(config.Conf.Port)) - if err != nil { - return + server := &http.Server{ + Addr: ":" + strconv.Itoa(config.Conf.Port), + Handler: r, + ReadTimeout: 5 * time.Second, // 客户端读取超时 + WriteTimeout: 10 * time.Second, // 服务端写入超时 + IdleTimeout: 30 * time.Second, // 空闲连接超时 + } + if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed { + log.Fatalf("Server failed to start: %v", err) } defer func() { sqlDB, err := databaseInit.UserDB.DB()