feat: 基本的websocket echo服务

This commit is contained in:
2025-12-17 12:03:06 +08:00
parent b824dc3792
commit 1bc9c6a924
11 changed files with 242 additions and 29 deletions

View File

@ -4,5 +4,7 @@ import "net/http"
func Health(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
if _, err := w.Write([]byte("OK")); err != nil {
http.Error(w, "failed to write response", http.StatusInternalServerError)
}
}

View File

@ -6,12 +6,12 @@ import (
"time"
"git.jinshen.cn/remilia/push-server/interval/api/dto"
"git.jinshen.cn/remilia/push-server/interval/hub"
"git.jinshen.cn/remilia/push-server/interval/model"
"git.jinshen.cn/remilia/push-server/interval/ws"
"github.com/go-chi/chi/v5"
)
func PushHandler(hub *hub.Hub) http.HandlerFunc {
func PushHandler(hub *ws.Hub) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
topicStr := chi.URLParam(r, "topic")
topic := model.Topic(topicStr)

View File

@ -1,16 +1,19 @@
package api
import (
"context"
"net/http"
"git.jinshen.cn/remilia/push-server/interval/api/handler"
"git.jinshen.cn/remilia/push-server/interval/hub"
"git.jinshen.cn/remilia/push-server/interval/ws"
"github.com/go-chi/chi/v5"
)
func NewRouter(h *hub.Hub) http.Handler {
func NewRouter(h *ws.Hub, ctx context.Context) http.Handler {
r := chi.NewRouter()
r.Get("/ws", ws.Handler(ctx, h))
r.Post("/health", handler.Health)
r.Post("/push/{topic}", handler.PushHandler(h))