feat: add website

This commit is contained in:
tux
2025-03-03 19:44:08 +05:30
parent eb4f430ad7
commit 17fcdc73f7
21 changed files with 790 additions and 2 deletions

View File

@ -22,6 +22,7 @@ type Conn struct {
}
type Trok struct {
webServer *TrokWeb
controlServer TCPServer
publicConns map[string]Conn
mutex sync.Mutex
@ -29,17 +30,20 @@ type Trok struct {
func (t *Trok) Init(addr string) error {
t.publicConns = make(map[string]Conn)
t.webServer = NewTrokWeb(":3000")
err := t.controlServer.Init(addr, "Controller")
return err
}
func (t *Trok) Start() {
go t.controlServer.Start(t.ControlConnHandler)
go t.webServer.Start()
log.Info().Msgf("started Trok server on %s", t.controlServer.Addr())
}
func (t *Trok) Stop() {
t.controlServer.Stop()
t.webServer.Stop()
log.Info().Msgf("stopped Trok server on %s", t.controlServer.Addr())
}

41
internal/server/web.go Normal file
View File

@ -0,0 +1,41 @@
package server
import (
"net/http"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/filesystem"
"github.com/tuxdotrs/trok/internal/web"
)
type TrokWeb struct {
app *fiber.App
addr string
}
func NewTrokWeb(addr string) *TrokWeb {
return &TrokWeb{
app: fiber.New(),
addr: addr,
}
}
func (t *TrokWeb) Start() {
t.app.Use("/", filesystem.New(filesystem.Config{
Root: http.FS(web.EmbedDirStatic),
PathPrefix: "dist",
Browse: true,
}))
t.app.Use("/assets", filesystem.New(filesystem.Config{
Root: http.FS(web.EmbedDirStatic),
PathPrefix: "dist/assets",
Browse: true,
}))
t.app.Listen(t.addr)
}
func (t *TrokWeb) Stop() {
t.app.Shutdown()
}