123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- package main
- import (
- "fmt"
- "log"
- "net/http"
- "os"
- "strings"
- "./comm"
- "./mdns"
- "./models"
- "./pkg/logging"
- "./pkg/setting"
- "./pkg/util"
- "./routers"
- "github.com/kardianos/service"
- //"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() {
- setting.Setup("")
- models.Setup()
- logging.Setup()
- 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)
- _ = server.ListenAndServe()
- }
- }
- func (p *program) Stop(s service.Service) error {
- fmt.Println("程序被关闭")
- return nil
- }
|