golang gin windows service

Golang Gin Api become Windows Service Use github.com/kardianos/service package main import ( "errors" "fmt" "io" "log" "net" "net/http" "os" "path/filepath" "strings" "time" "github.com/gin-gonic/gin" "github.com/joho/godotenv" "github.com/kardianos/service" ) // Process Windows Service // github.com/kardianos/service var logger service.Logger type program struct{} func (p *program) Start(s service.Service) error { // Start should not block. Do the actual work async. go p.run() return nil } func (p *program) Stop(s service.Service) error { // Stop should not block. Return with a few seconds. return nil } func ExecPath() string { ex, err := os.Executable() if err != nil { panic(err) } exPath := filepath.Dir(ex) return exPath } func main() { File, err := os.Create(ExecPath() + "\\gin_service_run.log") if err != nil { File = os.Stdout } defer File.Close() log.SetOutput(File) var s service.Service svcConfig := service.Config{ Name: "GinAPI", DisplayName: "Gin API Service", Description: "Gin API Server Converter Windows Service", } prg := &program{} s, err = service.New(prg, &svcConfig) if err != nil { log.Fatalln(err) } logger, err = s.Logger(nil) if err != nil { log.Fatalln(err) } if len(os.Args) > 1 { switch os.Args[1] { case "install": err := s.Install() if err != nil { logger.Errorf("Install Service Fail!", err.Error()) return } fmt.Printf("Install Service Success!") case "uninstall": err := s.Uninstall() if err != nil { logger.Errorf("Uninstall Service Fail!", err.Error()) return } fmt.Printf("Uninstall Service Success!") case "start": err := s.Start() if err != nil { logger.Errorf("Start Service Fail!", err.Error()) return } fmt.Printf("Start Service Success!") case "stop": err := s.Stop() if err != nil { logger.Errorf("Stop Service Fail!", err.Error()) return } fmt.Printf("Stop Service Success!") } return } err = s.Run() if err != nil { logger.Error(err) } } func (p *program) run() { // // Create Error logger // errFile, err := os.OpenFile(ExecPath()+"\\errors.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) if err != nil { log.Fatalf("打開日誌文件失敗:", err) } Error = log.New(io.MultiWriter(errFile, os.Stderr), "【Error】", log.Ldate|log.Ltime|log.Lshortfile) // // Get .env // err = godotenv.Load(ExecPath() + "\\.env") if err != nil { msg := "找不到 .env 檔案" logger.Errorf(msg, err) Error.Printf(msg, err) } web_port := os.Getenv("WEB_PORT") // // Start Gin // router := gin.Default() corsConfig := cors.DefaultConfig() corsConfig.AllowAllOrigins = true router.Use(cors.New(corsConfig)) router.GET("/", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"status": "Gin Api Success!"}) }) log.Fatal(http.ListenAndServe(":"+web_port, router)) } 1. run server.exe can run Gin Api Server right now. 2. run server install, server uninstall, server start, server stop. 2.1 【Install and Start】run server install Then server start 2.2 【Stop and uninstall】run server stop Then server uninstall 3. log file use absolute path. ExecPath() can get absolute path. 4. .env must use absolute path. Try to use svcConfig.EnvVars = map[string]string{ "WEB_PORT": web port } see register Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\GinAPI have EnvVars Data. But err := godotenv.Load() can’t get anything, just No Error. So must use absolute path. ...

2023-01-11 · 3 min · 472 words · Me

可參考的gin架構

https://marcoma.xyz/2019/02/23/gin-tutorial-1/

2020-01-22 · 1 min · word · Me

golang loggin gin

https://github.com/uber-go/zap https://marcoma.xyz/2019/03/17/gin-tutorial-7/ https://github.com/natefinch/lumberjack https://juejin.im/post/5d3932bde51d454f73356e2d

2020-01-21 · 1 min · 4 words · Me

gin-jwt 又更新了 renew again

1、Authenticator old:Authenticator: func(userId string, password string, c *gin.Context) New: ============================ `` type login struct { Username string `form:"username" json:"username" binding:"required"` Password string `form:"password" json:"password" binding:"required"` } Authenticator: func(c *gin.Context) (interface{}, error) { var loginVals login if err := c.ShouldBind(&loginVals); err != nil { return "", jwt.ErrMissingLoginValues } userID := loginVals.Username password := loginVals.Password `` 2、authMiddleware old: authMiddleware := jwt.GinJWTMiddleware{ New: authMiddleware, err := jwt.New(&jwt.GinJWTMiddleware{

2018-10-29 · 1 min · 64 words · Me

[轉]Building a single binary containing templates

https://github.com/gin-gonic/gin/blob/master/examples/assets-in-binary/README.md

2018-07-17 · 1 min · word · Me