From 44d805fbfebfb32acee0a43835f76425bf236da6 Mon Sep 17 00:00:00 2001 From: Mathieu Aumont Date: Tue, 11 Nov 2025 16:07:29 +0100 Subject: [PATCH] fix: Correct .gitignore to track cmd/server/main.go The pattern 'server' was too broad and ignored the cmd/server/ directory. Changed to '/server' to only ignore the binary at root level. This fixes the missing main.go file in the repository. --- .gitignore | 6 +-- cmd/server/main.go | 123 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+), 3 deletions(-) create mode 100644 cmd/server/main.go diff --git a/.gitignore b/.gitignore index e9cf949..561a1e5 100644 --- a/.gitignore +++ b/.gitignore @@ -5,9 +5,9 @@ *.so *.dylib -# Go build output -server -project-notes +# Go build output (binaries only, not source directories) +/server +/project-notes cmd/server/server # Test binary, built with `go test -c` diff --git a/cmd/server/main.go b/cmd/server/main.go new file mode 100644 index 0000000..6c867bc --- /dev/null +++ b/cmd/server/main.go @@ -0,0 +1,123 @@ +package main + +import ( + "context" + "errors" + "flag" + "fmt" + "html/template" + "log" + "net/http" + "os" + "os/signal" + "syscall" + "time" + + "github.com/mathieu/project-notes/internal/api" + "github.com/mathieu/project-notes/internal/indexer" + "github.com/mathieu/project-notes/internal/watcher" +) + +func main() { + addr := flag.String("addr", ":8080", "Adresse d ecoute HTTP") + notesDir := flag.String("notes-dir", "./notes", "Repertoire contenant les notes Markdown") + flag.Parse() + + logger := log.New(os.Stdout, "[server] ", log.LstdFlags) + + ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) + defer stop() + + if err := ensureDir(*notesDir); err != nil { + logger.Fatalf("repertoire notes invalide: %v", err) + } + + idx := indexer.New() + if err := idx.Load(*notesDir); err != nil { + logger.Fatalf("echec de l indexation initiale: %v", err) + } + + w, err := watcher.Start(ctx, *notesDir, idx, logger) + if err != nil { + logger.Fatalf("echec du watcher: %v", err) + } + defer w.Close() + + // Pre-parse templates + templates, err := template.ParseGlob("templates/*.html") + if err != nil { + logger.Fatalf("echec de l analyse des templates: %v", err) + } + + mux := http.NewServeMux() + + // Servir les fichiers statiques + mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static")))) + mux.Handle("/frontend/", http.StripPrefix("/frontend/", http.FileServer(http.Dir("./frontend")))) + + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/" { + http.NotFound(w, r) + return + } + data := map[string]interface{}{ + "Now": time.Now(), + } + if err := templates.ExecuteTemplate(w, "index.html", data); err != nil { + logger.Printf("erreur d execution du template index: %v", err) + http.Error(w, "erreur interne", http.StatusInternalServerError) + } + }) + + apiHandler := api.NewHandler(*notesDir, idx, templates, logger) + mux.Handle("/api/v1/notes", apiHandler) // REST API v1 + mux.Handle("/api/v1/notes/", apiHandler) // REST API v1 + mux.Handle("/api/search", apiHandler) + mux.Handle("/api/folders/create", apiHandler) + mux.Handle("/api/files/move", apiHandler) + mux.Handle("/api/files/delete-multiple", apiHandler) + mux.Handle("/api/home", apiHandler) + mux.Handle("/api/about", apiHandler) // About page + mux.Handle("/api/daily", apiHandler) // Daily notes + mux.Handle("/api/daily/", apiHandler) // Daily notes + mux.Handle("/api/favorites", apiHandler) // Favorites + mux.Handle("/api/notes/", apiHandler) + mux.Handle("/api/tree", apiHandler) + + srv := &http.Server{ + Addr: *addr, + Handler: mux, + ReadTimeout: 15 * time.Second, + WriteTimeout: 15 * time.Second, + IdleTimeout: 60 * time.Second, + } + + logger.Printf("demarrage du serveur sur %s", *addr) + + go func() { + <-ctx.Done() + shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + if err := srv.Shutdown(shutdownCtx); err != nil { + logger.Printf("erreur durant l arret du serveur: %v", err) + } + }() + + if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { + logger.Fatalf("arret inattendu du serveur: %v", err) + } +} + +func ensureDir(path string) error { + info, err := os.Stat(path) + if err != nil { + if os.IsNotExist(err) { + return fmt.Errorf("le chemin %s n existe pas", path) + } + return err + } + if !info.IsDir() { + return fmt.Errorf("%s n est pas un repertoire", path) + } + return nil +}