3 Star 14 Fork 3

Time/Ehome

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.go 2.24 KB
一键复制 编辑 原始数据 按行查看 历史
[email protected] 提交于 2021-06-14 14:00 . init
package main
import (
"fmt"
"github.com/astaxie/beego"
"github.com/astaxie/beego/plugins/cors"
"github.com/common-nighthawk/go-figure"
"github.com/kqbi/service"
_ "github.com/tsingeye/FreeEhome/routers"
"github.com/tsingeye/FreeEhome/service/udp"
"github.com/tsingeye/FreeEhome/tools"
"github.com/tsingeye/FreeEhome/tools/logs"
"github.com/tsingeye/FreeEhome/tools/sqlDB"
"os"
"time"
)
type program struct {
exit chan bool
}
func (p *program) Start(s service.Service) error {
appPath := tools.GetAbsPath()
os.Chdir(appPath)
beego.SetStaticPath("/", appPath+"www")
go p.run()
return nil
}
func (p *program) run() {
figure.NewFigure("FreeEhome", "", true).Print()
logs.BeeLogger.Info("FreeEHome Service Start!!!")
fmt.Printf("%s FreeEHome Service Start!!!\n", time.Now().Format("2006-01-02 15:04:05"))
//初始化数据库
sqlDB.InitDB()
//开启UDP服务
go udp.ListenUDPServer()
//实现服务端允许跨域
beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
AllowAllOrigins: true,
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowHeaders: []string{"Origin", "Authorization", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type"},
ExposeHeaders: []string{"Content-Length", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type"},
AllowCredentials: true,
}))
beego.Run()
return
}
func (p *program) Stop(s service.Service) error {
logs.BeeLogger.Info("FreeEHome Service Stop!!!")
fmt.Printf("%s FreeEHome Service Stop!!!\n", time.Now().Format("2006-01-02 15:04:05"))
close(p.exit)
if service.Interactive() {
os.Exit(0)
}
return nil
}
func main() {
svcConfig := &service.Config{
Name: "FreeEHome", //服务显示名称
DisplayName: "FreeEHome", //服务名称
Description: "FreeEHome", //服务描述
}
prg := &program{
exit: make(chan bool),
}
s, err := service.New(prg, svcConfig)
if err != nil {
logs.PanicLogger.Fatalln("service.New() error: ", err)
}
if len(os.Args) > 1 {
//install, uninstall, start, stop 的另一种实现方式
err = service.Control(s, os.Args[1])
if err != nil {
logs.PanicLogger.Fatalln(err)
}
return
}
err = s.Run()
if err != nil {
logs.PanicLogger.Fatalln(err)
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/XIAOAIlin/ehome.git
[email protected]:XIAOAIlin/ehome.git
XIAOAIlin
ehome
Ehome
master

搜索帮助