Example #1
0
func (s *Server) handleImage(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	requestFilename := ps.ByName("filename")[1:]

	if cacheFile, ok := s.Cache.Get(requestFilename); ok {
		// extract url params
		height, _ := strconv.Atoi(r.URL.Query().Get("height"))
		width, _ := strconv.Atoi(r.URL.Query().Get("width"))
		align := r.URL.Query().Get("align")
		if align == "" {
			align = "cc"
		}

		// fetch and render, or, read from disk
		image, err := cacheFile.Image()
		w.Header().Set("Content-Type", cacheFile.FileType())
		w.Header().Set("Expires", time.Now().AddDate(1, 0, 0).Format(http.TimeFormat))
		w.Header().Set("ETag", fmt.Sprintf("%s,w=%d,h=%d,a=%s", image.Hash, width, height, align))
		err = image.Render(w, width, height, align)
		if err != nil {
			log.Printf("Error rendering %s: %s\n", requestFilename, err.Error())
			rest.SetInternalServerErrorResponse(w, err)
			return
		}
		return
	}
	http.Error(w, "File not found", http.StatusNotFound)
}
Example #2
0
func (s *Server) handleReload(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	cache := NewCache(s.Config)
	if err := cache.Setup(); err != nil {
		log.Printf("Error setting up cache: %s\n", err.Error())
		rest.SetInternalServerErrorResponse(w, err)
		return
	}
	s.Cache = cache
	rest.SetNoContentResponse(w)
}
Example #3
0
func (s *Server) handlePanic(w http.ResponseWriter, r *http.Request, err interface{}) {
	rest.SetInternalServerErrorResponse(w, err)
}