first draft version

This commit is contained in:
Simon Ding
2024-06-22 17:15:24 +08:00
parent be0dc4a466
commit ab8e653c5b
63 changed files with 15137 additions and 17 deletions

36
server/common.go Normal file
View File

@@ -0,0 +1,36 @@
package server
import (
"polaris/log"
"fmt"
"github.com/gin-gonic/gin"
)
func HttpHandler(f func(*gin.Context) (interface{}, error)) gin.HandlerFunc {
return func(ctx *gin.Context) {
r, err := f(ctx)
if err != nil {
ctx.JSON(200, Response{
Code: 1,
Message: fmt.Sprintf("%v", err),
})
return
}
log.Infof("url %v return: %+v", ctx.Request.URL, r)
ctx.JSON(200, Response{
Code: 0,
Message: "success",
Data: r,
})
}
}
type Response struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data"`
}

62
server/server.go Normal file
View File

@@ -0,0 +1,62 @@
package server
import (
"polaris/db"
"polaris/log"
"polaris/pkg/tmdb"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
)
func NewServer(db *db.Client) *Server {
r := gin.Default()
return &Server{
r: r,
db: db,
}
}
type Server struct {
r *gin.Engine
db *db.Client
language string
}
func (s *Server) Serve() error {
api := s.r.Group("/api/v1")
setting := api.Group("/setting")
{
setting.POST("/do", HttpHandler(s.SetSetting))
setting.GET("/do", HttpHandler(s.GetSetting))
}
tv := api.Group("/tv")
{
tv.GET("/search", HttpHandler(s.SearchTvSeries))
tv.POST("/watchlist", HttpHandler(s.AddWatchlist))
tv.GET("/watchlist", HttpHandler(s.GetWatchlist))
}
s.language = s.db.GetLanguage()
return s.r.Run(":8080")
}
func (s *Server) TMDB() (*tmdb.Client, error) {
api := s.db.GetSetting(db.SettingTmdbApiKey)
if api == "" {
return nil, errors.New("tmdb api not set")
}
return tmdb.NewClient(api)
}
func (s *Server) MustTMDB() *tmdb.Client {
t, err := s.TMDB()
if err != nil {
log.Panicf("get tmdb: %v", err)
}
return t
}

33
server/setting.go Normal file
View File

@@ -0,0 +1,33 @@
package server
import (
"polaris/log"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
)
type setSettingIn struct {
Key string `json:"key"`
Value string `json:"value"`
}
func (s *Server) SetSetting(c *gin.Context) (interface{}, error) {
var in setSettingIn
if err := c.ShouldBindJSON(&in); err != nil {
return nil, errors.Wrap(err, "bind json")
}
err := s.db.SetSetting(in.Key, in.Value)
return nil, err
}
func (s *Server) GetSetting(c *gin.Context) (interface{}, error) {
q := c.Query("key")
log.Infof("query key: %v", q)
if q == "" {
return nil, nil
}
v := s.db.GetSetting(q)
log.Infof("get value for key %v: %v", q, v)
return v, nil
}

52
server/watchlist.go Normal file
View File

@@ -0,0 +1,52 @@
package server
import (
"polaris/log"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
)
type searchTvParam struct {
Query string `form:"query"`
}
func (s *Server) SearchTvSeries(c *gin.Context) (interface{}, error) {
var q searchTvParam
if err := c.ShouldBindQuery(&q); err != nil {
return nil, errors.Wrap(err, "bind query")
}
log.Infof("search tv series with keyword: %v", q.Query)
r, err := s.MustTMDB().SearchTvShow(q.Query, "")
if err != nil {
return nil, errors.Wrap(err, "search tv")
}
return r, nil
}
type addWatchlistIn struct {
ID int `json:"id" binding:"required"`
RootFolder string `json:"folder" binding:"required"`
}
func (s *Server) AddWatchlist(c *gin.Context) (interface{}, error) {
var in addWatchlistIn
if err := c.ShouldBindQuery(&in); err != nil {
return nil, errors.Wrap(err, "bind query")
}
detail, err := s.MustTMDB().GetTvDetails(in.ID, s.language)
if err != nil {
return nil, errors.Wrap(err, "get tv detail")
}
if err := s.db.AddWatchlist(in.RootFolder, detail); err != nil {
return nil, errors.Wrap(err, "add to list")
}
log.Infof("add tv %s to watchlist success", detail.Name)
return nil, nil
}
func (s *Server) GetWatchlist(c *gin.Context) (interface{}, error) {
list := s.db.GetWatchlist()
return list, nil
}