// Monit monitors the database and file system to remove old entries func Monit() { logger.Info("monitoring", "Started Monitoring") var err error tc := time.NewTicker(1 * time.Minute) for { logger.Debug("monitoring", "Started Monit on Resources") now := time.Now() found := 0 var sizeRemoved uint64 err = database.DB.Update(func(tx *bolt.Tx) error { b := tx.Bucket([]byte("resources")) err = b.ForEach(func(k, v []byte) error { var err error r := &models.Resource{} if err = r.Decode(v); err != nil { return err } if r.DeleteAt.Before(now) { if err = b.Delete([]byte(r.Key)); err != nil { logger.Err("monitoring", "While deleting entry (skipped)", err) } else { if err = os.Remove(path.Join(conf.C.UploadDir, r.Key)); err != nil { logger.Err("monitoring", "While deleting file (skipped)", err) } else { found++ sizeRemoved += uint64(r.Size) } } } return nil }) if err != nil { return err } if found > 0 { var data []byte models.S.CurrentFiles -= uint64(found) models.S.CurrentSize -= sizeRemoved if data, err = models.S.Encode(); err != nil { return err } return tx.Bucket([]byte("statistics")).Put([]byte("main"), data) } return nil }) if err != nil { logger.Err("monitoring", "While monitoring", err) } else { if found > 0 { logger.Info("monitoring", fmt.Sprintf("Deleted %d entries and files in %s", found, time.Since(now))) logger.Info("monitoring", fmt.Sprintf("Serving %d (%s) files", models.S.CurrentFiles, utils.HumanBytes(models.S.CurrentSize))) } logger.Debug("monitoring", fmt.Sprintf("Serving %d (%s) files", models.S.CurrentFiles, utils.HumanBytes(models.S.CurrentSize))) } logger.Debug("monitoring", fmt.Sprintf("Done Monit on Resources (%s)", time.Since(now))) <-tc.C } }
// Initialize loads the previous state of the statistics func Initialize() error { logger.Debug("server", "Started Initialize on statistics object") var cfiles uint64 var csize uint64 var err error err = database.DB.View(func(tx *bolt.Tx) error { S.Decode(tx.Bucket([]byte("statistics")).Get([]byte("main"))) return tx.Bucket([]byte("resources")).ForEach(func(k, v []byte) error { r := &Resource{} if err = r.Decode(v); err != nil { return err } csize += uint64(r.Size) cfiles++ return nil }) }) if err != nil { logger.Err("server", "Could not initialize statistics") return err } S.CurrentFiles = cfiles S.CurrentSize = csize logger.Info("server", fmt.Sprintf("Total %d (%s)", S.TotalFiles, utils.HumanBytes(S.TotalSize))) logger.Info("server", fmt.Sprintf("Current %d (%s)", cfiles, utils.HumanBytes(csize))) logger.Debug("server", "Done Initialize on statistics object") return err }
func main() { var err error var cp string var initial bool var r *gin.Engine tbox, _ := rice.FindBox("templates") abox, _ := rice.FindBox("assets") flag.StringVarP(&cp, "conf", "c", "conf.yml", "Local path to configuration file.") flag.BoolVarP(&initial, "initial", "i", false, "Run the initial setup of the server.") flag.Parse() if err = conf.Load(cp, !initial); err != nil || initial { setup.Run() } if err = database.Initialize(); err != nil { log.Fatal(err) } defer database.DB.Close() if err = models.Initialize(); err != nil { log.Fatal(err) } go monitoring.Monit() if r, err = router.Setup(tbox, abox); err != nil { log.Fatal(err) } logger.Info("server", "Started goploader server on port", conf.C.Port) if conf.C.ServeHTTPS { if err = http.ListenAndServeTLS(fmt.Sprintf(":%d", conf.C.Port), conf.C.SSLCert, conf.C.SSLPrivKey, r); err != nil { logger.Err("server", "Fatal error", err) } } else { if err = r.Run(fmt.Sprintf(":%d", conf.C.Port)); err != nil { logger.Err("server", "Fatal error", err) } } }