Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store database via URL #4

Merged
merged 1 commit into from
Aug 6, 2023
Merged
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
10 changes: 8 additions & 2 deletions websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func ReloadRemoteDatabase(url string) error {
return nil
}

func Shutdown(database Database, filename string) error {
func StoreDatabase(database Database, filename string) error {
err := Dump(database, filename)
if err != nil {
return err
Expand Down Expand Up @@ -103,6 +103,12 @@ func NewContainer(filename string) (Database, *http.ServeMux, error) {
log.Println(err)
}
})
mux.HandleFunc("/store", func(w http.ResponseWriter, r *http.Request) {
err := StoreDatabase(database, filename)
if err != nil {
log.Println(err)
}
})
mux.HandleFunc("/database", DatabaseHandleFunc(database))

return database, mux, nil
Expand All @@ -119,7 +125,7 @@ func main() {
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-signals
err := Shutdown(database, filename)
err := StoreDatabase(database, filename)
if err != nil {
log.Fatalln(err)
}
Expand Down
45 changes: 35 additions & 10 deletions websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,46 @@ func TestWebsocket_Client(t *testing.T) {
assert.Check(t, strings.Contains(string(body), "<title>Database</title>"))
}

func TestWebsocket_Shutdown(t *testing.T) {
// Given database with data
database := NewDatabase()
database.Update("username", "john")
func TestWebsocket_Store(t *testing.T) {
// Given empty database server
filename := "test.txt"
_, mux, err := NewContainer(filename)
assert.NilError(t, err)
server := httptest.NewServer(mux)
ws, _, err := websocket.DefaultDialer.Dial(strings.Replace(server.URL, "http", "ws", 1)+"/database", nil)
assert.NilError(t, err)

// When we add value
err = ws.WriteMessage(websocket.TextMessage, []byte("update username john"))
assert.NilError(t, err)

// Then value is there
err = ws.WriteMessage(websocket.TextMessage, []byte("select username"))
assert.NilError(t, err)
_, message, err := ws.ReadMessage()
assert.NilError(t, err)
assert.Equal(t, string(message), "john")

// When we shut it down
err := Shutdown(database, filename)
// When we store dump
request, err := http.NewRequest("GET", server.URL+"/store", nil)
assert.NilError(t, err)
client := &http.Client{}
_, err = client.Do(request)
assert.NilError(t, err)

// Then state is saved
database = NewDatabase()
err = Load(database, filename)
// And when we load it again
_, mux, err = NewContainer("test.txt")
assert.NilError(t, err)
server = httptest.NewServer(mux)
ws, _, err = websocket.DefaultDialer.Dial(strings.Replace(server.URL, "http", "ws", 1)+"/database", nil)
assert.NilError(t, err)
assert.DeepEqual(t, database.Select("username"), []string{"john"})

// Then value is there
err = ws.WriteMessage(websocket.TextMessage, []byte("select username"))
assert.NilError(t, err)
_, message, err = ws.ReadMessage()
assert.NilError(t, err)
assert.Equal(t, string(message), "john")

// Clean up
err = os.Remove(filename)
Expand Down