142 lines
3.1 KiB
Go
142 lines
3.1 KiB
Go
//go:build !windows
|
||||
|
|
|
|||
|
|
package main
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"errors"
|
|||
|
|
"flag"
|
|||
|
|
"fmt"
|
|||
|
|
"io/fs"
|
|||
|
|
"log/slog"
|
|||
|
|
"net/http"
|
|||
|
|
"os"
|
|||
|
|
"os/signal"
|
|||
|
|
"path/filepath"
|
|||
|
|
"syscall"
|
|||
|
|
|
|||
|
|
"bj_power_mes/internal/handler"
|
|||
|
|
"bj_power_mes/internal/middleware"
|
|||
|
|
|
|||
|
|
xhttp "bj_power_mes/common/httpx"
|
|||
|
|
xlog "bj_power_mes/common/logx"
|
|||
|
|
"bj_power_mes/internal/config"
|
|||
|
|
"bj_power_mes/internal/svc"
|
|||
|
|
|
|||
|
|
"github.com/dromara/carbon/v2"
|
|||
|
|
"github.com/pkg/browser"
|
|||
|
|
"github.com/zeromicro/go-zero/core/conf"
|
|||
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|||
|
|
"github.com/zeromicro/go-zero/rest"
|
|||
|
|
"github.com/zeromicro/go-zero/rest/httpx"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// macOS/Linux 无互斥锁支持,直接返回成功
|
|||
|
|
var ErrAlreadyExists = errors.New("already exists")
|
|||
|
|
|
|||
|
|
const AppName = "bj_power_mes"
|
|||
|
|
|
|||
|
|
var configFile = flag.String("f", "etc/bj_power_mes-api.yaml", "the config file")
|
|||
|
|
|
|||
|
|
var AppPath, _ = filepath.Abs(os.Args[0])
|
|||
|
|
|
|||
|
|
var baseUrl string
|
|||
|
|
|
|||
|
|
func main() {
|
|||
|
|
flag.Parse()
|
|||
|
|
|
|||
|
|
// 业务影响:所有时间显示使用北京时间
|
|||
|
|
carbon.SetDefault(carbon.Default{
|
|||
|
|
Layout: carbon.DateTimeLayout,
|
|||
|
|
Timezone: carbon.PRC,
|
|||
|
|
Locale: "zh-CN",
|
|||
|
|
WeekStartsAt: carbon.Monday,
|
|||
|
|
})
|
|||
|
|
|
|||
|
|
var c config.Config
|
|||
|
|
conf.MustLoad(*configFile, &c)
|
|||
|
|
baseUrl = fmt.Sprintf("http://127.0.0.1:%d", c.Port)
|
|||
|
|
|
|||
|
|
logger := xlog.NewLogger(c.Logger)
|
|||
|
|
xlog.SetDefault(logger)
|
|||
|
|
logx.SetWriter(logger)
|
|||
|
|
|
|||
|
|
// 业务影响:初始化数据库连接、PLC 连接
|
|||
|
|
svcCtx := svc.NewServiceContext(c)
|
|||
|
|
|
|||
|
|
var publicFS http.FileSystem
|
|||
|
|
if _, err := os.Stat("public"); err == nil {
|
|||
|
|
publicFS = http.Dir("public")
|
|||
|
|
} else {
|
|||
|
|
sub, _ := fs.Sub(public, "public")
|
|||
|
|
publicFS = http.FS(sub)
|
|||
|
|
}
|
|||
|
|
spaHandler := xhttp.NewNotFoundHandler(publicFS)
|
|||
|
|
|
|||
|
|
server := rest.MustNewServer(
|
|||
|
|
c.RestConf,
|
|||
|
|
rest.WithCors("*"),
|
|||
|
|
rest.WithNotFoundHandler(spaHandler),
|
|||
|
|
)
|
|||
|
|
logx.DisableStat()
|
|||
|
|
logx.SetWriter(logger)
|
|||
|
|
defer server.Stop()
|
|||
|
|
|
|||
|
|
handler.RegisterHandlers(server, svcCtx)
|
|||
|
|
|
|||
|
|
// 写操作审计中间件(兜底覆盖所有写接口)
|
|||
|
|
server.Use(middleware.WriteAudit(svcCtx))
|
|||
|
|
|
|||
|
|
// 业务影响:统一错误响应格式,前端不用处理多种错误格式
|
|||
|
|
httpx.SetValidator(xhttp.NewValidator())
|
|||
|
|
httpx.SetOkHandler(xhttp.OkJsonCtx)
|
|||
|
|
httpx.SetErrorHandlerCtx(xhttp.ErrorCtx)
|
|||
|
|
|
|||
|
|
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
|
|||
|
|
slog.Info(fmt.Sprintf("Starting server at %s:%d...", c.Host, c.Port))
|
|||
|
|
|
|||
|
|
go server.Start()
|
|||
|
|
|
|||
|
|
_ = browser.OpenURL(baseUrl)
|
|||
|
|
|
|||
|
|
// 阻塞等待退出信号(Ctrl+C / kill)
|
|||
|
|
signalChan := make(chan os.Signal, 1)
|
|||
|
|
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
|
|||
|
|
<-signalChan
|
|||
|
|
|
|||
|
|
slog.Info("shutting down server...")
|
|||
|
|
server.Stop()
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// macOS/Linux 下 Windows 特有功能的空实现
|
|||
|
|
|
|||
|
|
var mutex uintptr
|
|||
|
|
|
|||
|
|
func createMutex(name string) (uintptr, error) {
|
|||
|
|
return 1, nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var procCloseHandle = closeHandleHelper{}
|
|||
|
|
|
|||
|
|
type closeHandleHelper struct{}
|
|||
|
|
|
|||
|
|
func (closeHandleHelper) Call(uintptr) uintptr {
|
|||
|
|
return 0
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func MessageBox(title, message string) {
|
|||
|
|
fmt.Printf("[Dialog] %s: %s\n", title, message)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func setAutoStart(name, path string) error {
|
|||
|
|
slog.Info("setAutoStart not supported on macOS")
|
|||
|
|
return nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func removeAutoStart(name string) error {
|
|||
|
|
slog.Info("removeAutoStart not supported on macOS")
|
|||
|
|
return nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func isAutoStartEnabled(name string) bool {
|
|||
|
|
return false
|
|||
|
|
}
|