func TestList(t *testing.T) {
	var err error
	storage := stats_entry_storage.New("/tmp/stats_test.db", false)
	service := New(storage)
	if err = storage.Truncate(); err != nil {
		t.Fatal(err)
	}
	entries, err := service.List()
	if err != nil {
		t.Fatal(err)
	}
	if err = AssertThat(len(entries), Is(0)); err != nil {
		t.Fatal(err)
	}
	err = storage.CreateEntry(&stats_entry.Entry{Value: 1337})
	if err = AssertThat(err, NilValue()); err != nil {
		t.Fatal(err)
	}
	entries, err = service.List()
	if err != nil {
		t.Fatal(err)
	}
	if err = AssertThat(len(entries), Is(1)); err != nil {
		t.Fatal(err)
	}
}
func readEntriesFromDb(dbPath string) ([]stats_entry.Entry, error) {
	var err error
	var entries []stats_entry.Entry
	dbPath, err = io_util.NormalizePath(dbPath)
	if err != nil {
		return nil, err
	}
	storage := stats_entry_storage.New(dbPath, false)
	if entries, err = storage.FindEntries(); err != nil {
		return nil, err
	}
	return entries, nil
}
Exemple #3
0
func NewHandler(documentRoot string, dbPath string) http.Handler {
	logger.Debugf("root: %s", documentRoot)

	entryStorage := stats_entry_storage.New(dbPath, false)
	entryService := stats_entry_service.New(entryStorage)

	fileServer := cachingheader.NewCachingHeaderHandler(contenttype.NewContentTypeHandler(http.FileServer(http.Dir(documentRoot))))
	handlerFinder := part.New("")
	handlerFinder.RegisterHandler("/", fileServer)
	handlerFinder.RegisterHandler("/css", fileServer)
	handlerFinder.RegisterHandler("/js", fileServer)
	handlerFinder.RegisterHandler("/images", fileServer)
	handlerFinder.RegisterHandlerFinder("/entry", createEntryHandlerFinder("/entry", entryService))
	return log_handler.NewLogHandler(fallback.NewFallback(handlerFinder, static.NewHandlerStaticContentReturnCode("not found", 404)))
}
Exemple #4
0
func do(writer io.Writer, dbPath string, valueString string) error {
	var err error
	dbPath, err = io_util.NormalizePath(dbPath)
	if err != nil {
		return err
	}
	var value int
	if value, err = strconv.Atoi(valueString); err != nil {
		return err
	}
	storage := stats_entry_storage.New(dbPath, false)
	timestamp := time.Now().UnixNano()
	entry := &stats_entry.Entry{Value: value, Timestamp: timestamp}
	return storage.CreateEntry(entry)
}