diff --git a/cmd/main.go b/cmd/main.go index efec73d..99465d2 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -3,6 +3,7 @@ package main import ( "github.com/KristianJBorgwarth/dendrite.daemon/core/http" "github.com/KristianJBorgwarth/dendrite.daemon/core/logging" + _ "modernc.org/sqlite" ) func main() { diff --git a/core/handlers/init.go b/core/handlers/init.go index d5384f5..b8fb4b7 100644 --- a/core/handlers/init.go +++ b/core/handlers/init.go @@ -2,6 +2,8 @@ package handlers import ( "encoding/json" + "log/slog" + "github.com/KristianJBorgwarth/dendrite.daemon/config" "github.com/KristianJBorgwarth/dendrite.daemon/persistence" ) @@ -25,6 +27,7 @@ type initializeCommand struct { func Initialize(raw json.RawMessage) (*config.Config, error) { var params initializeCommand + slog.Info("initializing with params", "params", string(raw)) if err := json.Unmarshal(raw, ¶ms); err != nil { return nil, err } diff --git a/core/http/server.go b/core/http/server.go index 17cb995..f240a9a 100644 --- a/core/http/server.go +++ b/core/http/server.go @@ -27,6 +27,10 @@ func (s *Server) Start() error { mux := http.NewServeMux() mux.HandleFunc("/rpc", s.RPCHandler) + mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + w.Write([]byte("ok")) + }) return http.ListenAndServe(s.addr, mux) } diff --git a/dendrite b/dendrite index 83ad510..6782f21 100755 Binary files a/dendrite and b/dendrite differ diff --git a/persistence/db_migrator.go b/persistence/db_migrator.go index 87541af..c17d0a6 100644 --- a/persistence/db_migrator.go +++ b/persistence/db_migrator.go @@ -1,39 +1,46 @@ -// Package persistence handles database schema migrations package persistence import ( "database/sql" + "embed" "log/slog" - "os" "path/filepath" "sort" ) -func InitializeIndex(vaulPath string) error { - dbPath := filepath.Join(vaulPath, "index.db") +//go:embed migrations/*.sql +var migrationsFS embed.FS + +func InitializeIndex(vaultPath string) error { + dbPath := filepath.Join(vaultPath, "index.db") db, err := sql.Open("sqlite", dbPath) if err != nil { + slog.Error("failed to open database", "error", err) return err } + defer db.Close() - migrationsDir := filepath.Join("persistence", "migrations") - - if err := applyMigrations(db, migrationsDir); err != nil { + if err := applyMigrations(db); err != nil { slog.Error("failed to apply migrations", "error", err) return err } + return nil } -func applyMigrations(db *sql.DB, dir string) error { - _, err := db.Exec(`CREATE TABLE IF NOT EXISTS schema_migrations (version TEXT PRIMARY KEY);`) +func applyMigrations(db *sql.DB) error { + _, err := db.Exec(` + CREATE TABLE IF NOT EXISTS schema_migrations ( + version TEXT PRIMARY KEY + ); + `) if err != nil { return err } - entries, err := os.ReadDir(dir) + entries, err := migrationsFS.ReadDir("migrations") if err != nil { return err } @@ -43,7 +50,7 @@ func applyMigrations(db *sql.DB, dir string) error { }) for _, e := range entries { - if e.IsDir(){ + if e.IsDir() { continue } @@ -58,9 +65,7 @@ func applyMigrations(db *sql.DB, dir string) error { continue } - path := filepath.Join(dir, name) - - sqlBytes, err := os.ReadFile(path) + sqlBytes, err := migrationsFS.ReadFile("migrations/" + name) if err != nil { return err } @@ -69,9 +74,14 @@ func applyMigrations(db *sql.DB, dir string) error { return err } - if _, err := db.Exec(`INSERT INTO schema_migrations (version) VALUES (?)`, name); err != nil { + if _, err := db.Exec( + `INSERT INTO schema_migrations (version) VALUES (?)`, + name, + ); err != nil { return err } + + slog.Info("applied migration", "file", name) } return nil @@ -79,7 +89,11 @@ func applyMigrations(db *sql.DB, dir string) error { func isMigrationApplied(db *sql.DB, version string) (bool, error) { var v string - err := db.QueryRow(`SELECT version FROM schema_migrations WHERE version= ?`, version).Scan(&v) + + err := db.QueryRow( + `SELECT version FROM schema_migrations WHERE version = ?`, + version, + ).Scan(&v) if err == sql.ErrNoRows { return false, nil diff --git a/persistence/doc.go b/persistence/doc.go new file mode 100644 index 0000000..0276d0a --- /dev/null +++ b/persistence/doc.go @@ -0,0 +1,2 @@ +// Package persistence provides a simple interface for saving and loading data to and from a file. +package persistence