htmgo/examples/chat/main.go

55 lines
1.1 KiB
Go
Raw Normal View History

package main
import (
"chat/__htmgo"
"chat/chat"
2024-09-30 22:31:09 +00:00
"chat/internal/db"
"chat/ws"
"fmt"
"github.com/maddalax/htmgo/framework/h"
"github.com/maddalax/htmgo/framework/service"
"io/fs"
"net/http"
"runtime"
"time"
)
func main() {
locator := service.NewLocator()
2024-09-30 22:31:09 +00:00
service.Set[db.Queries](locator, service.Singleton, db.Provide)
service.Set[ws.SocketManager](locator, service.Singleton, func() *ws.SocketManager {
return ws.NewSocketManager()
})
2024-09-30 22:31:09 +00:00
chatManager := chat.NewManager(locator)
go chatManager.StartListener()
go func() {
for {
count := runtime.NumGoroutine()
fmt.Printf("goroutines: %d\n", count)
time.Sleep(10 * time.Second)
}
}()
h.Start(h.AppOpts{
ServiceLocator: locator,
LiveReload: true,
Register: func(app *h.App) {
2024-10-02 03:26:03 +00:00
sub, err := fs.Sub(GetStaticAssets(), "assets/dist")
if err != nil {
panic(err)
}
http.FileServerFS(sub)
app.Router.Handle("/public/*", http.StripPrefix("/public", http.FileServerFS(sub)))
2024-10-01 03:08:52 +00:00
app.Router.Handle("/ws/chat/{id}", ws.Handle())
__htmgo.Register(app.Router)
},
})
}