Allow auth-less
This commit is contained in:
parent
013886a2ff
commit
aca2e8778c
|
|
@ -13,6 +13,8 @@ COPY server/config/*.go ./config/
|
|||
COPY server/routes/*.go ./routes/
|
||||
COPY server/services/*.go ./services/
|
||||
COPY server/middleware/*.go ./middleware/
|
||||
COPY server/integrations/*.go ./integrations/
|
||||
COPY server/models/*.go ./models/
|
||||
COPY server/database ./database/
|
||||
|
||||
RUN go build -o /basic-sensor-receiver
|
||||
|
|
@ -53,6 +55,7 @@ ENV BIND_IP 0.0.0.0
|
|||
ENV AUTH_USERNAME admin
|
||||
ENV AUTH_PASSWORD password
|
||||
ENV AUTH_KEY password
|
||||
ENV AUTH_ENABLED true
|
||||
|
||||
EXPOSE ${PORT}
|
||||
VOLUME [ "/data" ]
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ GIN_MODE=debug
|
|||
DATABASE_URL=./sensors.sqlite3?_busy_timeout=500
|
||||
PORT=8083
|
||||
BIND_IP=localhost
|
||||
AUTH_ENABLED=true
|
||||
AUTH_USERNAME=admin
|
||||
AUTH_PASSWORD=password
|
||||
AUTH_KEY=password
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ type Config struct {
|
|||
DatabaseUrl string
|
||||
Port int
|
||||
Ip string
|
||||
AuthEnabled bool
|
||||
AuthUsername string
|
||||
AuthPassword string
|
||||
AuthKey string
|
||||
|
|
@ -27,6 +28,7 @@ func LoadConfig() *Config {
|
|||
DatabaseUrl: os.Getenv("DATABASE_URL"),
|
||||
Port: port,
|
||||
Ip: os.Getenv("BIND_IP"),
|
||||
AuthEnabled: os.Getenv("AUTH_ENABLED") != "false",
|
||||
AuthUsername: os.Getenv("AUTH_USERNAME"),
|
||||
AuthPassword: os.Getenv("AUTH_PASSWORD"),
|
||||
AuthKey: os.Getenv("AUTH_KEY"),
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ import (
|
|||
func main() {
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
log.Println("Error loading .env file")
|
||||
log.Println("Error loading .env file: ", err)
|
||||
}
|
||||
|
||||
server := app.InitializeServer()
|
||||
|
|
@ -33,8 +33,10 @@ func main() {
|
|||
router.Use(middleware.CorsMiddleware())
|
||||
}
|
||||
|
||||
// User login route
|
||||
router.POST("/api/login", routes.Login(server))
|
||||
if server.Config.AuthEnabled {
|
||||
// User login route
|
||||
router.POST("/api/login", routes.Login(server))
|
||||
}
|
||||
|
||||
// Routes that are only accessible after logging in
|
||||
loginProtected := router.Group("/", middleware.LoginAuthMiddleware(server))
|
||||
|
|
@ -63,7 +65,10 @@ func main() {
|
|||
loginProtected.PUT("/api/contact-points/:contactPointId", routes.PutContactPoint(server))
|
||||
loginProtected.DELETE("/api/contact-points/:contactPointId", routes.DeleteContactPoint(server))
|
||||
loginProtected.POST("/api/contact-points/test", routes.TestContactPoint(server))
|
||||
loginProtected.POST("/api/logout", routes.Logout(server))
|
||||
|
||||
if server.Config.AuthEnabled {
|
||||
loginProtected.POST("/api/logout", routes.Logout(server))
|
||||
}
|
||||
|
||||
// Routes accessible using auth key
|
||||
keyProtected := router.Group("/", middleware.KeyAuthMiddleware(server))
|
||||
|
|
@ -75,6 +80,10 @@ func main() {
|
|||
// Starts alerts handling goroutine
|
||||
server.StartAlerts()
|
||||
|
||||
address := fmt.Sprintf("%s:%d", server.Config.Ip, server.Config.Port)
|
||||
|
||||
log.Println("Starting server on", address)
|
||||
|
||||
// Run the server
|
||||
router.Run(fmt.Sprintf("%s:%d", server.Config.Ip, server.Config.Port))
|
||||
router.Run(address)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,6 +10,12 @@ import (
|
|||
|
||||
func LoginAuthMiddleware(server *app.Server) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
if !server.Config.AuthEnabled {
|
||||
c.Next()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
_, err := server.Services.Auth.FromContext(c)
|
||||
|
||||
if err != nil {
|
||||
|
|
|
|||
Loading…
Reference in New Issue