Example #1
0
// clean removes old cache files from cache root.
func clean() (err error) {
	// Get a list of all pages.
	pages, err := ini.ReadPages(settings.PagesPath)
	if err != nil {
		return errutil.Err(err)
	}

	// Get a list of all cached pages.
	caches, err := ioutil.ReadDir(settings.CacheRoot)
	if err != nil {
		return errutil.Err(err)
	}

	for _, cache := range caches {
		remove := true
		for _, p := range pages {
			pageName, err := filename.Encode(p.UrlAsFilename())
			if err != nil {
				return errutil.Err(err)
			}
			if cache.Name() == pageName+".htm" {
				remove = false
				break
			}
		}
		if remove {
			err = os.Remove(settings.CacheRoot + cache.Name())
			if err != nil {
				return err
			}
		}
	}

	return nil
}
Example #2
0
// Wait for input and send output to client.
func takeInput(conn net.Conn) (err error) {
	for {
		query, err := bufioutil.NewReader(conn).ReadLine()
		if err != nil {
			if err.Error() == "EOF" {
				break
			}
			return errutil.Err(err)
		}

		// Do something with the query
		switch query {
		case settings.QueryUpdates:
			// Encode (send) the value.
			err = gob.NewEncoder(conn).Encode(settings.Updates)
		case settings.QueryClearAll:
			settings.Updates = make(map[string]bool)
			err = settings.SaveUpdates()
		case settings.QueryForceRecheck:
			pages, err := ini.ReadPages(settings.PagesPath)
			if err != nil {
				return errutil.Err(err)
			}
			err = page.ForceUpdate(pages)
		}
		if err != nil {
			return errutil.Err(err)
		}
	}
	return nil
}
Example #3
0
func watchConfig(watcher *fsnotify.Watcher) (err error) {
	for {
		select {
		case ev := <-watcher.Event:
			if ev != nil {
				if ev.Name == settings.ConfigPath {
					// Read settings from config file.
					err = ini.ReadSettings(settings.ConfigPath)
					if err != nil {
						return errutil.Err(err)
					}
				}
				// Retrieve an array of pages from INI file.
				pages, err := ini.ReadPages(settings.PagesPath)
				if err != nil {
					return errutil.Err(err)
				}
				err = page.ForceUpdate(pages)
				if err != nil {
					return errutil.Err(err)
				}
			}
		case err = <-watcher.Error:
			if err != nil {
				return errutil.Err(err)
			}
		}
	}
}