123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- package main
- import (
- "fmt"
- "log"
- "net/http"
- _ "net/http/pprof"
- "os"
- "strings"
- "./models"
- "./pkg/logging"
- "./pkg/setting"
- "./routers"
- "./routers/api"
- "github.com/kardianos/service"
- "github.com/kptyun/KPTCOMM/mdns"
- //"unsafe"
- )
- // sss
- // @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 {
- // ceshi
- fmt.Println("install err 1", err)
- } else {
- fmt.Println("install success")
- }
- return
- }
- // 服务移除
- if os.Args[1] == "remove" {
- err = s.Uninstall()
- if err != nil {
- fmt.Println("Uninstall err3123123", 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() {
- // 代码写在这儿
- //路由初始化
- routersInit := routers.InitRouter()
- if setting.ServerSetting.GRFD == "1" { //格润富德定时任务
- api.CronGRFD()
- }
- 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)
- // 开启http/https服务
- 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 https server listening port %s", endPoint)
- _ = server.ListenAndServe()
- }
- }
- func (p *program) Stop(s service.Service) error {
- fmt.Println("程序被关闭")
- return nil
- }
|