feat(db): add basic auth

Signed-off-by: Klagarge <remi@heredero.ch>
This commit is contained in:
2026-05-03 17:49:31 +02:00
parent fcdb5b5485
commit bf7d0a7005
6 changed files with 79 additions and 2 deletions

View File

@@ -1,11 +1,16 @@
@room-id = B3 @room-id = B3
@username = user
@password = password
### GET last value of temp, co2, humidity, windows states ### GET last value of temp, co2, humidity, windows states
GET http://localhost:8080/api/v1/rooms/{{room-id}}/current GET http://localhost:8080/api/v1/rooms/{{room-id}}/current
Authorization: Basic {{username}} {{password}}
### GET history of a room ### GET history of a room
@window = 1 day @window = 1 day
GET http://localhost:8080/api/v1/rooms/{{room-id}}/history?window={{window}} GET http://localhost:8080/api/v1/rooms/{{room-id}}/history?window={{window}}
Authorization: Basic {{username}} {{password}}
### GET all rooms ### GET all rooms
GET http://localhost:8080/api/v1/rooms GET http://localhost:8080/api/v1/rooms
Authorization: Basic {{username}} {{password}}

View File

@@ -17,6 +17,11 @@ const docTemplate = `{
"paths": { "paths": {
"/rooms": { "/rooms": {
"get": { "get": {
"security": [
{
"BasicAuth": []
}
],
"description": "Get a list of all unique rooms from the measurement", "description": "Get a list of all unique rooms from the measurement",
"produces": [ "produces": [
"application/json" "application/json"
@@ -49,6 +54,11 @@ const docTemplate = `{
}, },
"/rooms/{room-id}/current": { "/rooms/{room-id}/current": {
"get": { "get": {
"security": [
{
"BasicAuth": []
}
],
"description": "Get the latest record for a specific room", "description": "Get the latest record for a specific room",
"produces": [ "produces": [
"application/json" "application/json"
@@ -97,6 +107,11 @@ const docTemplate = `{
}, },
"/rooms/{room-id}/history": { "/rooms/{room-id}/history": {
"get": { "get": {
"security": [
{
"BasicAuth": []
}
],
"description": "Get history for a specific room", "description": "Get history for a specific room",
"produces": [ "produces": [
"application/json" "application/json"
@@ -144,6 +159,11 @@ const docTemplate = `{
} }
} }
} }
},
"securityDefinitions": {
"BasicAuth": {
"type": "basic"
}
} }
}` }`

View File

