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.
106 lines
3.0 KiB
Go
106 lines
3.0 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
docs "lsm-api/docs"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
swaggerfiles "github.com/swaggo/files"
|
|
ginSwagger "github.com/swaggo/gin-swagger"
|
|
"gopkg.in/ini.v1"
|
|
|
|
"lsm-api/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.CPU{})
|
|
if err != nil {
|
|
log.Fatal("Failed to migrate CPU schema")
|
|
}
|
|
err = DB.AutoMigrate(&models.Memory{})
|
|
if err != nil {
|
|
log.Fatal("Failed to migrate Memory schema")
|
|
}
|
|
err = DB.AutoMigrate(&models.Host{})
|
|
if err != nil {
|
|
log.Fatal("Failed to migrate Host schema")
|
|
}
|
|
|
|
r := gin.Default()
|
|
docs.SwaggerInfo.BasePath = "/api/v1"
|
|
v1 := r.Group("/api/v1")
|
|
{
|
|
hosts := v1.Group("/hosts")
|
|
{
|
|
hosts.GET("/hosts", GetHosts)
|
|
hosts.GET("/get_host_by_hostname/:hostname", GetHostByHostname)
|
|
hosts.GET("/get_host_id_for_hostname/:hostname", GetHostIDForHostname)
|
|
hosts.POST("/add_host", AddHost)
|
|
hosts.DELETE("/delete_host/:host_id", DeleteHost)
|
|
hosts.DELETE("/delete_host_by_hostname/:hostname", DeleteHostByHostname)
|
|
}
|
|
cpus := v1.Group("/cpus")
|
|
{
|
|
cpus.GET("/cpus", GetCPUs)
|
|
cpus.POST("/add_cpu_to_host", AddCPUToHost)
|
|
}
|
|
}
|
|
|
|
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))
|
|
err = r.Run(":8080")
|
|
if err != nil {
|
|
log.Fatal("Failed to start API")
|
|
}
|
|
}
|