patchman/client/main.go

55 lines
1.2 KiB
Go
Raw Normal View History

package main
2023-10-22 16:11:13 +02:00
import (
"fmt"
"git.jmbit.de/jmb/patchman/client/api"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
"log"
"net/http"
"os"
)
2023-10-22 16:11:13 +02:00
// loadConfig reads in the Config file(s) and sets defaults.
func loadConfig() {
viper.SetDefault("server.ip", "127.0.0.1")
viper.SetDefault("server.port", 8008)
viper.SetConfigName("client.yaml")
viper.SetConfigType("yaml")
if os.Getenv("GIN_MODE") == "release" {
viper.AddConfigPath("/etc/patchman/")
} else {
viper.AddConfigPath("./")
}
// Read Config and handle produced error, separating out File not found
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// Config file not found; ignore error if desired
} else {
// Config file was found but another error was produced
}
}
2023-10-22 16:11:13 +02:00
}
2023-10-22 16:11:13 +02:00
func setupRouter() *gin.Engine {
router := gin.Default()
// Ping test
router.GET("/ping", func(c *gin.Context) {
c.String(http.StatusOK, "pong\n")
})
return router
}
2023-10-22 16:11:13 +02:00
func main() {
loadConfig()
router := setupRouter()
router = api.GroupV0(router)
err := router.Run(fmt.Sprintf("%s:%d", viper.Get("address"), viper.Get("port")))
if err != nil {
log.Fatal(err)
}
2023-10-22 16:11:13 +02:00
}