refactor: 重构项目结构

- 将server端相关依赖单独防止在server中
This commit is contained in:
2025-12-17 12:32:21 +08:00
parent 1bc9c6a924
commit 53555a31c0
23 changed files with 15 additions and 17 deletions

View File

@ -0,0 +1,2 @@
// Package api defines the HTTP control plane of the push service
package api

View File

@ -0,0 +1,2 @@
// Package dto contains data transfer objects used in the interval API.
package dto

View File

@ -0,0 +1,17 @@
package dto
import (
"git.jinshen.cn/remilia/push-server/interval/server/model"
)
type Message struct {
Topic string `json:"topic"`
Content string `json:"content"`
}
func MessageFromModel(m model.Message) Message {
return Message{
Topic: string(m.Topic),
Content: string(m.Content),
}
}

View File

@ -0,0 +1,5 @@
package dto
type PublishRequest struct {
Content string `json:"content"`
}

View File

@ -0,0 +1,17 @@
package dto
import (
"git.jinshen.cn/remilia/push-server/interval/server/model"
)
type Subscription struct {
Topic string `json:"topic"`
ClientID string `json:"client_id"`
}
func SubscriptionFromModel(s model.Subscription) Subscription {
return Subscription{
Topic: string(s.Topic),
ClientID: string(s.ClientID),
}
}

View File

@ -0,0 +1,2 @@
// Package handler contains HTTP request handlers for the REST API.
package handler

View File

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

View File

@ -0,0 +1,47 @@
package handler
import (
"encoding/json"
"net/http"
"time"
"git.jinshen.cn/remilia/push-server/interval/server/api/dto"
"git.jinshen.cn/remilia/push-server/interval/server/model"
"git.jinshen.cn/remilia/push-server/interval/server/ws"
"github.com/go-chi/chi/v5"
)
func PushHandler(hub *ws.Hub) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
topicStr := chi.URLParam(r, "topic")
topic := model.Topic(topicStr)
if !topic.Valid() {
http.Error(w, "invalid topic", http.StatusBadRequest)
return
}
var req dto.PublishRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, "invalid request body", http.StatusBadRequest)
return
}
if req.Content == "" {
http.Error(w, "content cannot be empty", http.StatusBadRequest)
return
}
msg := model.Message{
Topic: topic,
Content: []byte(req.Content),
Timestamp: time.Now().Unix(),
}
if err := hub.BroadcastMessage(r.Context(), msg); err != nil {
http.Error(w, "request cancelled", http.StatusRequestTimeout)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusAccepted)
}
}

View File

@ -0,0 +1,21 @@
package api
import (
"context"
"net/http"
"git.jinshen.cn/remilia/push-server/interval/server/api/handler"
"git.jinshen.cn/remilia/push-server/interval/server/ws"
"github.com/go-chi/chi/v5"
)
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))
return r
}