master
Service Manager
A flexible service management library for Go applications.
Features
- Full service lifecycle management (install, uninstall, start, stop, restart)
- Multi-language support (English/Chinese built-in)
- Rich command-line parameter system
- Configuration persistence
- Detailed build information support
- Colorful console output
- Windows service support
Installation
go get github.com/darkit/service
Requirements
- Go 1.16 or higher
- Windows, Linux, or macOS
Dependencies
- github.com/kardianos/service
- github.com/fatih/color
Quick Start
package main
import (
"log"
"github.com/darkit/service"
)
func main() {
svc, err := service.New(&service.Options{
Name: "myapp",
DisplayName: "My Application",
Description: "This is my application service",
Version: "1.0.0",
})
if err != nil {
log.Fatal(err)
}
if err := svc.Run(); err != nil {
log.Fatal(err)
}
}
Command Line Usage
# Show help
./myapp -h
# Install service
./myapp install
# Start service
./myapp start
# Stop service
./myapp stop
# Show status
./myapp status
# Uninstall service
./myapp uninstall
# Use Chinese language
./myapp --lang zh install
Parameter System
The library provides a flexible parameter management system:
// Add parameters
svc.ParamManager().AddParam(&service.Parameter{
Name: "config",
Short: "c",
Long: "config",
Description: "Config file path",
Required: true,
})
// Add enum parameter
svc.ParamManager().AddParam(&service.Parameter{
Name: "mode",
Long: "mode",
Description: "Run mode",
Default: "prod",
EnumValues: []string{"dev", "test", "prod"},
})
Multi-language Support
Built-in support for English and Chinese, easy to add more languages:
// Add new language
svc.AddLanguage("fr", service.Messages{
Install: "Installer le service",
Uninstall: "Désinstaller le service",
Start: "Démarrer le service",
Stop: "Arrêter le service",
// ...
})
// Switch language
svc.SetLanguage("fr")
Build Information
Support for detailed build information:
svc, err := service.New(&service.Options{
Name: "myapp",
Version: "1.0.0",
BuildInfo: &service.BuildInfo{
Version: "1.0.0",
BuildTime: time.Now(),
GoVersion: runtime.Version(),
GitCommit: "abc123",
GitBranch: "main",
GitTag: "v1.0.0",
Debug: true,
},
})
Configuration Persistence
All parameters and settings are automatically persisted:
// Save current configuration
if err := svc.SaveConfig(); err != nil {
log.Fatal(err)
}
// Load saved configuration
if err := svc.LoadConfig(); err != nil {
log.Fatal(err)
}
// Access custom configuration
svc.SetConfigValue("custom_key", "custom_value")
value, exists := svc.GetConfigValue("custom_key")
Service Events
Support for service lifecycle events:
svc, err := service.New(&service.Options{
Name: "myapp",
Run: func() error {
// Service run logic
return nil
},
Stop: func() error {
// Service stop logic
return nil
},
})
Advanced Usage
See examples directory for more advanced usage examples.
Development
Requirements for development:
- Go 1.16 or higher
- Make (for build scripts)
- Git
# Clone repository
git clone https://github.com/darkit/service.git
# Install dependencies
go mod tidy
# Run tests
go test ./...
Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -am 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Description
Languages
Go
100%