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.
52 lines
1.2 KiB
Go
52 lines
1.2 KiB
Go
package grpc
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"log/slog"
|
|
|
|
pb "schedule_converter/proto/runner"
|
|
|
|
"schedule_converter/config"
|
|
"schedule_converter/parser"
|
|
apperr "schedule_converter/pkg/errors"
|
|
)
|
|
|
|
func (h *TaskHandler) getEmptyClassrooms(ctx context.Context, payload []byte) ([]byte, error) {
|
|
var req pb.GetEmptyClassroomPayload
|
|
if err := unmarshalPayload(payload, &req); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
log := slog.With("username", req.Username, "semester", req.Semester)
|
|
log.Info("获取空教室任务")
|
|
|
|
httpClient, err := createHTTPClient()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
cfg := config.Get()
|
|
setTWFIDCookie(httpClient, cfg.BaseURL, "")
|
|
|
|
if err := executeLogin(ctx, httpClient, req.Username, req.Password); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
classrooms, err := parser.FetchEmptyClassrooms(httpClient, req.Semester, req.WeekStart, req.WeekEnd, req.CampusCode)
|
|
if err != nil {
|
|
return nil, apperr.New("fetch_empty_classrooms", err, "获取空教室失败")
|
|
}
|
|
|
|
pbClassrooms := convertEmptyClassrooms(classrooms)
|
|
|
|
result := &pb.EmptyClassroomResultData{
|
|
StudentId: req.Username,
|
|
Semester: req.Semester,
|
|
Classrooms: pbClassrooms,
|
|
}
|
|
|
|
log.Info("空教室获取成功", "classroom_count", len(pbClassrooms))
|
|
return json.Marshal(result)
|
|
}
|