Azpect3120 276a6be7b9 FEAT: Table tree is now displayed!
Has no functionality yet, but that's next. And not sure how it works
when switching connections.
2024-08-07 18:08:40 -07:00

76 lines
2.0 KiB
Go

package http
import (
"encoding/json"
"fmt"
"time"
"github.com/Azpect3120/Web-Database-Viewer/internal/database"
"github.com/Azpect3120/Web-Database-Viewer/internal/templates"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
)
// Populate the server with routes
func populate(web, api *gin.RouterGroup) {
web.GET("/view", func(c *gin.Context) {
c.HTML(200, "index.html", gin.H{})
})
api.GET("/status", func(c *gin.Context) {
c.JSON(200, gin.H{
"status": "ok",
"time": time.Now().String(),
})
})
api.POST("/query", database.QueryCurrent)
api.POST("/connections/test", database.TestConnectionURL)
api.POST("/connections", database.CreateConnection)
api.GET("/connections", func(c *gin.Context) {
session := sessions.Default(c)
connections_bytes, ok := session.Get("connections").([]byte)
if !ok {
c.JSON(200, gin.H{"connections": make(map[string]string), "current": "", "count": 0, "time": time.Now().String(), "status": 200})
return
}
current := session.Get("current")
var connections map[string]string
if err := json.Unmarshal(connections_bytes, &connections); err != nil {
fmt.Println(err)
}
c.JSON(200, gin.H{
"connections": connections,
"current": current,
"count": len(connections),
"time": time.Now().String(),
"status": 200,
})
})
web.GET("/connections", func(c *gin.Context) {
session := sessions.Default(c)
connections_bytes, conn_ok := session.Get("connections").([]byte)
current, curr_ok := session.Get("current").(string)
var connections map[string]string
if conn_ok || curr_ok {
if err := json.Unmarshal(connections_bytes, &connections); err != nil {
fmt.Println(err)
}
} else {
connections = make(map[string]string)
}
html := templates.ConnectionsList(connections, current)
c.String(200, html)
})
api.POST("/connections/connect", database.ChangeConnection)
web.GET("/connections/tree", func(c *gin.Context) {
c.String(200, database.TableTree(c))
})
}