예제 #1
0
파일: handlers.go 프로젝트: delfvad/hi
// middleware sets json and cache control headers, adds logging.
func middleware(next http.HandlerFunc) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json; charset=utf-8")
		w.Header().Set("Cache-Control", "public, max-age=60")

		report.JSON(os.Stdout, next).ServeHTTP(w, r)
	}
}
예제 #2
0
파일: main.go 프로젝트: soundcloud/ent
func main() {
	var (
		fsRoot      = flag.String("fs.root", "/tmp", "FileSystem root directory")
		httpAddress = flag.String("http.addr", ":5555", "HTTP listen address")
		providerDir = flag.String("provider.dir", "/tmp", "Provider directory with bucket policies")
	)
	flag.Parse()

	prometheus.MustRegister(requestDurations)
	prometheus.MustRegister(requestBytes)
	prometheus.MustRegister(responseBytes)

	var (
		fs = newDiskFS(*fsRoot)
		r  = pat.New()
	)

	p, err := newDiskProvider(*providerDir)
	if err != nil {
		log.Fatal(err)
	}

	// GET /metrics
	r.Handle("/metrics", prometheus.Handler())

	// DELETE /$bucket/$file
	r.Add(
		"DELETE",
		ent.RouteFile,
		report.JSON(
			os.Stdout,
			metrics(
				"handleDelete",
				handleDelete(p, fs),
			),
		),
	)
	// GET /$bucket/$file
	r.Add(
		"GET",
		ent.RouteFile,
		report.JSON(
			os.Stdout,
			metrics(
				"handleGet",
				addCORSHeaders(
					handleGet(p, fs),
				),
			),
		),
	)
	// HEAD /$bucket/$file
	r.Add(
		"HEAD",
		ent.RouteFile,
		report.JSON(
			os.Stdout,
			metrics(
				"handleExists",
				handleExists(p, fs),
			),
		),
	)
	// POST /$bucket/$file
	r.Add(
		"POST",
		ent.RouteFile,
		report.JSON(
			os.Stdout,
			metrics(
				"handleCreate",
				addCORSHeaders(
					handleCreate(p, fs),
				),
			),
		),
	)

	// GET /$bucket
	r.Add(
		"GET",
		ent.RouteBucket,
		report.JSON(
			os.Stdout,
			metrics(
				"handleFileList",
				addCORSHeaders(
					handleFileList(p, fs),
				),
			),
		),
	)

	// GET /
	r.Add(
		"GET",
		"/",
		report.JSON(
			os.Stdout,
			metrics(
				"handleBucketList",
				addCORSHeaders(
					handleBucketList(p),
				),
			),
		),
	)

	r.Add(
		"OPTIONS",
		"/{.*}",
		report.JSON(
			os.Stdout,
			metrics(
				"handleOptions",
				addCORSHeaders(
					handleOptions(),
				),
			),
		),
	)

	log.Printf("ent %s listening on %s", Version, *httpAddress)
	log.Fatal(http.ListenAndServe(*httpAddress, http.Handler(r)))
}
예제 #3
0
파일: handlers.go 프로젝트: surma-dump/rp
func (h handyReport) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	report.JSON(os.Stdout, h.next).ServeHTTP(w, r)
}