refactor: 重构项目结构
- 将server端相关依赖单独防止在server中
This commit is contained in:
2
interval/server/api/doc.go
Normal file
2
interval/server/api/doc.go
Normal file
@ -0,0 +1,2 @@
|
||||
// Package api defines the HTTP control plane of the push service
|
||||
package api
|
||||
2
interval/server/api/dto/doc.go
Normal file
2
interval/server/api/dto/doc.go
Normal file
@ -0,0 +1,2 @@
|
||||
// Package dto contains data transfer objects used in the interval API.
|
||||
package dto
|
||||
17
interval/server/api/dto/message.go
Normal file
17
interval/server/api/dto/message.go
Normal 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),
|
||||
}
|
||||
}
|
||||
5
interval/server/api/dto/publish.go
Normal file
5
interval/server/api/dto/publish.go
Normal file
@ -0,0 +1,5 @@
|
||||
package dto
|
||||
|
||||
type PublishRequest struct {
|
||||
Content string `json:"content"`
|
||||
}
|
||||
17
interval/server/api/dto/subscription.go
Normal file
17
interval/server/api/dto/subscription.go
Normal 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),
|
||||
}
|
||||
}
|
||||
2
interval/server/api/handler/doc.go
Normal file
2
interval/server/api/handler/doc.go
Normal file
@ -0,0 +1,2 @@
|
||||
// Package handler contains HTTP request handlers for the REST API.
|
||||
package handler
|
||||
10
interval/server/api/handler/health.go
Normal file
10
interval/server/api/handler/health.go
Normal 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)
|
||||
}
|
||||
}
|
||||
47
interval/server/api/handler/push.go
Normal file
47
interval/server/api/handler/push.go
Normal 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)
|
||||
}
|
||||
}
|
||||
21
interval/server/api/router.go
Normal file
21
interval/server/api/router.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user