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
@username = user
@password = password
### GET last value of temp, co2, humidity, windows states
GET http://localhost:8080/api/v1/rooms/{{room-id}}/current
Authorization: Basic {{username}} {{password}}
### GET history of a room
@window = 1 day
GET http://localhost:8080/api/v1/rooms/{{room-id}}/history?window={{window}}
Authorization: Basic {{username}} {{password}}
### GET all rooms
GET http://localhost:8080/api/v1/rooms
Authorization: Basic {{username}} {{password}}

View File

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

View File

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

View File

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

View File

@@ -12,6 +12,13 @@ import (
"time"
)
func getEnv(key, fallback string) string {
if value := os.Getenv(key); value != "" {
return value
}
return fallback
}
type ProvenceData struct {
CO2PPM int `json:"co2_ppm"`
Temp float64 `json:"temp"`
@@ -98,6 +105,7 @@ func influxConnection() *influx.InfluxGateway {
// @description This is a gateway API for IoT data.
// @host doc.db.e.kb28.ch
// @BasePath /api/v1
// @securityDefinitions.basic BasicAuth
func main() {
// Load mapping configuration
mappingPath := os.Getenv("MAPPING_CONFIG_PATH")
@@ -165,7 +173,9 @@ func main() {
}
// 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")
if !ok {

View File

@@ -17,13 +17,17 @@ type RestGateway struct {
influxGateway *influx.InfluxGateway
engine *gin.Engine
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{
influxGateway: influxGateway,
engine: gin.Default(),
measurementName: measurementName,
username: username,
password: password,
}
g.setupRoutes()
@@ -32,6 +36,12 @@ func NewRestGateway(influxGateway *influx.InfluxGateway, measurementName string)
func (g *RestGateway) setupRoutes() {
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/:room-id/current", g.getRoomCurrent)
@@ -53,6 +63,7 @@ func (g *RestGateway) Run(addr string) error {
// @Produce json
// @Success 200 {array} string
// @Failure 500 {object} map[string]string
// @Security BasicAuth
// @Router /rooms [get]
func (g *RestGateway) getRooms(c *gin.Context) {
// Query unique rooms from the measurement
@@ -91,6 +102,7 @@ func (g *RestGateway) getRooms(c *gin.Context) {
// @Success 200 {object} map[string]any
// @Failure 404 {object} map[string]string
// @Failure 500 {object} map[string]string
// @Security BasicAuth
// @Router /rooms/{room-id}/current [get]
func (g *RestGateway) getRoomCurrent(c *gin.Context) {
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)
// @Success 200 {array} map[string]any
// @Failure 500 {object} map[string]string
// @Security BasicAuth
// @Router /rooms/{room-id}/history [get]
func (g *RestGateway) getRoomHistory(c *gin.Context) {
roomID := c.Param("room-id")