2024-07-01 17:22:31 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-07-02 17:50:49 +08:00
|
|
|
"errors"
|
2024-07-01 17:22:31 +08:00
|
|
|
"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()},
|
2024-07-02 17:50:49 +08:00
|
|
|
ErrorHandler: func(ctx fiber.Ctx, err error) error {
|
|
|
|
// Status code defaults to 500
|
|
|
|
code := fiber.StatusInternalServerError
|
|
|
|
|
|
|
|
// Retrieve the custom status code if it's a *fiber.Error
|
|
|
|
var e *fiber.Error
|
|
|
|
if errors.As(err, &e) {
|
|
|
|
code = e.Code
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send custom error page
|
|
|
|
err = ctx.Status(code).JSON(fiber.Map{
|
|
|
|
"data": "",
|
|
|
|
"code": code,
|
|
|
|
"msg": err.Error(),
|
|
|
|
})
|
|
|
|
|
|
|
|
// Return from handler
|
|
|
|
return nil
|
|
|
|
},
|
2024-07-01 17:22:31 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
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",
|
|
|
|
})
|
|
|
|
}
|