54 lines
1023 B
Go
54 lines
1023 B
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"os/signal"
|
|
"time"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
"myschools.me/suguo/ddns/gin"
|
|
"myschools.me/suguo/ddns/mysql"
|
|
)
|
|
|
|
func main() {
|
|
mysql.Init(&mysql.Config{
|
|
ConnString: os.Getenv("MYSQL_DSN"),
|
|
ConnMaxLifetime: 600,
|
|
MaxIdleConns: 1,
|
|
MaxOpenConns: 5,
|
|
InitTable: true,
|
|
})
|
|
|
|
if err := mysql.InitTable(); err != nil {
|
|
logrus.WithFields(logrus.Fields{
|
|
"app": "ddns",
|
|
"func": "main",
|
|
}).Panicf("mysql.InitTabl: %s", err.Error())
|
|
}
|
|
|
|
gin.Service(&gin.Config{
|
|
RootPath: "/ddns",
|
|
Addr: "0.0.0.0",
|
|
Port: 8080,
|
|
Ssl: false,
|
|
})
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
"app": "ddns",
|
|
"func": "main",
|
|
}).Infoln("service is running...")
|
|
|
|
// 服务停止相应
|
|
c := make(chan os.Signal, 1)
|
|
signal.Notify(c, os.Interrupt)
|
|
<-c
|
|
_, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
|
defer cancel()
|
|
logrus.WithFields(logrus.Fields{
|
|
"app": "ddns",
|
|
"func": "main",
|
|
}).Infoln("service is stopped.")
|
|
os.Exit(0)
|
|
}
|