Files
energy-management-system/main.go
2024-08-29 09:05:26 +08:00

143 lines
3.4 KiB
Go

package main
import (
"context"
"energy-management-system/core"
"energy-management-system/global"
"energy-management-system/request"
"energy-management-system/router"
"errors"
"fmt"
"net/http"
"os"
"os/signal"
"time"
)
func init() {
// 配置
core.InitViper()
// 数据库
core.InitGorm()
// gin验证器
request.InitTrans()
// 可选初始化表结构
core.AutoMigrate(global.Db)
// 可选初始化表数据
core.AutoInitDbData()
}
func main() {
r := router.InitRouter()
host := global.AppConf.Service.Http.Host
port := global.AppConf.Service.Http.Port
srv := initServer(host, port, r)
// 启动服务
go startServer(srv, host, port)
// 优雅关闭
gracefulShutdown(srv)
//// 定义一个切片来接收查询结果
//var users []peaks_valleys.PeakValleyTimeBlock
//
//// 查询所有用户
//result := global.Db.Find(&users)
//if result.Error != nil {
// fmt.Println("Error:", result.Error)
//}
//
//// 打印查询结果
//for _, user := range users {
//
// if user.BlockIndex < 20 {
// global.Db.Create(&peaks_valleys.PeakValleyTimeBlockPrice{
// BlockId: user.BlockIndex,
// Price: 35,
// PeakValleyType: 5,
// })
// } else if user.BlockIndex < 40 {
// global.Db.Create(&peaks_valleys.PeakValleyTimeBlockPrice{
// BlockId: user.BlockIndex,
// Price: 45,
// PeakValleyType: 4,
// })
// } else if user.BlockIndex < 60 {
// global.Db.Create(&peaks_valleys.PeakValleyTimeBlockPrice{
// BlockId: user.BlockIndex,
// Price: 55,
// PeakValleyType: 3,
// })
// } else if user.BlockIndex < 80 {
// global.Db.Create(&peaks_valleys.PeakValleyTimeBlockPrice{
// BlockId: user.BlockIndex,
// Price: 65,
// PeakValleyType: 2,
// })
// } else if user.BlockIndex < 100 {
// global.Db.Create(&peaks_valleys.PeakValleyTimeBlockPrice{
// BlockId: user.BlockIndex,
// Price: 85,
// PeakValleyType: 1,
// })
// } else if user.BlockIndex <= 144 {
// global.Db.Create(&peaks_valleys.PeakValleyTimeBlockPrice{
// BlockId: user.BlockIndex,
// Price: 35,
// PeakValleyType: 5,
// })
// }
//
// //fmt.Printf("ID: %d, Name: %s, Email: %s\n", user.BlockIndex)
//}
}
//func loadConfig() (host, port string) {
// host = viper.GetString("service.http.host")
// if host == "" {
// host = "localhost"
// fmt.Println("Using default host:", host)
// }
//
// port = viper.GetString("service.http.port")
// if port == "" {
// port = "9999"
// fmt.Println("Using default port:", port)
// }
//
// return host, port
//}
func initServer(host, port string, handler http.Handler) *http.Server {
return &http.Server{
Addr: fmt.Sprintf("%s:%s", host, port),
Handler: handler,
ReadTimeout: 60 * time.Second,
WriteTimeout: 60 * time.Second,
}
}
func startServer(srv *http.Server, host, port string) {
fmt.Println("Listening and serving HTTP on", host, ":", port)
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
fmt.Printf("listen: %s\n", err)
}
}
func gracefulShutdown(srv *http.Server) {
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
fmt.Println("Shutdown Server ...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
fmt.Println("Server Shutdown:", err)
}
fmt.Println("Server exiting")
}