123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- package main
- import (
- "flag"
- "fmt"
- "log"
- "net/http"
- "os"
- "strings"
- // logrus "github.com/sirupsen/logrus"
- "github.com/kardianos/service"
- "kpt.xdmy/apiserver/config"
- "kpt.xdmy/apiserver/dao"
- "kpt.xdmy/apiserver/routers"
- "kpt.xdmy/apiserver/routers/api"
- pservice "kpt.xdmy/apiserver/service"
- "kpt.xdmy/middleware/comm"
- "kpt.xdmy/middleware/mdns"
- plog "kpt.xdmy/pkg/log"
- "kpt.xdmy/pkg/logging"
- "kpt.xdmy/pkg/setting"
- "kpt.xdmy/pkg/util"
- //"unsafe"
- )
- // @title 科湃腾Go语言后台API
- // @version 1.1.2
- // @description kptyun_service
- // @contact.name 朱红宾
- // @contact.url https://www.dairyinfo.com.cn
- // @contact.email kptzhu@163.com
- // @host
- // @BasePath /
- func main() {
- var path string
- // path = "/Users/desire/kptdev/xdmy_mod/cmd/"
- flag.Parse()
- setting.Setup(path)
- dao.Setup()
- routers.InitRouter()
- logging.Setup()
- config.Init(path)
- plog.Init()
- svc := pservice.New(config.Conf)
- routers.Init(svc)
- api.Init(svc)
- svcConfig := &service.Config{
- Name: setting.ServerSetting.ServerName, //服务显示名称
- DisplayName: setting.ServerSetting.DisplayName, //服务名称
- Description: setting.ServerSetting.Description, //服务描述
- }
- prg := &program{}
- s, err := service.New(prg, svcConfig)
- if err != nil {
- fmt.Println(err, "service.New() err", err)
- }
- if len(os.Args) > 1 {
- if os.Args[1] == "install" {
- err = s.Install()
- if err != nil {
- fmt.Println("install err", err)
- } else {
- fmt.Println("install success")
- }
- return
- }
- if os.Args[1] == "remove" {
- err = s.Uninstall()
- if err != nil {
- fmt.Println("Uninstall err", err)
- } else {
- fmt.Println("Uninstall success")
- }
- return
- }
- }
- s.Run()
- }
- type program struct{}
- func (p *program) Start(s service.Service) error {
- go p.run()
- return nil
- }
- func (p *program) run() {
- // 代码写在这儿
- // 邮件定时任务
- if setting.ServerSetting.MailStart == "1" {
- util.CronMail()
- }
- // 企业微信定时任务
- if setting.ServerSetting.WXworkStart == "1" {
- util.CronWXwork()
- }
- // 系统定时任务
- util.CronServer()
- // 现代牧业eas 系统对接初始化
- util.InitWebS(setting.ServerSetting.EasName, setting.ServerSetting.EasPassword, setting.ServerSetting.EasUrl)
- routersInit := routers.InitRouter()
- readTimeout := setting.ServerSetting.ReadTimeout
- writeTimeout := setting.ServerSetting.WriteTimeout
- endPoint := fmt.Sprintf(":%d", setting.ServerSetting.HttpPort)
- maxHeaderBytes := 1 << 20
- server := &http.Server{
- Addr: endPoint,
- Handler: routersInit,
- ReadTimeout: readTimeout,
- WriteTimeout: writeTimeout,
- MaxHeaderBytes: maxHeaderBytes,
- }
- if setting.CommSetting.PortName != "" {
- go comm.OpenComm()
- }
- if strings.Trim(setting.ServerSetting.Mdns_servicename, " ") != "" {
- go mdns.Mdns(strings.Trim(setting.ServerSetting.Mdns_servicename, " "), setting.ServerSetting.Mdns_serviceport)
- }
- httpsPort := fmt.Sprintf(":%d", setting.ServerSetting.HttpsPort)
- if setting.ServerSetting.Https == 1 {
- log.Printf("[info] start https server listening port %s", httpsPort)
- _ = routersInit.RunTLS(httpsPort, setting.ServerSetting.CrtPath, setting.ServerSetting.KeyPath)
- } else if setting.ServerSetting.Https == 2 {
- log.Printf("[info] start https & http server listening port https %s, http %s", httpsPort, endPoint)
- go routersInit.RunTLS(httpsPort, setting.ServerSetting.CrtPath, setting.ServerSetting.KeyPath)
- _ = server.ListenAndServe()
- } else {
- log.Printf("[info] start http server listening port %s", endPoint)
- e := server.ListenAndServe()
- log.Default().Printf("net http listen error: %v", e)
- }
- }
- func (p *program) Stop(s service.Service) error {
- fmt.Println("程序被关闭")
- return nil
- }
|