减少变量
This commit is contained in:
35
bin/main.go
35
bin/main.go
@@ -1,7 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -46,6 +45,13 @@ type Image struct {
|
|||||||
UpdateTime time.Time `json:"update_time"`
|
UpdateTime time.Time `json:"update_time"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Images struct {
|
||||||
|
Page int `json:"page"`
|
||||||
|
PageSize int `json:"page_size"`
|
||||||
|
Total int `json:"total"`
|
||||||
|
List []Image `json:"list"`
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
runtime.GOMAXPROCS(runtime.NumCPU())
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
||||||
|
|
||||||
@@ -83,9 +89,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 获取图片列表
|
// 获取图片列表
|
||||||
var images []Image
|
var images Images
|
||||||
page, size := stringToInt(r.URL.Query().Get("page"), 1), stringToInt(r.URL.Query().Get("pageSize"), 10)
|
images.Page, images.PageSize = stringToInt(r.URL.Query().Get("page"), 1), stringToInt(r.URL.Query().Get("pageSize"), 10)
|
||||||
rows, err := mysqlConnection.Database.Query("SELECT id, content, update_time, create_time FROM web_images LIMIT ?, ?", (page-1)*size, size)
|
rows, err := mysqlConnection.Database.Query("SELECT id, content, update_time, create_time FROM web_images LIMIT ?, ?", (images.Page-1)*images.PageSize, images.PageSize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("获取图片列表失败", err)
|
log.Println("获取图片列表失败", err)
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
@@ -100,22 +106,27 @@ func main() {
|
|||||||
image.UpdateTime = image.UpdateTime.UTC()
|
image.UpdateTime = image.UpdateTime.UTC()
|
||||||
image.CreateTime = image.CreateTime.UTC()
|
image.CreateTime = image.CreateTime.UTC()
|
||||||
image.Content = regexp.MustCompile(`http:`).ReplaceAllString(image.Content, "https:")
|
image.Content = regexp.MustCompile(`http:`).ReplaceAllString(image.Content, "https:")
|
||||||
images = append(images, image)
|
images.List = append(images.List, image)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 将对象转换为JSON
|
// 获取总数
|
||||||
data, err := json.Marshal(images)
|
err = mysqlConnection.Database.QueryRow("SELECT COUNT(*) FROM web_images").Scan(&images.Total)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("获取图片总数失败", err)
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将对象转换为有缩进的JSON输出
|
||||||
|
data, err := json.MarshalIndent(images, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("转换图片列表失败", err)
|
log.Println("转换图片列表失败", err)
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||||
|
w.Write(data)
|
||||||
|
|
||||||
// 格式化为有缩进的JSON输出
|
|
||||||
var out bytes.Buffer
|
|
||||||
json.Indent(&out, data, "", " ")
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
w.Write(out.Bytes())
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// URL 格式: /img/{type}-{id}.{format}?width=320&height=320&fit=cover
|
// URL 格式: /img/{type}-{id}.{format}?width=320&height=320&fit=cover
|
||||||
|
Reference in New Issue
Block a user