patchman/server/main.go

61 lines
1.4 KiB
Go

package main
import (
"fmt"
"git.jmbit.de/jmb/patchman/server/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", 8443)
viper.SetDefault("address", "0.0.0.0")
viper.SetDefault("server.ip", "127.0.0.1")
viper.SetConfigName("server.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 {
err := viper.WriteConfig()
if err != nil {
log.Printf("Could not write config file. Running with default configuration: %w", err)
}
// Config file not found; ignore error if desired
} else {
// Config file was found but another error was produced
log.Fatalf("Could not load config: %w", err)
}
}
}
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)
}
}