refactor(grpc): restructure task handling and migrate to slog
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:
2026-06-09 23:08:03 +08:00
parent ee5c36f3ac
commit accc6567f2
26 changed files with 720 additions and 1442 deletions

View File

@@ -2,6 +2,7 @@ package parser
import (
"fmt"
"log/slog"
"net/http"
"strings"
@@ -40,8 +41,9 @@ func examTypeToCode(t ExamType) string {
}
func FetchExams(httpClient *http.Client, examType ExamType) ([]ExamEntry, error) {
examURL := config.BaseURL + "/kscx/queryKsForXs"
fmt.Printf("[考试] 正在获取考试时间数据 (%s)...\n", examType)
examURL := config.Get().BaseURL + "/kscx/queryKsForXs"
log := slog.With("exam_type", string(examType))
log.Info("获取考试时间数据")
data := fmt.Sprintf("xnxq=&kssjd=%s", examTypeToCode(examType))
req, err := http.NewRequest("POST", examURL, strings.NewReader(data))
@@ -57,34 +59,43 @@ func FetchExams(httpClient *http.Client, examType ExamType) ([]ExamEntry, error)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("获取考试数据失败, 状态码: %d", resp.StatusCode)
}
html, err := decodeResponseBody(resp)
if err != nil {
return nil, err
}
exams, err := ParseExamTimeHTML(html, examType)
exams, err := parseExamTimeHTML(html, examType)
if err != nil {
return nil, err
}
fmt.Printf("[考试] 共获取 %d 条考试记录 (%s)\n", len(exams), examType)
log.Info("考试数据获取完成", "exam_count", len(exams))
return exams, nil
}
func FetchAllExams(httpClient *http.Client) ([]ExamEntry, error) {
var allExams []ExamEntry
var lastErr error
for _, et := range []ExamType{ExamTypeFinal, ExamTypeMidterm, ExamTypeMakeup} {
exams, err := FetchExams(httpClient, et)
if err != nil {
fmt.Printf("[考试] 获取%s考试失败: %v\n", et, err)
slog.Warn("获取考试数据失败", "exam_type", string(et), "error", err)
lastErr = err
continue
}
allExams = append(allExams, exams...)
}
if len(allExams) == 0 && lastErr != nil {
return nil, fmt.Errorf("所有考试类型获取均失败: %w", lastErr)
}
return allExams, nil
}
func ParseExamTimeHTML(html string, examType ExamType) ([]ExamEntry, error) {
func parseExamTimeHTML(html string, examType ExamType) ([]ExamEntry, error) {
doc, err := goquery.NewDocumentFromReader(strings.NewReader(html))
if err != nil {
return nil, err