Skip to content

Commit

Permalink
fix应用切换
Browse files Browse the repository at this point in the history
  • Loading branch information
jianyun8023 committed Aug 23, 2024
1 parent 48937a7 commit 64e9140
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
47 changes: 24 additions & 23 deletions internal/calibre/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type Api struct {
useIndex string
}

func (c Api) SetupRouter(r *gin.Engine) {
func (c *Api) SetupRouter(r *gin.Engine) {

base := r.Group("/api")
base.GET("/get/cover/:id", c.getCover)
Expand All @@ -52,11 +52,11 @@ func (c Api) SetupRouter(r *gin.Engine) {
base.POST("/index/switch", c.switchIndex)
}

func (c Api) currentIndex() *meilisearch.Index {
func (c *Api) currentIndex() *meilisearch.Index {
return c.client.Index(c.useIndex)
}

func NewClient(config *Config) Api {
func NewClient(config *Config) *Api {
client := meilisearch.NewClient(meilisearch.ClientConfig{
Host: config.Search.Host,
APIKey: config.Search.APIKey,
Expand All @@ -80,14 +80,15 @@ func NewClient(config *Config) Api {
if err != nil {
log.Fatal(err)
}
return Api{
api := Api{
config: config,
client: client,
baseDir: config.TmpDir,
contentApi: &newClient,
http: newClient.Client,
useIndex: config.Search.Index,
}
return &api
}

// ensureIndexExists checks if a Meilisearch index exists, and if not, creates it and updates its settings.
Expand Down Expand Up @@ -133,7 +134,7 @@ func ensureIndexExists(client *meilisearch.Client, indexName string) (*meilisear
return index, nil
}

func (c Api) search(r *gin.Context) {
func (c *Api) search(r *gin.Context) {
var req = meilisearch.SearchRequest{}
err2 := r.Bind(&req)
if err2 != nil {
Expand Down Expand Up @@ -180,7 +181,7 @@ func (c Api) search(r *gin.Context) {
})
}

func (c Api) getBook(r *gin.Context) {
func (c *Api) getBook(r *gin.Context) {
id := r.Param("id")
var book Book
err := c.currentIndex().GetDocument(id, nil, &book)
Expand All @@ -194,7 +195,7 @@ func (c Api) getBook(r *gin.Context) {

}

func (c Api) deleteBook(r *gin.Context) {
func (c *Api) deleteBook(r *gin.Context) {
id := r.Param("id")

err := c.contentApi.DeleteBooks([]string{id}, "")
Expand All @@ -211,7 +212,7 @@ func (c Api) deleteBook(r *gin.Context) {
r.JSON(http.StatusOK, "success")
}

func (c Api) getBookToc(r *gin.Context) {
func (c *Api) getBookToc(r *gin.Context) {
id := strings.TrimSuffix(r.Param("id"), ".epub")

filepath, _ := c.getFileOrCache(id)
Expand Down Expand Up @@ -239,7 +240,7 @@ func (c Api) getBookToc(r *gin.Context) {

}

func (c Api) expansionTree(ori []epub.NavPoint) []epub.NavPoint {
func (c *Api) expansionTree(ori []epub.NavPoint) []epub.NavPoint {
var points []epub.NavPoint
for i := range ori {
point := ori[i]
Expand All @@ -251,7 +252,7 @@ func (c Api) expansionTree(ori []epub.NavPoint) []epub.NavPoint {
return points
}

func (c Api) getBookContent(r *gin.Context) {
func (c *Api) getBookContent(r *gin.Context) {
id := strings.TrimSuffix(r.Param("id"), ".epub")

//path1 := path.Join(c.Query("baseDir"), c.Query("path"))
Expand Down Expand Up @@ -285,12 +286,12 @@ func (c Api) getBookContent(r *gin.Context) {
}
}

func (c Api) getFile(id string) (int64, io.ReadCloser, error) {
func (c *Api) getFile(id string) (int64, io.ReadCloser, error) {
size, reader, err := c.contentApi.GetBook(id, "library")
return size, reader, err
}

func (c Api) getBookFile(r *gin.Context) {
func (c *Api) getBookFile(r *gin.Context) {
filesuffix := path.Ext(r.Param("id"))
id := strings.TrimSuffix(r.Param("id"), filesuffix)

Expand All @@ -306,7 +307,7 @@ func (c Api) getBookFile(r *gin.Context) {
r.DataFromReader(http.StatusOK, size, "application/epub+zip", reader, nil)
}

func (c Api) getCover(r *gin.Context) {
func (c *Api) getCover(r *gin.Context) {
id := strings.TrimSuffix(r.Param("id"), ".jpg")
size, reader, err := c.contentApi.GetCover(id, "library")
if err != nil {
Expand All @@ -320,7 +321,7 @@ func (c Api) getCover(r *gin.Context) {
r.DataFromReader(http.StatusOK, size, "image/jpeg", reader, nil)
}

func (c Api) getFileOrCache(id string) (string, error) {
func (c *Api) getFileOrCache(id string) (string, error) {
filename := path.Join(c.baseDir, id+".epub")
_, err := os.Stat(filename)
if Exists(filename) {
Expand All @@ -346,7 +347,7 @@ func (c Api) getFileOrCache(id string) (string, error) {
return filename, err
}

func (c Api) switchIndex(c2 *gin.Context) {
func (c *Api) switchIndex(c2 *gin.Context) {

resp, err := c.client.GetTasks(&meilisearch.TasksQuery{
Limit: 2,
Expand Down Expand Up @@ -374,7 +375,7 @@ func (c Api) switchIndex(c2 *gin.Context) {
"message": "success",
})
}
func (c Api) updateIndex(c2 *gin.Context) {
func (c *Api) updateIndex(c2 *gin.Context) {
booksIds, err2 := c.contentApi.GetAllBooksIds()
if err2 != nil {
log.Warn(err2)
Expand Down Expand Up @@ -435,7 +436,7 @@ func (c Api) updateIndex(c2 *gin.Context) {
})
}

func waitForTask(c Api, taskIds []int64) error {
func waitForTask(c *Api, taskIds []int64) error {
timeout := time.After(30 * time.Second) // Set timeout duration
done := make(chan bool)

Expand Down Expand Up @@ -474,7 +475,7 @@ func waitForTask(c Api, taskIds []int64) error {
}
}

func (c Api) recently(r *gin.Context) {
func (c *Api) recently(r *gin.Context) {
limit, err := strconv.Atoi(r.DefaultQuery("limit", "10"))
if err != nil {
r.JSON(http.StatusBadRequest, gin.H{"error": "Invalid limit"})
Expand Down Expand Up @@ -530,7 +531,7 @@ func (c Api) recently(r *gin.Context) {
})
}

func (c Api) updateMetadata(r *gin.Context) {
func (c *Api) updateMetadata(r *gin.Context) {
id := r.Param("id")
book := &Book{}
err := r.Bind(book)
Expand Down Expand Up @@ -652,7 +653,7 @@ func convertContentToBooks(content map[string]content.Content) ([]Book, error) {
return books, nil
}

func (c Api) getIsbn(c2 *gin.Context) {
func (c *Api) getIsbn(c2 *gin.Context) {
isbn := c2.Param("isbn")
var jsonData map[string]interface{}
resp, err := c.http.R().SetResult(&jsonData).Get(c.config.Metadata.DoubanUrl + "/v2/book/isbn/" + isbn)
Expand All @@ -664,7 +665,7 @@ func (c Api) getIsbn(c2 *gin.Context) {
c2.JSON(http.StatusOK, resp.Result())
}

func (c Api) queryMetadata(c2 *gin.Context) {
func (c *Api) queryMetadata(c2 *gin.Context) {
query := c2.Query("query")
//url encode
var jsonData map[string]interface{}
Expand All @@ -677,7 +678,7 @@ func (c Api) queryMetadata(c2 *gin.Context) {
c2.JSON(http.StatusOK, resp.Result())
}

func (c Api) proxyCover(r *gin.Context) {
func (c *Api) proxyCover(r *gin.Context) {
path := strings.TrimPrefix(r.Param("path"), "/")
log.Infof("proxy cover: %s", path)
response, err := c.http.R().SetDoNotParseResponse(true).
Expand All @@ -697,7 +698,7 @@ func (c Api) proxyCover(r *gin.Context) {
r.DataFromReader(http.StatusOK, length, "image/jpeg", reader, nil)
}

func (c Api) listPublisher(context *gin.Context) {
func (c *Api) listPublisher(context *gin.Context) {
publishers, err := c.contentApi.GetAllPublisher()

if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ func main() {
}
r := gin.Default()
setPages(r, conf)
calibre.NewClient(conf).SetupRouter(r)
client := calibre.NewClient(conf)
client.SetupRouter(r)
for _, route := range r.Routes() {
log.Infof("route: %s %s", route.Method, route.Path)
}
Expand Down

0 comments on commit 64e9140

Please sign in to comment.