refactor(grpc): restructure task handling and migrate to slog
All checks were successful
Build / build (push) Successful in 5m28s
All checks were successful
Build / build (push) Successful in 5m28s
Refactor the gRPC client and task handling logic to improve modularity, error handling, and observability. - Split `grpc/handler.go` into specialized handler files (login, classroom, exams, grades, schedule) to reduce complexity. - Replace standard `log` with `log/slog` throughout the project for structured logging. - Refactor `grpc/client.go` to use a thread-safe connection management pattern with `sync.RWMutex`. - Remove the legacy `service` package and HTTP server implementation, transitioning the application to a pure gRPC runner mode. - Clean up `config` and `pkg/errors` to remove unused utilities and simplify the API. - Improve error reporting in parsers and HTTP clients by checking response status codes.
This commit is contained in:
@@ -3,20 +3,15 @@ package captcha
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"schedule_converter/client"
|
||||
"schedule_converter/config"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
)
|
||||
|
||||
func SaveCaptchaImage(httpClient *http.Client) ([]byte, error) {
|
||||
fmt.Println("[2] 获取验证码...")
|
||||
|
||||
req, err := http.NewRequest("GET", config.BaseURL+"/captchaImage", nil)
|
||||
req, err := http.NewRequest("GET", config.Get().BaseURL+"/captchaImage", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -28,25 +23,18 @@ func SaveCaptchaImage(httpClient *http.Client) ([]byte, error) {
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("获取验证码失败, 状态码: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := os.WriteFile("captcha.png", body, 0644); err != nil {
|
||||
fmt.Printf(" 警告: 无法保存验证码图片: %v\n", err)
|
||||
} else {
|
||||
fmt.Println(" 验证码已保存到: captcha.png")
|
||||
}
|
||||
|
||||
return body, nil
|
||||
}
|
||||
|
||||
func Recognize(imagePath string) (string, error) {
|
||||
fmt.Println("[3] 使用固定验证码...")
|
||||
return "....", nil
|
||||
}
|
||||
|
||||
func GetAndRecognize(httpClient *http.Client) (string, error) {
|
||||
_, err := SaveCaptchaImage(httpClient)
|
||||
if err != nil {
|
||||
@@ -58,10 +46,10 @@ func GetAndRecognize(httpClient *http.Client) (string, error) {
|
||||
return "", err
|
||||
}
|
||||
|
||||
fmt.Printf(" 识别结果: %s\n", captchaCode)
|
||||
slog.Debug("验证码识别完成", "result", captchaCode)
|
||||
return captchaCode, nil
|
||||
}
|
||||
|
||||
func ParseHTML(html string) (*goquery.Document, error) {
|
||||
return goquery.NewDocumentFromReader(strings.NewReader(html))
|
||||
}
|
||||
func Recognize(imagePath string) (string, error) {
|
||||
return "....", nil
|
||||
}
|
||||
Reference in New Issue
Block a user