This commit was merged in pull request #8.
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"git.eeqj.de/sneak/chat/web"
|
||||
|
||||
sentryhttp "github.com/getsentry/sentry-go/http"
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/go-chi/chi/middleware"
|
||||
@@ -45,4 +48,40 @@ func (s *Server) SetupRoutes() {
|
||||
r.Get("/metrics", http.HandlerFunc(promhttp.Handler().ServeHTTP))
|
||||
})
|
||||
}
|
||||
|
||||
// API v1
|
||||
s.router.Route("/api/v1", func(r chi.Router) {
|
||||
r.Get("/server", s.h.HandleServerInfo())
|
||||
r.Post("/session", s.h.HandleCreateSession())
|
||||
|
||||
// Unified state and message endpoints
|
||||
r.Get("/state", s.h.HandleState())
|
||||
r.Get("/messages", s.h.HandleGetMessages())
|
||||
r.Post("/messages", s.h.HandleSendCommand())
|
||||
r.Get("/history", s.h.HandleGetHistory())
|
||||
|
||||
// Channels
|
||||
r.Get("/channels", s.h.HandleListAllChannels())
|
||||
r.Get("/channels/{channel}/members", s.h.HandleChannelMembers())
|
||||
})
|
||||
|
||||
// Serve embedded SPA
|
||||
distFS, err := fs.Sub(web.Dist, "dist")
|
||||
if err != nil {
|
||||
s.log.Error("failed to get web dist filesystem", "error", err)
|
||||
} else {
|
||||
fileServer := http.FileServer(http.FS(distFS))
|
||||
s.router.Get("/*", func(w http.ResponseWriter, r *http.Request) {
|
||||
// Try to serve the file; if not found, serve index.html for SPA routing
|
||||
f, err := distFS.(fs.ReadFileFS).ReadFile(r.URL.Path[1:])
|
||||
if err != nil || len(f) == 0 {
|
||||
indexHTML, _ := distFS.(fs.ReadFileFS).ReadFile("index.html")
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write(indexHTML)
|
||||
return
|
||||
}
|
||||
fileServer.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user