🔨 Adds db integration

This commit is contained in:
Daniel Svitan 2025-03-29 22:52:12 +01:00
parent ebe87d1eb3
commit 0335c3157e
2 changed files with 88 additions and 38 deletions

View File

@ -67,6 +67,7 @@ func ConnectDbFromEnv() {
type Dummy struct { type Dummy struct {
ID uuid.UUID `gorm:"primaryKey;type:uuid;unique" json:"id"` ID uuid.UUID `gorm:"primaryKey;type:uuid;unique" json:"id"`
Name string `json:"name"`
Connected bool `json:"connected"` Connected bool `json:"connected"`
ConnectedAt time.Time `json:"connectedAt"` ConnectedAt time.Time `json:"connectedAt"`
DisconnectedAt time.Time `json:"disconnectedAt"` DisconnectedAt time.Time `json:"disconnectedAt"`

View File

@ -9,6 +9,7 @@ import (
"syscall" "syscall"
"time" "time"
"github.com/google/uuid"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"github.com/joho/godotenv" "github.com/joho/godotenv"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
@ -19,19 +20,32 @@ import (
const TimeFormat = "2006-01-02 15:04:05" const TimeFormat = "2006-01-02 15:04:05"
type Client struct { type Client struct {
ID int Obj *Dummy `json:"obj"`
conn *websocket.Conn conn *websocket.Conn `json:"-"`
Exit bool ExitWanted bool `json:"exitWanted"`
} }
var id = 0
var clients = []*Client{} var clients = []*Client{}
var dataDir = "." var dataDir = "."
var token = "" var token = ""
var upgrader = websocket.Upgrader{} var upgrader = websocket.Upgrader{}
type ReqData struct { type ReqData struct {
ID int `json:"id"` ID uuid.UUID `json:"id"`
}
type IDParam struct {
ID string `param:"id"`
}
func authed(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if c.Request().Header.Get("Authorization") != token {
return c.NoContent(http.StatusUnauthorized)
}
return next(c)
}
} }
func main() { func main() {
@ -104,31 +118,28 @@ func main() {
} }
} }
// client connection
app.GET("/", func(c echo.Context) error { app.GET("/", func(c echo.Context) error {
return c.JSON(http.StatusOK, "Hello World!") return c.JSON(http.StatusOK, "Hello World!")
}) })
app.GET("/keys", keys) app.GET("/keys", keys)
app.GET("/clients", func(c echo.Context) error { // administration
app.GET("/admin/clients", func(c echo.Context) error {
if c.Request().Header.Get("Authorization") != token { if c.Request().Header.Get("Authorization") != token {
return c.NoContent(http.StatusUnauthorized) return c.NoContent(http.StatusUnauthorized)
} }
var a = []echo.Map{} return c.JSON(http.StatusOK, clients)
for _, client := range clients {
a = append(a, echo.Map{
"id": client.ID,
"exit": client.Exit,
})
}
return c.JSON(http.StatusOK, a)
}) })
app.POST("/exit", func(c echo.Context) error { app.GET("/admin/:id/logs", authed(func(c echo.Context) error {
if c.Request().Header.Get("Authorization") != token { // TODO: finish
return c.NoContent(http.StatusUnauthorized) return nil
} }))
app.POST("/admin/:id/name", authed(func(c echo.Context) error {
return nil
}))
app.POST("/admin/exit", authed(func(c echo.Context) error {
var data ReqData var data ReqData
err := c.Bind(&data) err := c.Bind(&data)
if err != nil { if err != nil {
@ -136,13 +147,13 @@ func main() {
} }
for _, client := range clients { for _, client := range clients {
if client.ID == data.ID { if client.Obj.ID == data.ID {
client.Exit = true client.ExitWanted = true
return c.JSON(http.StatusOK, echo.Map{"message": "ok"}) return c.JSON(http.StatusOK, echo.Map{"message": "ok"})
} }
} }
return c.JSON(http.StatusNotFound, echo.Map{"message": "not found"}) return c.JSON(http.StatusNotFound, echo.Map{"message": "not found"})
}) }))
log.Fatal(app.Start(":8080")) log.Fatal(app.Start(":8080"))
} }
@ -154,25 +165,53 @@ func keys(c echo.Context) error {
} }
defer ws.Close() defer ws.Close()
now := time.Now()
client := Client{ client := Client{
ID: id, Obj: &Dummy{
conn: ws, ID: uuid.New(),
Exit: false, Name: "Dummy",
Connected: true,
ConnectedAt: now,
DisconnectedAt: now,
CreatedAt: now,
UpdatedAt: now,
},
conn: ws,
ExitWanted: false,
}
tx := db.Create(client.Obj)
if tx.Error != nil {
log.Error(err)
fmt.Printf("%s client %s crashed (couldn't create record)\n", time.Now().Format(TimeFormat), client.Obj.ID.String())
client.ExitWanted = true
client.Obj.Connected = false
client.Obj.DisconnectedAt = time.Now()
return c.NoContent(http.StatusInternalServerError)
} }
clients = append(clients, &client) clients = append(clients, &client)
id += 1 fmt.Printf("%s client %s connected\n", time.Now().Format(TimeFormat), client.Obj.ID.String())
fmt.Printf("%s client %-3d connected\n", time.Now().Format(TimeFormat), client.ID)
err = ws.WriteMessage(websocket.TextMessage, []byte("welcome")) err = ws.WriteMessage(websocket.TextMessage, []byte("welcome"))
if err != nil { if err != nil {
log.Error(err) log.Error(err)
} }
f, err := os.OpenFile(fmt.Sprintf("%s/%s_%d.txt", dataDir, time.Now().Format("2006-01-02_15-04"), client.ID), syscall.O_CREAT|syscall.O_APPEND|syscall.O_WRONLY, 0644) f, err := os.OpenFile(fmt.Sprintf("%s/%s_%d.txt", dataDir, time.Now().Format("2006-01-02_15-04"), client.Obj.ID.String()), syscall.O_CREAT|syscall.O_APPEND|syscall.O_WRONLY, 0644)
if err != nil { if err != nil {
log.Error(err) log.Error(err)
fmt.Printf("%s client %-3d crashed (couldn't open file)\n", time.Now().Format(TimeFormat), client.ID) fmt.Printf("%s client %s crashed (couldn't open file)\n", time.Now().Format(TimeFormat), client.Obj.ID)
client.Exit = true now = time.Now()
client.ExitWanted = true
client.Obj.Connected = false
client.Obj.DisconnectedAt = now
client.Obj.UpdatedAt = now
tx = db.Save(client.Obj)
if tx.Error != nil {
log.Error(err)
}
return c.NoContent(http.StatusInternalServerError) return c.NoContent(http.StatusInternalServerError)
} }
defer f.Close() defer f.Close()
@ -180,13 +219,23 @@ func keys(c echo.Context) error {
close := func() { close := func() {
_ = ws.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")) _ = ws.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
_ = ws.Close() _ = ws.Close()
client.Exit = true now = time.Now()
client.ExitWanted = true
client.Obj.Connected = false
client.Obj.DisconnectedAt = now
client.Obj.UpdatedAt = now
tx = db.Save(client.Obj)
if tx.Error != nil {
log.Error(err)
}
} }
for { for {
if client.Exit { if client.ExitWanted {
close() close()
fmt.Printf("%s client %-3d disconnected\n", time.Now().Format(TimeFormat), client.ID) fmt.Printf("%s client %s disconnected\n", time.Now().Format(TimeFormat), client.Obj.ID.String())
break break
} }
@ -194,16 +243,16 @@ func keys(c echo.Context) error {
if err != nil { if err != nil {
log.Error(err) log.Error(err)
close() close()
fmt.Printf("%s client %-3d crashed (websocket error)\n", time.Now().Format(TimeFormat), client.ID) fmt.Printf("%s client %s crashed (websocket error)\n", time.Now().Format(TimeFormat), client.Obj.ID.String())
break return c.NoContent(http.StatusInternalServerError)
} }
_, err = f.Write(msg) _, err = f.Write(msg)
if err != nil { if err != nil {
log.Error(err) log.Error(err)
close() close()
fmt.Printf("%s client %-3d crashed (file write error)\n", time.Now().Format(TimeFormat), client.ID) fmt.Printf("%s client %s crashed (file write error)\n", time.Now().Format(TimeFormat), client.Obj.ID.String())
break return c.NoContent(http.StatusInternalServerError)
} }
} }