You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
107 lines
3.1 KiB
Go
107 lines
3.1 KiB
Go
package main
|
|
|
|
import (
|
|
docs "digital_temperature_monitor/docs"
|
|
"fmt"
|
|
"log"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
swaggerfiles "github.com/swaggo/files"
|
|
ginSwagger "github.com/swaggo/gin-swagger"
|
|
"gopkg.in/ini.v1"
|
|
|
|
"digital_temperature_monitor/models"
|
|
|
|
"gorm.io/driver/postgres"
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
var DB *gorm.DB
|
|
|
|
// @BasePath /api/v1
|
|
|
|
func main() {
|
|
cfg, err := ini.Load("config.ini")
|
|
if err != nil {
|
|
log.Fatal("Fail to read file: ", err)
|
|
}
|
|
|
|
// Classic read of values, default section can be represented as empty string
|
|
fmt.Println("App Mode: ", cfg.Section("").Key("app_mode").String())
|
|
fmt.Println("Database Type: ", cfg.Section("database").Key("type").String())
|
|
|
|
if strings.ToLower(cfg.Section("database").Key("type").String()) == "postgres" {
|
|
fmt.Println("Database Host: ", cfg.Section("database").Key("host").String())
|
|
fmt.Println("Database Port: ", cfg.Section("database").Key("port").String())
|
|
fmt.Println("Database Username:", cfg.Section("database").Key("username").String())
|
|
fmt.Println("Database Name: ", cfg.Section("database").Key("db_name").String())
|
|
|
|
databaseConnectString := fmt.Sprintf(
|
|
"host=%s port=%s user=%s dbname=%s password=%s sslmode=disable",
|
|
cfg.Section("database").Key("host"),
|
|
cfg.Section("database").Key("port"),
|
|
cfg.Section("database").Key("user"),
|
|
cfg.Section("database").Key("db_name"),
|
|
cfg.Section("database").Key("password"),
|
|
)
|
|
|
|
log.Println(databaseConnectString)
|
|
DB, err = gorm.Open(postgres.Open(databaseConnectString), &gorm.Config{TranslateError: true})
|
|
if err != nil {
|
|
log.Fatal("Failed to connect to PostgreSQL DB")
|
|
}
|
|
} else if strings.ToLower(cfg.Section("database").Key("type").String()) == "sqlite" {
|
|
sqlite_path := cfg.Section("database").Key("path").String()
|
|
fmt.Println("sqlite DB Path: ", sqlite_path)
|
|
|
|
DB, err = gorm.Open(sqlite.Open(sqlite_path), &gorm.Config{TranslateError: true})
|
|
if err != nil {
|
|
log.Fatal("Failed to open sqlite DB")
|
|
}
|
|
}
|
|
|
|
log.Println("Successfully connected to database")
|
|
|
|
err = DB.AutoMigrate(&models.Temperature{})
|
|
if err != nil {
|
|
log.Fatal("failed to migrate temperature schema")
|
|
}
|
|
err = DB.AutoMigrate(&models.Room{})
|
|
if err != nil {
|
|
log.Fatal("Failed to migrate room schema")
|
|
}
|
|
|
|
r := gin.Default()
|
|
docs.SwaggerInfo.BasePath = "/api/v1"
|
|
v1 := r.Group("/api/v1")
|
|
{
|
|
rooms := v1.Group("/rooms")
|
|
{
|
|
rooms.GET("/rooms", GetRooms)
|
|
rooms.GET("/get_room_by_name/:name", GetRoomByName)
|
|
rooms.GET("/get_room_id_by_name/:name", GetRoomIDByName)
|
|
rooms.POST("/add_room", AddRoom)
|
|
rooms.DELETE("/delete_room/:room_id", DeleteRoom)
|
|
rooms.DELETE("/delete_room_by_name/:name", DeleteRoomByName)
|
|
}
|
|
temperature := v1.Group("/temperature")
|
|
{
|
|
temperature.GET("/temperatures", GetTemperatures)
|
|
temperature.POST("/add_temperature", AddTemperature)
|
|
}
|
|
humidity := v1.Group("/humidity")
|
|
{
|
|
humidity.GET("/humidities", GetHumidities)
|
|
humidity.POST("/add_humidity", AddHumidity)
|
|
}
|
|
}
|
|
|
|
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))
|
|
err = r.Run(":8080")
|
|
if err != nil {
|
|
log.Fatal("Failed to start API")
|
|
}
|
|
}
|