diff --git a/lib/config/config.go b/lib/config/config.go index f8a3fe1..5968c4c 100644 --- a/lib/config/config.go +++ b/lib/config/config.go @@ -13,6 +13,8 @@ type DHConfig struct { MaxHistoryAge int `json:"maxHistoryAge"` DatabaseFilePath string Listen string + IdentityUsername string + IdentityPassword string } func GetConfiguration() DHConfig { @@ -25,6 +27,8 @@ func GetConfiguration() DHConfig { DiskFetchFrequency: 5, // default value MaxHistoryAge: 2592000, // default value DatabaseFilePath: "./data.sqlite", + IdentityUsername: "admin", + IdentityPassword: "admin", Listen: ":8080", } @@ -49,5 +53,13 @@ func GetConfiguration() DHConfig { config.DatabaseFilePath = val } + if val, exists := os.LookupEnv("IDENTITY_USERNAME"); exists { + config.IdentityUsername = val + } + + if val, exists := os.LookupEnv("IDENTITY_PASSWORD"); exists { + config.IdentityPassword = val + } + return config } diff --git a/lib/web/auth_middleware.go b/lib/web/auth_middleware.go new file mode 100644 index 0000000..ce27d00 --- /dev/null +++ b/lib/web/auth_middleware.go @@ -0,0 +1,11 @@ +package web + +import "github.com/gin-gonic/gin" + +func BasicAuthMiddleware(username, password string) gin.HandlerFunc { + authorized := gin.Accounts{ + username: password, + } + + return gin.BasicAuth(authorized) +} diff --git a/lib/web/net.go b/lib/web/net.go index b280b40..5c28781 100644 --- a/lib/web/net.go +++ b/lib/web/net.go @@ -2,12 +2,16 @@ package web import ( "github.com/gin-gonic/gin" + "tea.chunkbyte.com/kato/drive-health/lib/config" ) func SetupRouter() *gin.Engine { // Initialize the Gin engine + cfg := config.GetConfiguration() r := gin.Default() + r.Use(BasicAuthMiddleware(cfg.IdentityUsername, cfg.IdentityPassword)) + // Setup Health Pings setupHealth(r) // Setup Api diff --git a/main.go b/main.go index f942c4b..43e5cc5 100644 --- a/main.go +++ b/main.go @@ -17,11 +17,12 @@ import ( func main() { // Init the database svc.InitDB() + cfg := config.GetConfiguration() router := web.SetupRouter() srv := &http.Server{ - Addr: config.GetConfiguration().Listen, + Addr: cfg.Listen, Handler: router, }