Initial commit

This commit is contained in:
zs
2026-02-28 20:05:15 +08:00
commit c66f5f9be4
185 changed files with 18356 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
package handler
import (
"net/http"
"os"
"github.com/zs/InsightReply/internal/service"
)
type AIHandler struct {
svc *service.AIService
}
func NewAIHandler(svc *service.AIService) *AIHandler {
return &AIHandler{svc: svc}
}
func (h *AIHandler) Test(w http.ResponseWriter, r *http.Request) {
// ...
}
func (h *AIHandler) Generate(w http.ResponseWriter, r *http.Request) {
var body struct {
TweetContent string `json:"tweet_content"`
Strategy string `json:"strategy"`
Identity string `json:"identity"`
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
SendError(w, http.StatusBadRequest, 4001, "Invalid request body")
return
}
if body.TweetContent == "" {
SendError(w, http.StatusBadRequest, 4002, "Tweet content is required")
return
}
ctx := r.Context()
reply, err := h.svc.GenerateReply(ctx, body.TweetContent, body.Strategy, body.Identity)
if err != nil {
SendError(w, http.StatusBadGateway, 5002, "Failed to generate AI reply: "+err.Error())
return
}
SendSuccess(w, map[string]string{
"reply": reply,
})
}

View File

@@ -0,0 +1,32 @@
package handler
import (
"encoding/json"
"net/http"
)
type Response struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data"`
}
func SendSuccess(w http.ResponseWriter, data interface{}) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(Response{
Code: 200,
Message: "success",
Data: data,
})
}
func SendError(w http.ResponseWriter, httpStatus int, bizCode int, message string) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(httpStatus)
json.NewEncoder(w).Encode(Response{
Code: bizCode,
Message: message,
Data: nil,
})
}

View File

@@ -0,0 +1,36 @@
package handler
import (
"encoding/json"
"net/http"
"github.com/zs/InsightReply/internal/service"
)
type UserHandler struct {
svc *service.UserService
}
func NewUserHandler(svc *service.UserService) *UserHandler {
return &UserHandler{svc: svc}
}
func (h *UserHandler) Register(w http.ResponseWriter, r *http.Request) {
var body struct {
Email string `json:"email"`
Identity string `json:"identity"`
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
SendError(w, http.StatusBadRequest, 4001, "Invalid request body")
return
}
user, err := h.svc.Register(body.Email, body.Identity)
if err != nil {
SendError(w, http.StatusInternalServerError, 5001, "Failed to register user")
return
}
SendSuccess(w, user)
}