All checks were successful
Template Cleanup / Template Cleanup (push) Has been skipped
81 lines
1.4 KiB
Go
81 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/gin-gonic/gin"
|
|
"io"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"time"
|
|
"zabbixagent2plugintemplate/config"
|
|
"zabbixagent2plugintemplate/constant"
|
|
"zabbixagent2plugintemplate/integration/zabbixagent"
|
|
"zabbixagent2plugintemplate/router"
|
|
)
|
|
|
|
func main() {
|
|
/**
|
|
* Logging
|
|
*/
|
|
logFile := fmt.Sprintf("%s.log", time.Now().Format("2006-01-02"))
|
|
var logFilePath string
|
|
if runtime.GOOS == "linux" {
|
|
logFilePath = fmt.Sprintf("/var/log/%s/%s", constant.AppName, logFile)
|
|
} else {
|
|
logFilePath = filepath.Join("log", logFile)
|
|
}
|
|
|
|
f, err := os.OpenFile(logFilePath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
|
if err != nil {
|
|
log.Fatalf("Error opening file: %v", err)
|
|
}
|
|
|
|
defer func(f *os.File) {
|
|
err := f.Close()
|
|
if err != nil {
|
|
log.Fatalf("Error closing file: %v", err)
|
|
}
|
|
}(f)
|
|
|
|
writer := io.MultiWriter(os.Stdout, f)
|
|
log.SetOutput(writer)
|
|
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
|
|
|
|
/**
|
|
* Load config
|
|
*/
|
|
if err := config.LoadConfig(); err != nil {
|
|
log.Fatalf("Error loading config: %v", err)
|
|
}
|
|
|
|
/**
|
|
* Web server
|
|
*/
|
|
go func() {
|
|
r := gin.Default()
|
|
|
|
router.SetupRoutes(r)
|
|
|
|
addr := fmt.Sprintf("%s:%d", config.Config.App.Host, config.Config.App.Port)
|
|
if err := r.Run(addr); err != nil {
|
|
fmt.Println("Failed to start server:", err)
|
|
}
|
|
}()
|
|
|
|
/**
|
|
* Zabbix agent integration
|
|
*/
|
|
go func() {
|
|
err = zabbixagent.Launch()
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
panic(err)
|
|
}()
|
|
|
|
select {}
|
|
}
|