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
}

View File

@ -1,2 +0,0 @@
// Package server provides HTTP server abstractions.
package server

View File

@ -0,0 +1,2 @@
// Package httpserver provides HTTP server abstractions.
package httpserver

View File

@ -1,4 +1,4 @@
package server
package httpserver
import (
"context"

View File

@ -0,0 +1,2 @@
// Package model defines core domain models of the push service.
package model

View File

@ -0,0 +1,7 @@
package model
type Message struct {
Topic Topic
Content []byte
Timestamp int64
}

View File

@ -0,0 +1,6 @@
package model
type Subscription struct {
Topic Topic
ClientID string
}

View File

@ -0,0 +1,7 @@
package model
type Topic string
func (t Topic) Valid() bool {
return t != ""
}

View File

@ -0,0 +1,58 @@
package ws
import (
"context"
"log"
"time"
"github.com/coder/websocket"
)
// Client represents a connected client in the hub.
type Client struct {
ID string
Conn *websocket.Conn
SendChan chan []byte
Ctx context.Context
Cancel context.CancelFunc
}
func NewClient(id string, conn *websocket.Conn, parentCtx context.Context) *Client {
ctx, cancel := context.WithCancel(parentCtx)
return &Client{
ID: id,
Conn: conn,
SendChan: make(chan []byte, 32),
Ctx: ctx,
Cancel: cancel,
}
}
// Write message to websocket connection.
func (c *Client) WriteMessage() {
defer func() {
_ = c.Conn.Close(websocket.StatusNormalClosure, "client writer closed")
}()
for {
select {
case <-c.Ctx.Done():
return
case msg, ok := <-c.SendChan:
if !ok {
return
}
writeCtx, cancel := context.WithTimeout(c.Ctx, 5*time.Second)
err := c.Conn.Write(writeCtx, websocket.MessageText, msg)
cancel()
if err != nil {
log.Println("WebSocket write error:", err)
return
}
}
}
}

View File

@ -0,0 +1,2 @@
// Package ws implements the Websocket handler for the push service.
package ws

View File

@ -0,0 +1,75 @@
package ws
import (
"context"
"io"
"log"
"net/http"
"time"
"github.com/coder/websocket"
)
func Handler(ctx context.Context, h *Hub) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
conn, err := websocket.Accept(w, r, &websocket.AcceptOptions{
OriginPatterns: []string{"*"},
})
log.Println("New WebSocket connection from", r.RemoteAddr, "at", time.Now().Format(time.RFC3339))
if err != nil {
log.Println("WebSocket accept error:", err)
return
}
c := NewClient(r.RemoteAddr, conn, ctx)
log.Println("Client", r.RemoteAddr, "connected.")
h.RegisterClient(c)
go echo(c, h)
go heartbeat(c)
}
}
func echo(c *Client, h *Hub) {
defer func() {
if c.Conn != nil {
log.Println("Closing WebSocket connection")
h.UnregisterClient(c)
c.Cancel()
_ = c.Conn.Close(websocket.StatusNormalClosure, "echo finished")
}
}()
for {
typ, r, err := c.Conn.Reader(c.Ctx)
if err != nil {
if websocket.CloseStatus(err) == websocket.StatusNormalClosure {
log.Println("WebSocket connection closed normally")
} else {
log.Println("WebSocket reader error:", err)
}
return
}
w, err := c.Conn.Writer(c.Ctx, typ)
if err != nil {
log.Println("WebSocket writer error:", err)
return
}
_, err = io.Copy(w, r)
if err != nil {
log.Println("WebSocket copy error:", err)
return
}
if err = w.Close(); err != nil {
log.Println("WebSocket writer close error:", err)
return
}
}
}

View File

@ -0,0 +1,35 @@
package ws
import (
"context"
"github.com/coder/websocket"
"log"
"time"
)
func heartbeat(c *Client) {
ticker := time.NewTicker(30 * time.Second)
defer ticker.Stop()
defer func() {
c.Cancel()
_ = c.Conn.Close(websocket.StatusNormalClosure, "heartbeat stopped")
}()
for {
select {
case <-c.Ctx.Done():
return
case <-ticker.C:
pingCtx, pingCancel := context.WithTimeout(c.Ctx, 5*time.Second)
err := c.Conn.Ping(pingCtx)
pingCancel()
if err != nil {
log.Println("Ping filed: ", err)
return
}
}
}
}

178
interval/server/ws/hub.go Normal file
View File

