You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
3.1 KiB

4 years ago
package main
import (
4 years ago
"context"
4 years ago
"log"
"net/http"
"os"
4 years ago
"os/signal"
4 years ago
"recook/configs"
"recook/internal/api/manage"
"recook/internal/api/mobile"
_ "recook/internal/api/mobile/bill"
"recook/internal/api/store"
"recook/internal/api/vend"
"recook/internal/cron"
"recook/internal/dbc"
"recook/internal/mq"
"runtime"
4 years ago
"syscall"
4 years ago
"time"
"github.com/gin-gonic/gin"
4 years ago
cron2 "github.com/robfig/cron"
4 years ago
"github.com/shopspring/decimal"
"golang.org/x/sync/errgroup"
)
var (
g errgroup.Group
)
func main() {
4 years ago
//dirs := []string{"credentials"} // 设置需要释放的目录
//for _, dir := range dirs {
// // 解压dir目录到当前目录
// if err := asset.RestoreAssets("./", dir); err != nil {
// log.Println(err)
// break
// }
//}
runtime.GOMAXPROCS(runtime.NumCPU())
4 years ago
decimal.MarshalJSONWithoutQuotes = true
dbc.SetupMysql()
dbc.SetupRedis()
if configs.IsProductionEnv() {
mq.SetUpMq()
}
4 years ago
if configs.IsProductionEnv() {
mq.SetUpShaMa()
}
4 years ago
if configs.IsProductionEnv() {
gin.SetMode("release")
} else {
gin.SetMode("debug")
}
/*将今天的数据copy一份分发到接下来的30天*/
manageServer := &http.Server{
Addr: ":8080",
Handler: manage.Service(),
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
appServer := &http.Server{
Addr: ":8081",
Handler: mobile.Service(),
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
vendServer := &http.Server{
Addr: ":8082",
Handler: vend.Service(),
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
storeServer := &http.Server{
Addr: ":8083",
Handler: store.Service(),
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
h5Server := &http.Server{
Addr: ":8084",
Handler: mobile.Service(),
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
g.Go(func() error {
return manageServer.ListenAndServe()
})
g.Go(func() error {
return appServer.ListenAndServe()
})
g.Go(func() error {
return vendServer.ListenAndServe()
})
g.Go(func() error {
return storeServer.ListenAndServe()
})
4 years ago
c := cron2.New()
4 years ago
4 years ago
idleConnClosed := make(chan struct{})
go func() {
sigint := make(chan os.Signal, 1)
signal.Notify(sigint, syscall.SIGINT, syscall.SIGTERM)
<-sigint
if err := manageServer.Shutdown(context.Background()); err != nil {
log.Printf("HTTP server Shutdown: %v", err)
4 years ago
}
4 years ago
if err := appServer.Shutdown(context.Background()); err != nil {
log.Printf("HTTP server Shutdown: %v", err)
4 years ago
}
4 years ago
if err := vendServer.Shutdown(context.Background()); err != nil {
log.Printf("HTTP server Shutdown: %v", err)
}
if err := storeServer.Shutdown(context.Background()); err != nil {
log.Printf("HTTP server Shutdown: %v", err)
}
if err := h5Server.Shutdown(context.Background()); err != nil {
log.Printf("HTTP server Shutdown: %v", err)
}
4 years ago
_ = mq.Conn.Close()
_ = mq.Conn1.Close()
4 years ago
c.Stop()
4 years ago
close(idleConnClosed)
}()
4 years ago
if configs.IsProductionEnv() {
cron.Task(c)
c.Start()
}
4 years ago
if err := g.Wait(); err != nil {
log.Fatal(err)
}
}