@@ -11,6 +11,11 @@
"paths": { "paths": {
"/rooms": { "/rooms": {
"get": { "get": {
"security": [
{
"BasicAuth": []
}
],
"description": "Get a list of all unique rooms from the measurement", "description": "Get a list of all unique rooms from the measurement",
"produces": [ "produces": [
"application/json" "application/json"
@@ -43,6 +48,11 @@
}, },
"/rooms/{room-id}/current": { "/rooms/{room-id}/current": {
"get": { "get": {
"security": [
{
"BasicAuth": []
}
],
"description": "Get the latest record for a specific room", "description": "Get the latest record for a specific room",
"produces": [ "produces": [
"application/json" "application/json"
@@ -91,6 +101,11 @@
}, },
"/rooms/{room-id}/history": { "/rooms/{room-id}/history": {
"get": { "get": {
"security": [
{
"BasicAuth": []
}
],
"description": "Get history for a specific room", "description": "Get history for a specific room",
"produces": [ "produces": [
"application/json" "application/json"
@@ -138,5 +153,10 @@
} }
} }
} }
},
"securityDefinitions": {
"BasicAuth": {
"type": "basic"
}
} }
} }

View File

@@ -24,6 +24,8 @@ paths:
additionalProperties: additionalProperties:
type: string type: string
type: object type: object
security:
- BasicAuth: []
summary: Get all unique rooms summary: Get all unique rooms
tags: tags:
- rooms - rooms
@@ -56,6 +58,8 @@ paths:
additionalProperties: additionalProperties:
type: string type: string
type: object type: object
security:
- BasicAuth: []
summary: Get current data for a room summary: Get current data for a room
tags: tags:
- rooms - rooms
@@ -89,7 +93,12 @@ paths:
additionalProperties: additionalProperties:
type: string type: string
type: object type: object
security:
- BasicAuth: []
summary: Get history for a room summary: Get history for a room
tags: tags:
- rooms - rooms
securityDefinitions:
BasicAuth:
type: basic
swagger: "2.0" swagger: "2.0"

View File

@@ -12,6 +12,13 @@ import (
"time" "time"
) )
func getEnv(key, fallback string) string {
if value := os.Getenv(key); value != "" {
return value
}
return fallback
}
type ProvenceData struct { type ProvenceData struct {
CO2PPM int `json:"co2_ppm"` CO2PPM int `json:"co2_ppm"`
Temp float64 `json:"temp"` Temp float64 `json:"temp"`
@@ -98,6 +105,7 @@ func influxConnection() *influx.InfluxGateway {
// @description This is a gateway API for IoT data. // @description This is a gateway API for IoT data.
// @host doc.db.e.kb28.ch // @host doc.db.e.kb28.ch
// @BasePath /api/v1 // @BasePath /api/v1
// @securityDefinitions.basic BasicAuth
func main() { func main() {
// Load mapping configuration // Load mapping configuration
mappingPath := os.Getenv("MAPPING_CONFIG_PATH") mappingPath := os.Getenv("MAPPING_CONFIG_PATH")
@@ -165,7 +173,9 @@ func main() {
} }
// Initialize and start REST Gateway // Initialize and start REST Gateway
restGateway := rest.NewRestGateway(influxGateway, measurementName) restUsername := getEnv("REST_USERNAME", "user")
restPassword := getEnv("REST_PASSWORD", "password")
restGateway := rest.NewRestGateway(influxGateway, measurementName, restUsername, restPassword)
port, ok := os.LookupEnv("REST_PORT") port, ok := os.LookupEnv("REST_PORT")
if !ok { if !ok {

View File

@@ -17,13 +17,17 @@ type RestGateway struct {
influxGateway *influx.InfluxGateway influxGateway *influx.InfluxGateway
engine *gin.Engine engine *gin.Engine
measurementName string measurementName string
username string
password string
} }
func NewRestGateway(influxGateway *influx.InfluxGateway, measurementName string) *RestGateway { func NewRestGateway(influxGateway *influx.InfluxGateway, measurementName string, username, password string) *RestGateway {
g := &RestGateway{ g := &RestGateway{
influxGateway: influxGateway, influxGateway: influxGateway,
engine: gin.Default(), engine: gin.Default(),
measurementName: measurementName, measurementName: measurementName,
username: username,
password: password,
} }
g.setupRoutes() g.setupRoutes()
@@ -32,6 +36,12 @@ func NewRestGateway(influxGateway *influx.InfluxGateway, measurementName string)
func (g *RestGateway) setupRoutes() { func (g *RestGateway) setupRoutes() {
v1 := g.engine.Group("/api/v1") v1 := g.engine.Group("/api/v1")
if g.username != "" && g.password != "" {
v1.Use(gin.BasicAuth(gin.Accounts{
g.username: g.password,
}))
}
{ {
v1.GET("/rooms", g.getRooms) v1.GET("/rooms", g.getRooms)
v1.GET("/rooms/:room-id/current", g.getRoomCurrent) v1.GET("/rooms/:room-id/current", g.getRoomCurrent)
@@ -53,6 +63,7 @@ func (g *RestGateway) Run(addr string) error {
// @Produce json // @Produce json
// @Success 200 {array} string // @Success 200 {array} string
// @Failure 500 {object} map[string]string // @Failure 500 {object} map[string]string
// @Security BasicAuth
// @Router /rooms [get] // @Router /rooms [get]
func (g *RestGateway) getRooms(c *gin.Context) { func (g *RestGateway) getRooms(c *gin.Context) {
// Query unique rooms from the measurement // Query unique rooms from the measurement
@@ -91,6 +102,7 @@ func (g *RestGateway) getRooms(c *gin.Context) {
// @Success 200 {object} map[string]any // @Success 200 {object} map[string]any
// @Failure 404 {object} map[string]string // @Failure 404 {object} map[string]string
// @Failure 500 {object} map[string]string // @Failure 500 {object} map[string]string
// @Security BasicAuth
// @Router /rooms/{room-id}/current [get] // @Router /rooms/{room-id}/current [get]
func (g *RestGateway) getRoomCurrent(c *gin.Context) { func (g *RestGateway) getRoomCurrent(c *gin.Context) {
roomID := c.Param("room-id") roomID := c.Param("room-id")
@@ -128,6 +140,7 @@ func (g *RestGateway) getRoomCurrent(c *gin.Context) {
// @Param window query string false "Time window (e.g., 1 day, 1 hour, 30 min)" default(1 day) // @Param window query string false "Time window (e.g., 1 day, 1 hour, 30 min)" default(1 day)
// @Success 200 {array} map[string]any // @Success 200 {array} map[string]any
// @Failure 500 {object} map[string]string // @Failure 500 {object} map[string]string
// @Security BasicAuth
// @Router /rooms/{room-id}/history [get] // @Router /rooms/{room-id}/history [get]
func (g *RestGateway) getRoomHistory(c *gin.Context) { func (g *RestGateway) getRoomHistory(c *gin.Context) {
roomID := c.Param("room-id") roomID := c.Param("room-id")