@ -0,0 +1,178 @@
package ws
import (
"context"
"log"
"git.jinshen.cn/remilia/push-server/interval/server/model"
"github.com/coder/websocket"
)
// Hub is the central message distribution hub.
type Hub struct {
// ClientID -> *Client
clientsByID map[string]*Client
// topic -> clientID -> *Client
//
// Invariant:
// - clientsByTopic contains only topics with at least one active subscriber.
// - A topic key must not exist if it has zero clients.
clientsByTopic map[model.Topic]map[string]*Client
// clientID -> topic -> struct{}
topicsByClients map[string]map[model.Topic]struct{}
register chan *Client
unregister chan *Client
subscribe chan model.Subscription
unsubscribe chan model.Subscription
broadcast chan model.Message
}
func NewHub() *Hub {
return &Hub{
clientsByID: make(map[string]*Client),
clientsByTopic: make(map[model.Topic]map[string]*Client),
topicsByClients: make(map[string]map[model.Topic]struct{}),
register: make(chan *Client),
unregister: make(chan *Client),
subscribe: make(chan model.Subscription, 8),
unsubscribe: make(chan model.Subscription, 8),
broadcast: make(chan model.Message, 64),
}
}
func (h *Hub) RegisterClient(client *Client) {
h.register <- client
}
func (h *Hub) UnregisterClient(client *Client) {
h.unregister <- client
}
func (h *Hub) Subscribe(sub model.Subscription) {
h.subscribe <- sub
}
func (h *Hub) Unsubscribe(sub model.Subscription) {
h.unsubscribe <- sub
}
func (h *Hub) BroadcastMessage(ctx context.Context, msg model.Message) error {
select {
case h.broadcast <- msg:
return nil
case <-ctx.Done():
return ctx.Err()
}
}
func (h *Hub) Run(ctx context.Context) {
log.Println("Hub is running")
for {
select {
case c := <-h.register:
h.onRegister(c)
case c := <-h.unregister:
h.onUnregister(c)
case c := <-h.subscribe:
h.onSubscribe(c)
case s := <-h.unsubscribe:
h.onUnsubscribe(s)
case msg := <-h.broadcast:
h.onBroadcast(msg)
case <-ctx.Done():
h.shutdown()
return
}
}
}
// Get a client by its ClientID
func (h *Hub) getClient(id string) (*Client, bool) {
c, ok := h.clientsByID[id]
return c, ok
}
// Create a new entry for the client in topicsByClients map when it registers.
func (h *Hub) onRegister(c *Client) {
h.clientsByID[c.ID] = c
h.topicsByClients[c.ID] = make(map[model.Topic]struct{})
log.Printf("Current clientList: %v\n", h.clientsByID)
}
// Delete all topic subscriptions for the client when it unregisters.
func (h *Hub) onUnregister(c *Client) {
topics := h.topicsByClients[c.ID]
for t := range topics {
if clients, ok := h.clientsByTopic[t]; ok {
delete(clients, c.ID)
if len(clients) == 0 {
delete(h.clientsByTopic, t)
}
}
}
delete(h.topicsByClients, c.ID)
delete(h.clientsByID, c.ID)
}
func (h *Hub) onSubscribe(s model.Subscription) {
c, ok := h.getClient(s.ClientID)
if !ok {
// If the client does not exist, log an error and return.
log.Printf("Subscribe failed: client %s not found", s.ClientID)
return
}
if h.clientsByTopic[s.Topic] == nil {
h.clientsByTopic[s.Topic] = make(map[string]*Client)
}
h.clientsByTopic[s.Topic][s.ClientID] = c
h.topicsByClients[s.ClientID][s.Topic] = struct{}{}
}
func (h *Hub) onUnsubscribe(s model.Subscription) {
if clients, ok := h.clientsByTopic[s.Topic]; ok {
delete(clients, s.ClientID)
if len(clients) == 0 {
delete(h.clientsByTopic, s.Topic)
}
}
if topics, ok := h.topicsByClients[s.ClientID]; ok {
delete(topics, s.Topic)
}
}
func (h *Hub) onBroadcast(msg model.Message) {
if !msg.Topic.Valid() {
log.Printf("Broadcast failed: invalid topic")
return
}
log.Printf("Receiving message for topic %s: %s", msg.Topic, string(msg.Content))
for _, c := range h.clientsByTopic[msg.Topic] {
select {
case c.SendChan <- msg.Content:
default:
h.UnregisterClient(c)
if c.Conn != nil {
_ = c.Conn.Close(websocket.StatusPolicyViolation, "Slow consumer")
}
}
}
}
func (h *Hub) shutdown() {
for _, c := range h.clientsByID {
close(c.SendChan)
if c.Conn != nil {
_ = c.Conn.Close(websocket.StatusNormalClosure, "Server shutdown")
}
}
}