bilibili/subtitles/main.go

63 lines
1.4 KiB
Go
Raw Normal View History

2024-07-01 17:22:31 +08:00
package main
import (
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/cors"
"github.com/gofiber/fiber/v3/middleware/healthcheck"
"github.com/gofiber/fiber/v3/middleware/recover"
"os"
)
type Request struct {
Url string `json:"url" validate:"required"`
}
func main() {
sessData := os.Getenv("SESSDATA")
if sessData == "" {
panic("SESSDATA is required")
}
cli := NewClient(sessData)
s := &Service{cli: cli}
app := fiber.New(fiber.Config{
StructValidator: &structValidator{validate: validator.New()},
})
app.Use(recover.New(), cors.New())
app.Get(healthcheck.DefaultLivenessEndpoint, healthcheck.NewHealthChecker())
// Provide a minimal config for readiness check
app.Get(healthcheck.DefaultReadinessEndpoint, healthcheck.NewHealthChecker())
app.Post("/api/v1/bilibili/subtitles", s.GetSubtitles)
_ = app.Listen(":80")
}
type Service struct {
cli *Client
}
// GetSubtitles @Summary 获取字幕
// @Description 获取字幕
// @Accept application/json
// @Produce application/json
// @Param json body Request true "请求参数"
// @Router /api/v1/bilibili/subtitles [post]
func (s *Service) GetSubtitles(ctx fiber.Ctx) error {
var req Request
if err := ctx.Bind().JSON(&req); err != nil {
return err
}
data, err := s.cli.GetSubtitles(req.Url)
if err != nil {
return err
}
return ctx.JSON(fiber.Map{
"data": data,
"code": 0,
"msg": "ok",
})
}