From: christiangoeschel Date: Mon, 30 Sep 2024 04:23:07 +0000 (+0000) Subject: Added config file parsing functionality X-Git-Url: https://git.christiangoeschel.com/?a=commitdiff_plain;p=repoman.git Added config file parsing functionality --- diff --git a/cmd/api-server/main.go b/cmd/api-server/main.go index e5eb18b..6446e6f 100644 --- a/cmd/api-server/main.go +++ b/cmd/api-server/main.go @@ -6,8 +6,23 @@ import ( "git.christiangoeschel.com/repoman/cmd/healthchecker" "git.christiangoeschel.com/repoman/cmd/configparse" "os" + "fmt" ) +type APIServerInfo struct { + + Version string `json:"Version"` +} + +func getAPIServerInfo(c *gin.Context) { + + apiInfo := APIServerInfo { + Version: "0.1.0924 beta", + } + + c.IndentedJSON(http.StatusOK, apiInfo) + +} func reportHealthStatus(c *gin.Context) { @@ -28,18 +43,54 @@ func reportHealthStatus(c *gin.Context) { } +func setupNetSocket(c *configparse.Config) (netSocket string, err error){ + + netSocket = fmt.Sprintf("%s:%d", c.Network.Host, c.Network.ListenPort) + + return netSocket, nil +} + +func startServer(c *configparse.Config, netSocket string, r *gin.Engine) (err error){ + + if c.Network.EnableSSL == true { + err = r.RunTLS((netSocket), c.SSL.CertFile, c.SSL.KeyFile) + if err != nil { + return err + } + } else { + err = r.Run(netSocket) + if err != nil { + return err + } + } + + return nil +} + func main() { - var TLSCertificates *configparse.TLSCertificates + var config = configparse.Config{} - TLSCertificates, err := configparse.GetTLSCertificates() + err := configparse.GetConfig( &config ) if err != nil { + fmt.Printf("Failed to parse config file\nDetails: %s", err) os.Exit(1) } router := gin.Default() + router.GET("/info", getAPIServerInfo) router.GET("/status", reportHealthStatus) - router.RunTLS(("0.0.0.0:8443"), TLSCertificates.TLS.CertFile, TLSCertificates.TLS.KeyFile) + + netSocket, err := setupNetSocket(&config) + if err != nil { + fmt.Printf("Failed to setup network socket\nDetails: %s\n", err) + os.Exit(1) + } + err = startServer(&config, netSocket, router) + if err != nil { + fmt.Printf("Failed to start API server\nDetails: %s\n", err) + os.Exit(1) + } } diff --git a/cmd/configparse/main.go b/cmd/configparse/main.go index cf072b9..3661ded 100644 --- a/cmd/configparse/main.go +++ b/cmd/configparse/main.go @@ -4,28 +4,41 @@ import ( "github.com/go-ini/ini" ) -type TLSCertificates struct { - TLS struct { - CertFile string `ini:"CertFile"` - KeyFile string `ini:"KeyFile"` - } `ini:"TLS"` +// Stores the SSL certificates' filesystem locations +type Config struct { -} + SSL struct { + CertFile string `ini:"CertFile"` + KeyFile string `ini:"KeyFile"` + } `ini:"SSL"` + + Network struct { + Host string `ini:"Host"` + ListenPort int `ini:"ListenPort"` + EnableSSL bool `ini:"EnableSSL"` + }`ini:"Network"` -var TLSCerts TLSCertificates + Repo struct { + DefaultGitDirectory string `ini:"DefaultGitDirectory"` + } `ini:"Repo"` -func GetTLSCertificates() (tlsCerts *TLSCertificates ,err error) { +} + +// Extracts the configured filesystem locations for the SSL certificates +func GetConfig( c *Config ) (err error) { + iniData, err := ini.Load("/etc/repoman/config.ini") if err != nil { - return nil, err + return err } - err = iniData.MapTo(&TLSCerts) + err = iniData.MapTo(&c) if err != nil { - return nil, err + return err } - return &TLSCerts, nil + return nil } +