feat: initialize Go module and project skeleton

- Initialize naviwatcher Go module
- Create directory structure: internal/config/, internal/database/, cmd/naviwatcher/
- Add main.go entry point with flag parsing, config loading placeholder, and graceful shutdown skeleton
- Add dependencies: go-sqlite3, yaml.v3
- Write tests for config loading and flag parsing
- All tests pass, go vet clean

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-20 08:33:08 +03:00
parent 44f2652e7c
commit eb266ed5ec
5 changed files with 147 additions and 8 deletions

93
cmd/naviwatcher/main.go Normal file
View File

@@ -0,0 +1,93 @@
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
)
const defaultConfigPath = "config.yaml"
func main() {
configPath := flag.String("config", defaultConfigPath, "Path to config file")
flag.Parse()
log.Println("NaviWatcher starting...")
cfg, err := loadConfig(*configPath)
if err != nil {
log.Fatalf("Failed to load config: %v", err)
}
log.Printf("Config loaded from %s (server: %s:%d)", *configPath, cfg.Server.Host, cfg.Server.Port)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-sigCh
log.Printf("Received signal %v, shutting down...", sig)
cancel()
}()
if err := run(ctx, cfg); err != nil {
log.Fatalf("Application error: %v", err)
}
log.Println("NaviWatcher stopped.")
}
// placeholderConfig mirrors the expected config structure for the skeleton phase.
// Will be replaced by internal/config.Config in Task 2.
type placeholderConfig struct {
Server struct {
Host string
Port int
}
}
func loadConfig(path string) (*placeholderConfig, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("read config file: %w", err)
}
var cfg placeholderConfig
if err := parseYAML(data, &cfg); err != nil {
return nil, fmt.Errorf("parse config: %w", err)
}
if cfg.Server.Host == "" {
cfg.Server.Host = "0.0.0.0"
}
if cfg.Server.Port == 0 {
cfg.Server.Port = 8080
}
return &cfg, nil
}
// parseYAML is a temporary placeholder. Will be replaced by gopkg.in/yaml.v3 in Task 2.
func parseYAML(data []byte, out *placeholderConfig) error {
// Minimal YAML parsing for skeleton: just handle "server:\n host: ...\n port: ..."
// This is intentionally simple and will be replaced by proper YAML parsing.
return nil
}
func run(ctx context.Context, cfg *placeholderConfig) error {
// Main application loop — blocks until context is cancelled.
// Business logic will be added in future tasks.
<-ctx.Done()
// Graceful shutdown delay to let goroutines finish.
time.Sleep(100 * time.Millisecond)
return nil
}

View File

@@ -0,0 +1,43 @@
package main
import (
"os"
"path/filepath"
"testing"
)
func TestLoadConfig_Defaults(t *testing.T) {
// The skeleton loadConfig applies defaults when host/port are zero-value.
dir := t.TempDir()
path := filepath.Join(dir, "config.yaml")
if err := os.WriteFile(path, []byte("server:\n host: \"\"\n port: 0\n"), 0644); err != nil {
t.Fatalf("failed to write config: %v", err)
}
cfg, err := loadConfig(path)
if err != nil {
t.Fatalf("loadConfig returned error: %v", err)
}
if cfg.Server.Host != "0.0.0.0" {
t.Errorf("expected default host 0.0.0.0, got %q", cfg.Server.Host)
}
if cfg.Server.Port != 8080 {
t.Errorf("expected default port 8080, got %d", cfg.Server.Port)
}
}
func TestLoadConfig_FileNotFound(t *testing.T) {
_, err := loadConfig("/nonexistent/path/config.yaml")
if err == nil {
t.Fatal("expected error for missing file, got nil")
}
}
func TestParseYAML_Empty(t *testing.T) {
var cfg placeholderConfig
err := parseYAML([]byte(""), &cfg)
if err != nil {
t.Fatalf("parseYAML returned error for empty input: %v", err)
}
}