func rootHandler(w traffic.ResponseWriter, r *traffic.Request) { response := map[string]string{ "version": VERSION, } w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(response) }
func ImageHandler(w traffic.ResponseWriter, r *traffic.Request) { // output the image with the correct content-type w.Header().Set("Content-Type", "image/jpeg") // at this point we can safely assume that the image file already exists if image_data, err := ioutil.ReadFile(w.GetVar("filename").(string)); err != nil { panic(err) } else { w.Write(image_data) } }
func codesHandler(w traffic.ResponseWriter, r *traffic.Request) { code := r.Param("code") url := fmt.Sprintf("%s%s", baseUrl, code) grid, err := qrencode.Encode(url, qrencode.ECLevelQ) if err != nil { panic(err) } w.Header().Set("Content-Type", "image/png") png.Encode(w, grid.Image(8)) }
func SetDefaultHeaders(w traffic.ResponseWriter, r *traffic.Request) { w.Header().Set("Cerebellum-Version", VERSION) w.Header().Set("Content-Type", "application/json; charset=utf-8") }
func PoweredByHandler(w traffic.ResponseWriter, r *traffic.Request) { w.Header().Set("X-Powered-By", "Grumpy cat") }
func addTimeHeader(w traffic.ResponseWriter, r *traffic.Request) { t := fmt.Sprintf("%s", time.Now()) w.Header().Add("X-APP-TIME", t) }
func addAppNameHeader(w traffic.ResponseWriter, r *traffic.Request) { w.Header().Add("X-APP-NAME", "My App") }
func sendJpeg(w traffic.ResponseWriter, data []byte) { w.Header().Set("Content-Type", "image/jpeg") w.Header().Set("Content-Length", strconv.Itoa(len(data))) w.Write(data) }
func addLocationHeader(w traffic.ResponseWriter, r *traffic.Request) { t := fmt.Sprintf("tel://%s", w.GetVar("phone")) w.Header().Add("Location", t) w.WriteHeader(http.StatusTemporaryRedirect) }