示例#1
0
func main() {
	middleware := stats.New()

	mux := http.NewServeMux()

	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json")
		w.Write([]byte("{\"hello\": \"world\"}"))
	})

	mux.HandleFunc("/stats", func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json")

		stats := middleware.Data()

		b, _ := json.Marshal(stats)

		w.Write(b)
	})

	n := negroni.Classic()
	n.Use(middleware)
	n.UseHandler(mux)
	n.Run(":3000")
}
示例#2
0
func main() {
	middleware := stats.New()

	m := martini.Classic()
	m.Get("/", func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json")
		w.Write([]byte("{\"hello\": \"world\"}"))
	})
	m.Get("/stats", func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json")

		stats := middleware.Data()

		b, _ := json.Marshal(stats)

		w.Write(b)
	})

	m.Use(func(c martini.Context, w http.ResponseWriter, r *http.Request) {
		beginning, recorder := middleware.Begin(w)

		c.Next()

		middleware.End(beginning, recorder)
	})
	m.Run()
}
示例#3
0
func main() {
	router := httprouter.New()
	s := stats.New()
	router.GET("/stats", func(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
		w.Header().Set("Content-Type", "application/json; charset=utf-8")
		s, err := json.Marshal(s.Data())
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
		}
		w.Write(s)
	})
	http.ListenAndServe(":8080", s.Handler(router))
}
示例#4
0
func serve(c *cli.Context) {
	port := c.GlobalString("port")

	fullPath, err := filepath.Abs(c.GlobalString("path"))
	if err != nil {
		log.Fatalf("Error setting path: %v", err)
	}

	logger := log.New(os.Stderr, "", log.Flags())
	stat := stats.New()
	mux := http.DefaultServeMux

	mux.HandleFunc("/stats", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json")

		bytes, err := json.Marshal(stat.Data())
		if err != nil {
			http.Error(w, err.Error(), 500)
			return
		}

		w.Write(bytes)
	}))

	http.Handle("/autoindexassets/",
		stat.Handler(
			http.StripPrefix("/autoindexassets/",
				http.FileServer(
					&assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, Prefix: "autoindexassets"}))))

	http.HandleFunc("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		stat.ServeHTTP(w, r, handlers.Autodir(fullPath))
	}))

	server := &http.Server{
		Addr:    ":" + port,
		Handler: handlers.NewApacheLoggingHandler(mux, logger),
	}

	log.Printf("Listening on %v", port)
	server.ListenAndServe()
}
示例#5
0
func main() {
	middleware := stats.New()

	goji.Get("/", func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json")
		w.Write([]byte("{\"hello\": \"world\"}"))
	})
	goji.Get("/stats", func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json")

		stats := middleware.Data()

		b, _ := json.Marshal(stats)

		w.Write(b)
	})

	goji.Use(middleware.Handler)
	goji.Serve()
}
示例#6
0
package main

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context"
	"github.com/krisrang/fancypants/Godeps/_workspace/src/github.com/thoas/stats"
	"time"
)

var Stats = stats.New()

type StatsController struct {
	beego.Controller
}

func (this *StatsController) Get() {
	this.Data["json"] = Stats.Data()
	this.Ctx.Output.SetStatus(200)
	this.ServeJson()
}

func main() {
	beego.InsertFilter("*", beego.BeforeRouter, func(ctx *context.Context) {
		startTime := time.Now()
		ctx.Input.SetData("stats_timer", startTime)
	})
	beego.InsertFilter("*", beego.FinishRouter, func(ctx *context.Context) {
		Stats.EndWithStatus(ctx.Input.GetData("stats_timer").(time.Time), ctx.Output.Status)
	})

	beego.Router("/stats", &StatsController{})