-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
74 lines (66 loc) · 2.41 KB
/
handlers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"encoding/json"
"fmt"
"github.com/zenazn/goji/web"
"math"
"net/http"
"regexp"
"strconv"
// "strings"
)
const perPage = 10
var categoryRegex = regexp.MustCompile(`^(\d{4})(?:\/(\d{1,2})(?:\/(\d{1,2}))?)?$`)
/*************************************** Handlers *******************************/
func writeJSONResponse(response map[string]interface{}, writer http.ResponseWriter) {
writer.Header().Set("Content-Type", "application/json")
responseString, _ := json.Marshal(response)
fmt.Fprintf(writer, string(responseString))
}
var PhotosHandler = func(context web.C, writer http.ResponseWriter, request *http.Request) {
var count int
db.Table("photo").Count(&count)
page, err := strconv.Atoi(context.URLParams["page"])
CheckError(err, "Invalid page param")
photos := LoadPhotos(db, (page-1)*perPage, perPage)
totalPages := math.Ceil(float64(count) / float64(perPage))
writeJSONResponse(map[string]interface{}{"photos": photos, "count": count, "page": page, "total_pages": totalPages}, writer)
}
var PhotoHandler = func(context web.C, writer http.ResponseWriter, request *http.Request) {
photoId, err := strconv.Atoi(context.URLParams["id"])
CheckError(err, "Invalid ID")
var photo Photo
db.First(&photo, photoId)
photo.Thumbnail = "/images/2-thumbnail.png"
photo.Poster = "/images/2-poster.png"
writeJSONResponse(map[string]interface{}{"photo": photo}, writer)
}
// var PhotoCategoryHandler = func(c *gin.Context) {
// page := c.MustGet("page").(int)
// category := c.Params.ByName("category")
// category = strings.TrimLeft(category, "/")
// category = strings.TrimRight(category, "/")
// matches := categoryRegex.FindAllStringSubmatch(category, -1)
// if matches != nil {
// fmt.Println(matches)
// photos := LoadPhotos(db, page*perPage, perPage)
// c.JSON(200, gin.H{"photos": photos, "status": 200})
// } else {
// c.JSON(400, gin.H{"message": "Invalid category", "status": 400})
// }
// }
//
var runningIndex bool = false
var ReindexHandler = func(context web.C, writer http.ResponseWriter, request *http.Request) {
if runningIndex {
writeJSONResponse(map[string]interface{}{"message": "Reindex already running"}, writer)
} else {
runningIndex = true
go reindexWithCompletion()
writeJSONResponse(map[string]interface{}{"message": "Reindex started"}, writer)
}
}
func reindexWithCompletion() {
RebuildIndex(db, config.Get("rootPhotoDirectory"), ".", false, false)
runningIndex = false
}