Separating server and client for reasons ...

This commit is contained in:
2026-03-18 21:41:58 +00:00
parent 45c394cded
commit 0935788b69
3 changed files with 53 additions and 44 deletions

29
isolated/server/main.go Normal file
View File

@@ -0,0 +1,29 @@
package main
import (
"context"
"flag"
"log"
"os"
"path/filepath"
"git.hrafn.xyz/aether/notes/internal/repository"
"git.hrafn.xyz/aether/notes/internal/store/sqlite"
"git.hrafn.xyz/aether/notes/server"
)
func main() {
databasefile := flag.String("db", filepath.Join(os.Getenv("HOME"), "notes.db"), "Path to the SQLite database file")
dbPath := *databasefile
sqliteStore, err := sqlite.NewSQLiteStore(context.Background(), dbPath)
if err != nil {
panic(err)
}
defer sqliteStore.Close()
repo := repository.NewNoteRepository(sqliteStore)
s := server.GetServer(repo, log.Default())
err = s.Start(":8080")
if err != nil {
panic(err)
}
}

View File

@@ -1,33 +1,25 @@
package main package main
import ( import (
"context"
"flag" "flag"
"log" "log"
"os" "os"
"path/filepath"
"git.hrafn.xyz/aether/notes/client" "git.hrafn.xyz/aether/notes/client"
"git.hrafn.xyz/aether/notes/internal/repository"
"git.hrafn.xyz/aether/notes/internal/store/sqlite"
"git.hrafn.xyz/aether/notes/server"
) )
func main() { func main() {
serverMode := flag.Bool("server", false, "Start the application in server mode")
list := flag.Bool("list", false, "List all notes in the database") list := flag.Bool("list", false, "List all notes in the database")
create := flag.Bool("create", false, "Create a new note") create := flag.Bool("create", false, "Create a new note")
update := flag.Bool("update", false, "Update an existing note") update := flag.Bool("update", false, "Update an existing note")
delete := flag.Bool("delete", false, "Delete a note from the database") delete := flag.Bool("delete", false, "Delete a note from the database")
get := flag.Bool("get", false, "Get a note by ID") get := flag.Bool("get", false, "Get a note by ID")
databasefile := flag.String("db", filepath.Join(os.Getenv("HOME"), "notes.db"), "Path to the SQLite database file (only used in server mode)")
id := flag.Int("id", 0, "ID of the note to get, update, or delete (only used with -get, -update, or -delete)") id := flag.Int("id", 0, "ID of the note to get, update, or delete (only used with -get, -update, or -delete)")
content := flag.String("content", "", "Content of the note to create or update (only used with -create or -update)") content := flag.String("content", "", "Content of the note to create or update (only used with -create or -update)")
flag.Parse() flag.Parse()
modes := map[string]bool{ modes := map[string]bool{
"server": *serverMode,
"list": *list, "list": *list,
"create": *create, "create": *create,
"update": *update, "update": *update,
@@ -42,7 +34,7 @@ func main() {
} }
} }
if enabledCount == 0 { if enabledCount == 0 {
log.Fatal("No mode specified. Use -server, -list, -create, -update, -delete, or -get.") log.Fatal("No mode specified. Use -list, -create, -update, -delete, or -get.")
os.Exit(1) os.Exit(1)
} }
if enabledCount > 1 { if enabledCount > 1 {
@@ -50,21 +42,6 @@ func main() {
os.Exit(1) os.Exit(1)
} }
if *serverMode {
dbPath := *databasefile
sqliteStore, err := sqlite.NewSQLiteStore(context.Background(), dbPath)
if err != nil {
panic(err)
}
defer sqliteStore.Close()
repo := repository.NewNoteRepository(sqliteStore)
s := server.GetServer(repo, log.Default())
err = s.Start(":8080")
if err != nil {
panic(err)
}
}
if *list { if *list {
client.ListNotes() client.ListNotes()
} }

View File

@@ -1,5 +1,7 @@
# Use 'just <command>' to run tasks # Use 'just <command>' to run tasks
build_path := "_build"
server_exec_name := "server"
simplecli_exec_name := "simplecli"
# Default recipe (runs when you just type 'just') # Default recipe (runs when you just type 'just')
default: default:
@@ -7,10 +9,20 @@ default:
build: clean build: clean
@echo "Building notes..." @echo "Building notes..."
@mkdir -p _build @mkdir -p {{ build_path }}
go build -o _build/notes -ldflags="-s -w" . @just build-server
chmod +x ./_build/notes @just build-simplecli
@echo "Build complete: ./_build/notes"
build-server:
@echo "Building the isolated server..."
@rm -f {{ build_path }}/{{ server_exec_name }}
go build -o {{ build_path }}/{{ server_exec_name }} -ldflags="-s -w" isolated/server/main.go
build-simplecli:
@echo "Building the simple cli..."
@rm -f {{ build_path }}/{{ simplecli_exec_name }}
go build -o {{ build_path }}/{{ simplecli_exec_name }} -ldflags="-s -w" isolated/simplecli/main.go
# Run tests with short output # Run tests with short output
test: test:
@@ -21,12 +33,12 @@ test:
# Run tests with coverage # Run tests with coverage
test-coverage: test-coverage:
@echo "Running tests with coverage..." @echo "Running tests with coverage..."
@mkdir -p _build @mkdir -p {{ build_path }}
@rm -f _build/coverage.out @rm -f {{ build_path }}/coverage.out
go test -v -coverprofile=_build/coverage.out ./... go test -v -coverprofile={{ build_path }}/coverage.out ./...
@echo "Generate HTML report" @echo "Generate HTML report"
go tool cover -html=_build/coverage.out -o _build/coverage.html go tool cover -html={{ build_path }}/coverage.out -o {{ build_path }}/coverage.html
@echo "Coverage report generated: _build/coverage.html" @echo "Coverage report generated: {{ build_path }}/coverage.html"
# Watch for changes and run tests automatically # Watch for changes and run tests automatically
watch: watch:
@@ -34,17 +46,8 @@ watch:
@command -v watchexec >/dev/null 2>&1 || (echo "Error: watchexec not installed. Install with: brew install watchexec" && exit 1) @command -v watchexec >/dev/null 2>&1 || (echo "Error: watchexec not installed. Install with: brew install watchexec" && exit 1)
@watchexec --clear --exts go -- sh -c 'echo "Changes detected, running tests..." && just test' @watchexec --clear --exts go -- sh -c 'echo "Changes detected, running tests..." && just test'
# Run the server
run:
@echo "Starting notes server..."
go run . --server
# Run the built binary
run-binary: build
./_build/notes --server
# Clean build artifacts # Clean build artifacts
clean: clean:
@echo "Cleaning build artifacts..." @echo "Cleaning build artifacts..."
rm -rf _build rm -rf {{ build_path }}
@echo "Clean complete" @echo "Clean complete"