Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 10 additions & 39 deletions handlers/paste_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,11 @@ import (
"testing"

"github.com/gorilla/mux"
"github.com/mtlynch/logpaste/store"
"github.com/mtlynch/logpaste/store/test_sqlite"
)

type mockStore struct {
entries map[string]string
}

func (ds mockStore) GetEntry(id string) (string, error) {
if contents, ok := ds.entries[id]; ok {
return contents, nil
}
return "", store.EntryNotFoundError{ID: id}
}

func (ds *mockStore) InsertEntry(id string, contents string) error {
ds.entries[id] = contents
return nil
}

func (ds *mockStore) Reset() {
ds.entries = make(map[string]string)
}

func TestPasteGet(t *testing.T) {
ds := mockStore{
entries: map[string]string{
"12345678": "dummy entry",
},
}
ds := test_sqlite.New()
router := mux.NewRouter()
s := defaultServer{
store: &ds,
Expand Down Expand Up @@ -124,9 +100,7 @@ func TestPastePut(t *testing.T) {
},
} {
t.Run(tt.description, func(t *testing.T) {
ds := mockStore{
entries: make(map[string]string),
}
ds := test_sqlite.New()
router := mux.NewRouter()
s := defaultServer{
store: &ds,
Expand Down Expand Up @@ -167,15 +141,6 @@ func TestPastePut(t *testing.T) {
}

func TestPastePost(t *testing.T) {
ds := mockStore{
entries: make(map[string]string),
}
router := mux.NewRouter()
s := defaultServer{
store: &ds,
router: router,
}
s.routes()
for _, tt := range []struct {
description string
contentType string
Expand Down Expand Up @@ -251,7 +216,13 @@ some data in a file
},
} {
t.Run(tt.description, func(t *testing.T) {
ds.Reset()
ds := test_sqlite.New()
router := mux.NewRouter()
s := defaultServer{
store: &ds,
router: router,
}
s.routes()

req, err := http.NewRequest("POST", "/",
strings.NewReader(strings.ReplaceAll(tt.body, "\n", "\r\n")))
Expand Down
2 changes: 1 addition & 1 deletion handlers/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Server interface {
func New(sp SiteProperties, perMinuteLimit int) Server {
s := defaultServer{
router: mux.NewRouter(),
store: sqlite.New(),
store: sqlite.New("data/store.db"),
siteProps: sp,
ipRateLimiter: limit.New(perMinuteLimit),
}
Expand Down
8 changes: 4 additions & 4 deletions store/sqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"database/sql"
"log"
"os"
"path/filepath"
"time"

_ "github.com/mattn/go-sqlite3"
Expand All @@ -15,10 +16,9 @@ type db struct {
ctx *sql.DB
}

func New() store.Store {
dbDir := "data"
ensureDirExists(dbDir)
ctx, err := sql.Open("sqlite3", dbDir+"/store.db")
func New(path string) store.Store {
ensureDirExists(filepath.Dir(path))
ctx, err := sql.Open("sqlite3", path)
if err != nil {
log.Fatalln(err)
}
Expand Down
18 changes: 18 additions & 0 deletions store/test_sqlite/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package test_sqlite

import (
"fmt"

"github.com/mtlynch/logpaste/random"
"github.com/mtlynch/logpaste/store"
"github.com/mtlynch/logpaste/store/sqlite"
)

func New() store.Store {
return sqlite.New(ephemeralDbURI())
}

func ephemeralDbURI() string {
name := random.String(10)
return fmt.Sprintf("file:%s?mode=memory&cache=shared", name)
}