diff --git a/main.go b/main.go index 2bfaad8..0496b63 100644 --- a/main.go +++ b/main.go @@ -4,13 +4,12 @@ import ( "encoding/json" "flag" "html/template" - "io" "log" "math/rand" "net/http" "os" - "path/filepath" "strconv" + "strings" "time" "github.com/sirupsen/logrus" @@ -21,6 +20,49 @@ var images []string var logger = logrus.New() var port string +// Okay I admit, this is bad. Just a workaround for now, until I figure out a clean +// way of displaying all images in a grid. +var tmpl = template.Must(template.New("index").Parse(` + + + + + + Image Gallery + + + +

Image Gallery

+ + +`)) + func init() { // Log as JSON instead of the default ASCII formatter logger.SetFormatter(&logrus.JSONFormatter{}) @@ -40,13 +82,9 @@ func main() { log.Fatalf("Error reading configuration file: %v", err) } - port := viper.GetString("server.port") + port = viper.GetString("server.port") + flag.Parse() - - if err := viper.ReadInConfig(); err != nil { - log.Fatalf("Error reading configuration file: %v", err) - } - images = getImages() // Add request logging middleware @@ -84,26 +122,20 @@ func getImages() []string { return images } -func sanitizeInput(input string) string { - return template.HTMLEscapeString(input) -} - func homeHandler(w http.ResponseWriter, r *http.Request) { - io.WriteString(w, "
") - for i := range images { - io.WriteString(w, ``) - io.WriteString(w, ``) - io.WriteString(w, ``) - } - io.WriteString(w, "
") + w.Header().Set("Content-Type", "text/html") + tmpl.Execute(w, struct { + Images []string + }{Images: images}) } func idHandler(w http.ResponseWriter, r *http.Request) { - id := sanitizeInput(r.URL.Query().Get("id")) + id := r.URL.Query().Get("id") if id == "" { http.Error(w, "Missing id", http.StatusBadRequest) return } + i, err := strconv.Atoi(id) if err != nil || i < 0 || i >= len(images) { http.Error(w, "Invalid id", http.StatusBadRequest) @@ -120,7 +152,7 @@ func idHandler(w http.ResponseWriter, r *http.Request) { } func isValidImagePath(path string) bool { - if !filepath.HasPrefix(path, "images/") { + if !strings.HasPrefix(path, "images/") { return false } return true @@ -135,11 +167,13 @@ func listHandler(w http.ResponseWriter, r *http.Request) { } imageList = append(imageList, imageInfo) } + jsonData, err := json.Marshal(imageList) if err != nil { http.Error(w, "Internal Server Error", http.StatusInternalServerError) return } + w.Header().Set("Content-Type", "application/json") w.Write(jsonData) } @@ -155,7 +189,9 @@ func logRequest(next http.Handler) http.Handler { } func randomHandler(w http.ResponseWriter, r *http.Request) { - rand.Seed(time.Now().UnixNano()) + source := rand.NewSource(time.Now().UnixNano()) + rand.New(source) + i := rand.Intn(len(images)) http.Redirect(w, r, "/api/id?id="+strconv.Itoa(i), http.StatusSeeOther) }