Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose config database endpoint #981

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions handlers/accounts_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@ package handlers

import (
"context"
"database/sql"
"encoding/json"
"fmt"
"log"
"net/http"

"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"github.com/tailwarden/komiser/models"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/pgdialect"
"github.com/uptrace/bun/dialect/sqlitedialect"
"github.com/uptrace/bun/driver/pgdriver"
"github.com/uptrace/bun/driver/sqliteshim"
)

func (handler *ApiHandler) IsOnboardedHandler(c *gin.Context) {
Expand All @@ -17,6 +25,10 @@ func (handler *ApiHandler) IsOnboardedHandler(c *gin.Context) {
Onboarded: false,
}

if handler.db != nil {
c.JSON(http.StatusOK, output)
}

accounts := make([]models.Account, 0)
err := handler.db.NewRaw("SELECT * FROM accounts").Scan(handler.ctx, &accounts)
if err != nil {
Expand Down Expand Up @@ -103,3 +115,33 @@ func (handler *ApiHandler) UpdateCloudAccountHandler(c *gin.Context) {

c.JSON(http.StatusOK, account)
}

func (handler *ApiHandler) ConfigureDatabaseHandler(c *gin.Context) {
var db models.DatabaseConfig
err := json.NewDecoder(c.Request.Body).Decode(&db)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}

if db.Type == "SQLITE" {
sqldb, err := sql.Open(sqliteshim.ShimName, fmt.Sprintf("file:%s?cache=shared", db.File))
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
sqldb.SetMaxIdleConns(1000)
sqldb.SetConnMaxLifetime(0)

handler.db = bun.NewDB(sqldb, sqlitedialect.New())
log.Println("Data will be stored in SQLite")
} else {
uri := fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=disable", db.Username, db.Password, db.Hostname, db.Database)
sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(uri)))
handler.db = bun.NewDB(sqldb, pgdialect.New())

log.Println("Data will be stored in PostgreSQL")
}

c.JSON(http.StatusOK, map[string]string{"message": "database has been configured"})
}
2 changes: 2 additions & 0 deletions internal/api/v1/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ func Endpoints(ctx context.Context, telemetry bool, analytics utils.Analytics, d
router.DELETE("/cloud_accounts/:id", api.DeleteCloudAccountHandler)
router.PUT("/cloud_accounts/:id", api.UpdateCloudAccountHandler)

router.POST("/databases", api.ConfigureDatabaseHandler)

router.NoRoute(gin.WrapH(http.FileServer(assetFS())))

return router
Expand Down
10 changes: 10 additions & 0 deletions models/database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package models

type DatabaseConfig struct {
Type string `json:"type"`
Hostname string `json:"hostname"`
Database string `json:"database"`
Username string `json:"username"`
Password string `json:"password"`
File string `json:"file"`
}