// service.go package main import ( "fmt" "os" "os/exec" "os/user" "path/filepath" "text/template" ) const serviceTemplate = `[Unit] Description=Blog Server After=network.target [Service] Type=simple User={{.User}} WorkingDirectory={{.WorkDir}} ExecStart={{.BinaryPath}} Restart=always RestartSec=5 [Install] WantedBy=multi-user.target ` type ServiceConfig struct { User string WorkDir string BinaryPath string } func InstallService() { // 1. Get Absolute Path to Binary binPath, err := os.Executable() if err != nil { fmt.Printf("Error getting executable path: %v\n", err) return } binPath, _ = filepath.Abs(binPath) // 2. Get Working Directory workDir, err := os.Getwd() if err != nil { fmt.Printf("Error getting working directory: %v\n", err) return } // 3. Determine User // If running as sudo, try to get the original user, otherwise use current username := os.Getenv("SUDO_USER") if username == "" { u, err := user.Current() if err == nil { username = u.Username } else { username = "root" } } config := ServiceConfig{ User: username, WorkDir: workDir, BinaryPath: binPath, } // 4. Generate Content tmpl, err := template.New("service").Parse(serviceTemplate) if err != nil { panic(err) } serviceFile := "/etc/systemd/system/blog.service" // Check if we have write permissions to /etc/systemd/system f, err := os.OpenFile(serviceFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) if err != nil { // Permission denied or other error: Print instructions instead fmt.Println("---------------------------------------------------") fmt.Println("Run the following command with sudo to install:") fmt.Println("---------------------------------------------------") fmt.Printf("sudo bash -c 'cat > %s <