refactor: reorganize codebase into modular packages

feat(global): add global package for shared variables and types
refactor(handlers): move handlers to dedicated package and update imports
refactor(session): extract session management to separate package
refactor(config): move config handling to dedicated package
refactor(router): update route handlers to use new package structure
refactor(main): simplify main.go by moving logic to packages
This commit is contained in:
2026-04-22 12:57:04 +08:00
parent df8df78bab
commit ddf91299e7
11 changed files with 667 additions and 663 deletions
+122
View File
@@ -0,0 +1,122 @@
package handlers
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"time"
"super-frpc/database"
"super-frpc/global"
"super-frpc/postLog"
"super-frpc/session"
)
type Response struct {
Success bool `json:"success"`
Message string `json:"message,omitempty"`
Data interface{} `json:"data,omitempty"`
}
func SendErrorResponse(w http.ResponseWriter, statusCode int, message string, data ...interface{}) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
resp := Response{
Success: false,
Message: message,
}
if len(data) > 0 {
resp.Data = data[0]
}
jsonResp, err := json.Marshal(resp)
if err != nil {
postLog.Error(fmt.Sprintf("Failed to marshal error response: %v", err))
return
}
w.Write(jsonResp)
}
func SendSuccessResponse(w http.ResponseWriter, message string, data interface{}) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
resp := Response{
Success: true,
Message: message,
Data: data,
}
jsonResp, err := json.Marshal(resp)
if err != nil {
postLog.Error(fmt.Sprintf("Failed to marshal success response: %v", err))
return
}
w.Write(jsonResp)
}
func Auth(w http.ResponseWriter, r *http.Request, targetMethod string, allowedUserLevels ...string) (int, error) {
if r.Method != targetMethod {
return 0, fmt.Errorf("Method not allowed: %s", targetMethod)
}
if !global.Is.Debug && !session.ValidateTimeStamp(r.Header, global.Is.Debug) {
return 0, fmt.Errorf("Invalid or missing X-Timestamp in header")
}
userID, err := session.ExtractUserIDFromToken(r.Header.Get("X-Token"))
if err != nil {
return 0, fmt.Errorf("Invalid token format: %w", err)
}
if err := session.ValidateToken(userID, r.Header.Get("X-Token")); err != nil {
return 0, fmt.Errorf("Token validation failed: %w", err)
}
if len(allowedUserLevels) > 0 {
currentUser, err := database.DBQuerySpecificUser(userID)
if err != nil {
return 0, fmt.Errorf("Failed to query user: %w", err)
}
allowed := false
for _, level := range allowedUserLevels {
if currentUser.Type == level {
allowed = true
break
}
}
if !allowed {
return 0, fmt.Errorf("User level not allowed: required one of %v, got %s", allowedUserLevels, currentUser.Type)
}
}
return userID, nil
}
func GetUserType(userID int) (string, error) {
user, err := database.GetUserByID(userID)
if err != nil {
return "", fmt.Errorf("Failed to get user type: %w", err)
}
return user.Type, nil
}
func GetClientIP(r *http.Request) string {
forwarded := r.Header.Get("X-Forwarded-For")
if forwarded != "" {
return forwarded
}
return r.RemoteAddr
}
func LogRequest(r *http.Request, userID int) {
postLog.Info(fmt.Sprintf("[%s] %s %s - UserID: %d - IP: %s",
time.Now().Format("2006-01-02 15:04:05"),
r.Method,
r.URL.Path,
userID,
GetClientIP(r),
))
}
func IntToString(i int) string {
return strconv.Itoa(i)
}