-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_server.go
111 lines (92 loc) · 2.93 KB
/
command_server.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"net/http"
"os"
"path"
"strconv"
"github.com/codegangsta/cli"
"github.com/codegangsta/negroni"
"github.com/gorilla/mux"
"github.com/jinzhu/gorm"
"github.com/unrolled/render"
)
var (
Render = render.New()
DB gorm.DB
AppConfig Config
)
func RunServer(c *cli.Context) {
router := mux.NewRouter()
var err error
AppConfig, err = LoadConfig(c.String("config"))
if err != nil {
os.Exit(1)
}
DB = SetupDatabase(AppConfig.DatabaseConnectionString)
router.HandleFunc("/media", MediaIndexHandler)
router.HandleFunc("/media/{id}", MediaHandler)
router.HandleFunc("/media/{id}/{thumb}", ThumbnailHandler)
router.HandleFunc("/similar", ResemblancesHandler)
router.HandleFunc("/all_dates", AllDates)
server := negroni.Classic()
server.UseHandler(router)
server.Run(":3001")
}
type JSONResponse map[string]interface{}
func AllDates(response http.ResponseWriter, request *http.Request) {
Render.JSON(response, http.StatusOK, JSONResponse{
"dates": AllDistinctDates(DB),
})
}
func MediaIndexHandler(response http.ResponseWriter, request *http.Request) {
page := parseIntValueForParamWithDefault(request, "page", 1)
year := parseIntValueForParamWithDefaultZero(request, "year")
month := parseIntValueForParamWithDefaultZero(request, "month")
day := parseIntValueForParamWithDefaultZero(request, "day")
media := AllMediaForDate(DB, page, year, month, day)
Render.JSON(response, http.StatusOK, media)
}
func MediaHandler(response http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request)
id := vars["id"]
var media Media
if DB.Preload("Resemblance").First(&media, id).RecordNotFound() {
errorResponse := JSONResponse{"error": "No media found for ID " + id}
Render.JSON(response, http.StatusNotFound, errorResponse)
} else {
Render.JSON(response, http.StatusOK, media)
}
}
func ThumbnailHandler(response http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request)
id := vars["id"]
thumb := vars["thumb"]
var media Media
if DB.First(&media, id).RecordNotFound() {
errorResponse := JSONResponse{"error": "No media found for ID " + id}
Render.JSON(response, http.StatusNotFound, errorResponse)
} else {
thumbFile := path.Join(
AppConfig.ThumbnailsFolderPath,
PartitionIdAsPath(media.Id),
thumb+ThumbnailExtension,
)
http.ServeFile(response, request, thumbFile)
}
}
func ResemblancesHandler(response http.ResponseWriter, request *http.Request) {
var resemblances []Resemblance
DB.Find(&resemblances)
Render.JSON(response, http.StatusOK, resemblances)
}
func parseIntValueForParamWithDefault(request *http.Request, param string, defaultValue int) int {
paramString := request.URL.Query().Get(param)
paramValue := defaultValue
if paramString != "" {
paramValue, _ = strconv.Atoi(paramString)
}
return paramValue
}
func parseIntValueForParamWithDefaultZero(request *http.Request, param string) int {
return parseIntValueForParamWithDefault(request, param, 0)
}