Documentation ¶
Overview ¶
Package gstats collects HTTP server statistics and and shows them inside a web page.
Example ¶
package main import ( "github.com/cevatbarisyilmaz/gstats" "log" "net" "net/http" ) func main() { gs := gstats.New("gstats") listener, err := net.Listen("tcp", "127.0.0.1:80") if err != nil { log.Fatal(err) } glistener := gs.Listener(listener) http.Handle("/gstats/", gs.Collect(http.StripPrefix("/gstats", gs.Show()))) http.Handle("/", gs.Collect(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { writer.Write([]byte("You are visiting " + request.URL.Path)) }))) err = http.Serve(glistener, nil) gs.PrepareToExit() log.Fatal(err) }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type GStats ¶
type GStats struct { // Country function is used for geolocation. // If not overridden, it will use https://github.com/cevatbarisyilmaz/ip2country // which is licensed under Creative Commons Attribution Share Alike 4.0 International (CC-BY-SA-4.0) // as it uses GeoLite2 database created by MaxMind (which is also licensed with CC-BY-SA-4.0). // Therefore, with the default value, this package falls under CC-BY-SA-4.0 License. // However, if you decide to use another source for geolocation by overriding this attribute, // you can use this package under MIT License. Country func(net.IP) (string, error) // contains filtered or unexported fields }
func (*GStats) Collect ¶
Collect wraps the given handler and returns another handler which will track HTTP-related statistics.
func (*GStats) Listener ¶
Listener wraps the given listener and returns another listener which will track hosts, connections and bandwidth.
func (*GStats) PrepareToExit ¶
func (g *GStats) PrepareToExit()
PrepareToExit saves any unsaved data to disk to not lose them on exiting the program. Normally, GStats does disk saves at the beginning of each hour.
func (*GStats) Show ¶
Show returns a handler which serves the web page to browse collected statistics. At public-facing usages, it is advised to authenticate the users first before calling this handler. If handler lies at a non-root path, any prefixes should be removed before invoking the handler. i.e. http.Handle("/statistics/", http.StripPrefix("/statistics", gs.Show()))