57 lines
1.2 KiB
Go
57 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"git.jmbit.de/jmb/patchman/client/api"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/spf13/viper"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
)
|
|
|
|
// loadConfig reads in the Config file(s) and sets defaults.
|
|
func loadConfig() {
|
|
viper.SetDefault("port", 8008)
|
|
viper.SetDefault("address", "0.0.0.0")
|
|
viper.SetDefault("server.ip", "127.0.0.1")
|
|
viper.SetDefault("server.port", 443)
|
|
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
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
func setupRouter() *gin.Engine {
|
|
router := gin.Default()
|
|
|
|
// Ping test
|
|
router.GET("/ping", func(c *gin.Context) {
|
|
c.String(http.StatusOK, "pong\n")
|
|
})
|
|
|
|
return router
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|