Use struct literal initialization

This commit is contained in:
Las Zenow 2016-05-03 10:11:59 -04:00
parent 0e8f1e7b56
commit baf2b5a6a9

View file

@ -37,34 +37,35 @@ type StatsGatherer struct {
} }
func InitStats(database *database.DB, store *storage.Store, hostname string, template *Template) *StatsGatherer { func InitStats(database *database.DB, store *storage.Store, hostname string, template *Template) *StatsGatherer {
sg := new(StatsGatherer) sg := StatsGatherer{
sg.channel = make(chan statsRequest, statsChanSize) channel: make(chan statsRequest, statsChanSize),
sg.db = database db: database,
sg.store = store store: store,
sg.template = template template: template,
sg.hostname = hostname hostname: hostname,
}
go sg.worker() go sg.worker()
return sg return &sg
} }
func (sg StatsGatherer) Gather(function func(handler)) func(http.ResponseWriter, *http.Request) { func (sg StatsGatherer) Gather(function func(handler)) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
log.Info("Query ", r.Method, " ", r.RequestURI) log.Info("Query ", r.Method, " ", r.RequestURI)
var h handler db := sg.db.Copy()
h.store = sg.store h := handler{
h.db = sg.db.Copy() store: sg.store,
db: db,
template: sg.template,
hostname: sg.hostname,
w: w,
r: r,
sess: GetSession(r, db),
}
defer h.db.Close() defer h.db.Close()
h.template = sg.template
h.hostname = sg.hostname
h.w = w
h.r = r
h.sess = GetSession(r, h.db)
function(h) function(h)
sg.channel <- statsRequest{time.Now(), mux.Vars(r), h.sess, r} sg.channel <- statsRequest{time.Now(), mux.Vars(r), h.sess, r}
} }
} }
@ -94,17 +95,17 @@ func (sg StatsGatherer) worker() {
} }
func statsHandler(h handler) { func statsHandler(h handler) {
var data statsData data := statsData{
data.S = GetStatus(h) S: GetStatus(h),
HVisits: getVisits(hourlyLabel, h.db, database.Hourly_visits),
DVisits: getVisits(dailyLabel, h.db, database.Daily_visits),
MVisits: getVisits(monthlyLabel, h.db, database.Monthly_visits),
HDownloads: getVisits(hourlyLabel, h.db, database.Hourly_downloads),
DDownloads: getVisits(dailyLabel, h.db, database.Daily_downloads),
MDownloads: getVisits(monthlyLabel, h.db, database.Monthly_downloads),
}
data.S.Title = "Stats -- " + data.S.Title data.S.Title = "Stats -- " + data.S.Title
data.S.Stats = true data.S.Stats = true
data.HVisits = getVisits(hourlyLabel, h.db, database.Hourly_visits)
data.DVisits = getVisits(dailyLabel, h.db, database.Daily_visits)
data.MVisits = getVisits(monthlyLabel, h.db, database.Monthly_visits)
data.HDownloads = getVisits(hourlyLabel, h.db, database.Hourly_downloads)
data.DDownloads = getVisits(dailyLabel, h.db, database.Daily_downloads)
data.MDownloads = getVisits(monthlyLabel, h.db, database.Monthly_downloads)
h.template.load(h, "stats", data) h.template.load(h, "stats", data)
} }