service

package
v1.7.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 2, 2026 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package service provides a simple and robust way to create, manage, and deploy system services (daemons) in Go applications across different platforms.

This package wraps the kardianos/service library and provides additional functionality for application lifecycle management, including automatic path detection, graceful shutdown handling, and seamless integration with other go-core packages.

The package automatically detects whether the application is running in interactive mode (console/terminal) or as a system service, and adjusts behavior accordingly. When running as a service, it automatically configures the data directory relative to the executable location.

Features:

  • Cross-platform service management (Windows, Linux, macOS)
  • Automatic interactive vs service mode detection
  • Graceful startup and shutdown handling
  • Integration with go-core logging and configuration systems
  • Error handling and recovery mechanisms
  • Support for service installation, uninstallation, and control

Basic Service Example:

package main

import (
	"context"
	"fmt"
	"time"

	"github.com/valentin-kaiser/go-core/service"
	"github.com/valentin-kaiser/go-core/logging"
)

func main() {
	// Configure service
	config := &service.Config{
		Name:        "MyApp",
		DisplayName: "My Application Service",
		Description: "A sample Go application running as a service",
	}

	// Define start function
	start := func(s *service.Service) error {
		fmt.Println("Service starting...")
		// Your application logic here
		go func() {
			for {
				fmt.Println("Service is running...")
				time.Sleep(30 * time.Second)
			}
		}()
		return nil
	}

	// Define stop function
	stop := func(s *service.Service) error {
		fmt.Println("Service stopping...")
		// Cleanup logic here
		return nil
	}

	// Run the service
	if err := service.Run(config, start, stop); err != nil {
		panic(err)
	}
}

Web Server Service Example:

package main

import (
	"context"
	"net/http"
	"time"

	"github.com/valentin-kaiser/go-core/service"
	"github.com/valentin-kaiser/go-core/web"
	"github.com/valentin-kaiser/go-core/logging"
)

func main() {
	config := &service.Config{
		Name:        "WebService",
		DisplayName: "Web Service",
		Description: "HTTP web server running as a system service",
	}

	var server *web.Server

	start := func(s *service.Service) error {
		server = web.New().WithPort(8080)

		server.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
			w.Write([]byte("Service is running!"))
		})

		go func() {
			if err := server.Start(); err != nil {
				logging.GetPackageLogger("main").Error().Err(err).Msg("Server failed")
			}
		}()

		return nil
	}

	stop := func(s *service.Service) error {
		if server != nil {
			return server.Shutdown()
		}
		return nil
	}

	if err := service.Run(config, start, stop); err != nil {
		panic(err)
	}
}

Platform Support:

  • Windows: Runs as Windows Service
  • Linux: Runs as systemd service or SysV init script
  • macOS: Runs as launchd service
  • Development: Runs in interactive mode for testing

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsInteractive

func IsInteractive() bool

IsInteractive returns true if the service is running in interactive mode

func Run

func Run(config *Config, start func(s *Service) error, stop func(s *Service) error) error

Run starts a service with the provided configuration and start/stop functions. It handles signal management and graceful shutdown.

Types

type Config

type Config = service.Config

Config is an alias for service.Config providing system service configuration options. It includes service name, display name, description, dependencies, and platform-specific settings.

type Service

type Service struct {
	service.Service
	// contains filtered or unexported fields
}

Service wraps the kardianos/service.Service interface and provides additional functionality for managing application lifecycle with custom start and stop handlers. It includes error handling and graceful shutdown capabilities.

func (*Service) Start

func (s *Service) Start(svc service.Service) error

Start implements the service.Interface Start method and executes the custom start handler. It is called by the service manager when the service should start.

func (*Service) Stop

func (s *Service) Stop(svc service.Service) error

Stop implements the service.Interface Stop method and executes the custom stop handler. It is called by the service manager when the service should stop gracefully.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL