Skip to content

Commit

Permalink
Support for JSON output on (/images/latest.json)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterhellberg committed Dec 15, 2013
1 parent 08e378d commit 4db2043
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 30 deletions.
78 changes: 48 additions & 30 deletions latest_images.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/json"
"encoding/xml"
"fmt"
"net/http"
Expand All @@ -13,54 +14,51 @@ import (
func handleLatestImages(w http.ResponseWriter, r *http.Request) {
contentType(w, "application/xml; charset=utf-8")

fmt.Fprintln(w, latestImagesXML())
fmt.Fprintln(w, marshalXML(latestImages()))
}

func marshalXML(images []*Image) string {
i := Images{Type: "array", Image: images}
func handleLatestImagesJSON(w http.ResponseWriter, r *http.Request) {
contentType(w, "application/json; charset=utf-8")

out, err := xml.MarshalIndent(i, "", " ")

if err != nil {
puts("error: %v\n", err)
}
fmt.Fprintln(w, marshalJSON(latestImages()))
}

return xml.Header + string(out)
func imageURL(fn string) string {
return BASE_URL + "/uploaded_images/" + fn
}

func latestImagesXML() string {
images := handleFileStats(func(f os.FileInfo, id int) *Image {
func latestImages() Images {
images := handleFileStats(func(f os.FileInfo) *Image {
idStr := strings.TrimRight(f.Name(), ".jpg")
return &Image{
Id: atoi(strings.TrimRight(f.Name(), ".jpg")),
Id: atoi(idStr),
Filename: f.Name(),
CreatedAt: f.ModTime(),
UpdatedAt: f.ModTime(),
Size: f.Size(),
Checksum: "foo"}
URL: imageURL(f.Name())}
})

return marshalXML(images)
return Images{Type: "array", Image: images}
}

type Image struct {
XMLName xml.Name `xml:"image"`
Id int `xml:"id"`
Filename string `xml:"filename"`
Orientation int `xml:"orientation"`
CreatedAt time.Time `xml:"created-at"`
UpdatedAt time.Time `xml:"updated-at"`
Width int `xml:"width"`
Height int `xml:"height"`
Size int64 `xml:"hatified-file-size"`
Checksum string `xml:"hatified-file-checksum"`
XMLName xml.Name `xml:"image" json:"-"`
Id int `xml:"id" json:"id"`
Filename string `xml:"filename" json:"filename"`
CreatedAt time.Time `xml:"created-at" json:"created_at"`
UpdatedAt time.Time `xml:"updated-at" json:"updated_at"`
Size int64 `xml:"hatified-file-size"`
URL string `xml:"url"`
}

type Images struct {
XMLName xml.Name `xml:"images"`
Type string `xml:"type,attr"`
Image []*Image
XMLName xml.Name `xml:"images" json:"-"`
Type string `xml:"type,attr" json:"-"`
Image []*Image `json:"images"`
}

type fileStatsHandler func(os.FileInfo, int) *Image
type fileStatsHandler func(os.FileInfo) *Image

func handleFileStats(yield fileStatsHandler) []*Image {
allFileNames, _ := filepath.Glob(UPLOAD_DIR + "/*.jpg")
Expand All @@ -75,14 +73,14 @@ func handleFileStats(yield fileStatsHandler) []*Image {

i := []*Image{}

for idx, fn := range fileNames {
for _, fn := range fileNames {
f, err := os.Stat(fn)

if err != nil {
puts("Unable to stat", fn)
}

i = append(i, yield(f, idx+1))
i = append(i, yield(f))
}

return i
Expand All @@ -91,3 +89,23 @@ func handleFileStats(yield fileStatsHandler) []*Image {
func contentType(w http.ResponseWriter, t string) {
w.Header().Set("Content-Type", t)
}

func marshalXML(images Images) string {
out, err := xml.MarshalIndent(images, "", " ")

if err != nil {
puts("error: %v\n", err)
}

return xml.Header + string(out)
}

func marshalJSON(images Images) string {
out, err := json.MarshalIndent(images, "", " ")

if err != nil {
puts("error: %v\n", err)
}

return string(out)
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ func main() {

http.HandleFunc("/images/new", handleUpload)
http.HandleFunc("/images/latest", handleLatestImages)
http.HandleFunc("/images/latest.json", handleLatestImagesJSON)

http.Handle("/uploaded_images/",
http.StripPrefix("/uploaded_images/",
Expand Down
2 changes: 2 additions & 0 deletions setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const (

var (
UPLOAD_DIR string
BASE_URL string
SANTA_HAT image.Image
DEBUG bool
)
Expand All @@ -32,5 +33,6 @@ func setup() {
puts("DEBUG:", DEBUG)

UPLOAD_DIR = os.Getenv("TOMTELIZER_UPLOAD_DIR")
BASE_URL = os.Getenv("TOMTELIZER_BASE_URL")
SANTA_HAT = loadImage(os.Getenv("TOMTELIZER_SANTA_HAT"))
}

0 comments on commit 4db2043

Please sign in to comment.