Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to enable the service? #319

Open
shrinidhi111 opened this issue Mar 8, 2022 · 5 comments
Open

How to enable the service? #319

shrinidhi111 opened this issue Mar 8, 2022 · 5 comments

Comments

@shrinidhi111
Copy link

I noticed in Cent OS 7 after installing the service it won't get enabled.

Is there something different I need to do?

For now I'm manually running systemctl command to enable it

@rngallen
Copy link

rngallen commented Nov 6, 2022

Override install function to run service after installation

@sandisahdewo
Copy link

@rngallen could you give me an example, please?

@rngallen
Copy link

rngallen commented Nov 11, 2022

@rngallen could you give me an example, please?

package main

import (
	"flag"
	"fmt"

	"log"
	"os"

	"github.com/dgrijalva/jwt-go"
	"github.com/goccy/go-json"
	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/middleware/logger"
	"github.com/kardianos/service"
	"github.com/rngallen/stockIntegration/pkg/config"
	"github.com/rngallen/stockIntegration/pkg/database"
	"github.com/rngallen/stockIntegration/services/logs"
	"github.com/rngallen/stockIntegration/services/middleware"
)

type program struct{}

func (p *program) Start(s service.Service) error {
	// Should be non-blocking, so run async using goroutine
	go p.run()
	return nil
}

// run will be called by Start() so business logic goes here
func (p *program) run() {
	app := fiber.New(fiber.Config{
		Prefork:                 true,
		CaseSensitive:           true,
		StrictRouting:           true,
		ServerHeader:            "Stock Integration",
		AppName:                 "Stock Integration v1.0.0",
		JSONEncoder:             json.Marshal,
		JSONDecoder:             json.Unmarshal,
		EnableTrustedProxyCheck: true,
	})

	// Load Environment Variables
	config.LoadConfig()

	// Application Base Middleware
	middleware.BaseMiddleware(app)

	// Logs
	dir := fmt.Sprintf("%v/stock.log", config.Conf.App.Dir)
	file, err := os.OpenFile(dir, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
	if err != nil {
		log.Fatal(err.Error())
	}
	defer file.Close()

	// Logger Middleware
	app.Use(logger.New(logger.Config{
		Output:     file,
		Format:     "[${time}] ${ip}:${port} -${protocol} -${method} -${url} -${status} - ${latency}\n",
		TimeFormat: "02-01-2006 15:04:05",
	}))

	// Connect Database
	database.ConnectDb()

	app.Get("/logs", func(c *fiber.Ctx) error {
		return c.SendFile(fmt.Sprintf("%v/logs.log", config.Conf.App.Dir))

	})


	logs.ErrorLogger.Fatal(app.Listen(config.Conf.App.Port))
}

func (p *program) Stop(s service.Service) error {
	// should be non-blocking
	return nil
}

func main() {
	var mode string
	flag.StringVar(&mode, "mode", "", "install/restart/run/start/stop/uninstall")
	flag.Parse()

	svConfig := &service.Config{
		Name:        "stockApp",
		DisplayName: "Stock & Sage Integration v1.0.0",
		Description: "Integrating Stock application and Sage 300 v1.0.0",
	}

	prg := &program{}
	s, err := service.New(prg, svConfig)
	if err != nil {
		log.Fatal(err)
	}

	switch mode {
	case "run":
		if err = s.Run(); err != nil {
			log.Fatal(err)
		}
	case "start":
		if err = s.Start(); err != nil {
			log.Fatal(err)
		}

	case "stop":
		if err = s.Stop(); err != nil {
			log.Fatal(err)
		}

	case "restart":
		if err = s.Restart(); err != nil {
			log.Fatal(err)
		}

	case "install":    # <== when you install it will install first then it will start
		if err = s.Install(); err != nil {
			log.Fatal(err)
		}
		if err = s.Start(); err != nil {
			log.Fatal(err)
		}

	case "uninstall":
		s.Stop()

		if err = s.Uninstall(); err != nil {
			log.Fatal(err)
		}

	default:
		if err = s.Run(); err != nil {
			log.Fatal(err)
		}
	}
}

compile you code e.g go build -o sample.exe
then run on command line sample.exe --mode==install
It will install and start service automatically

@shrinidhi111
Copy link
Author

Thanks for the comments above,

in my case I'm handling starting of service post install, but in Cent OS 7 for example it doesn't get enabled

Enabled means the service will auto start during system startup

In other common distros this is not a problem for me

@mitchellwarr
Copy link

This REALLY needs to be in the docs or examples

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants