Initial commit
This commit is contained in:
49
server/internal/handler/ai_handler.go
Normal file
49
server/internal/handler/ai_handler.go
Normal 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,
|
||||
})
|
||||
}
|
||||
32
server/internal/handler/common.go
Normal file
32
server/internal/handler/common.go
Normal 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,
|
||||
})
|
||||
}
|
||||
36
server/internal/handler/user_handler.go
Normal file
36
server/internal/handler/user_handler.go
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user