205 lines
5.8 KiB
Go
205 lines
5.8 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"html/template"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"path/filepath"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/mathieu/personotes/internal/api"
|
|
"github.com/mathieu/personotes/internal/i18n"
|
|
"github.com/mathieu/personotes/internal/indexer"
|
|
"github.com/mathieu/personotes/internal/watcher"
|
|
)
|
|
|
|
func main() {
|
|
// Vérifier si une sous-commande est demandée
|
|
if len(os.Args) > 1 && os.Args[1] == "list-public" {
|
|
cmdListPublic()
|
|
return
|
|
}
|
|
|
|
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)
|
|
|
|
runServer(*addr, *notesDir, logger)
|
|
}
|
|
|
|
// cmdListPublic liste les notes publiques
|
|
func cmdListPublic() {
|
|
notesDir := "./notes"
|
|
if len(os.Args) > 2 && os.Args[2] != "" {
|
|
notesDir = os.Args[2]
|
|
}
|
|
|
|
publicFile := filepath.Join(notesDir, ".public.json")
|
|
|
|
// Lire le fichier .public.json
|
|
data, err := os.ReadFile(publicFile)
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
fmt.Println("Aucune note publique trouvée.")
|
|
fmt.Printf("Le fichier %s n'existe pas.\n", publicFile)
|
|
os.Exit(0)
|
|
}
|
|
fmt.Fprintf(os.Stderr, "Erreur de lecture: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
var publicNotes struct {
|
|
Notes []struct {
|
|
Path string `json:"path"`
|
|
Title string `json:"title"`
|
|
PublishedAt time.Time `json:"published_at"`
|
|
} `json:"notes"`
|
|
}
|
|
|
|
if err := json.Unmarshal(data, &publicNotes); err != nil {
|
|
fmt.Fprintf(os.Stderr, "Erreur de parsing JSON: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
if len(publicNotes.Notes) == 0 {
|
|
fmt.Println("Aucune note publique.")
|
|
os.Exit(0)
|
|
}
|
|
|
|
fmt.Printf("\n📚 Notes publiques (%d):\n\n", len(publicNotes.Notes))
|
|
for _, note := range publicNotes.Notes {
|
|
filename := filepath.Base(note.Path)
|
|
htmlFile := filename[:len(filename)-3] + ".html"
|
|
|
|
fmt.Printf("• %s\n", note.Title)
|
|
fmt.Printf(" Source: %s\n", note.Path)
|
|
fmt.Printf(" Public: public/%s\n", htmlFile)
|
|
fmt.Printf(" Date: %s\n\n", note.PublishedAt.Format("2006-01-02 15:04:05"))
|
|
}
|
|
}
|
|
|
|
func runServer(addr, notesDir string, logger *log.Logger) {
|
|
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)
|
|
}
|
|
|
|
// Load translations
|
|
translator := i18n.New("en") // Default language: English
|
|
if err := translator.LoadFromDir("./locales"); err != nil {
|
|
logger.Fatalf("echec du chargement des traductions: %v", err)
|
|
}
|
|
logger.Printf("traductions chargees: %v", translator.GetAvailableLanguages())
|
|
|
|
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"))))
|
|
|
|
// Servir les fichiers HTML publics (notes exportées)
|
|
publicDir := filepath.Join(notesDir, "..", "public")
|
|
if _, err := os.Stat(publicDir); err == nil {
|
|
mux.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir(publicDir))))
|
|
}
|
|
|
|
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, translator)
|
|
mux.Handle("/api/i18n/", apiHandler) // I18n translations
|
|
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/folder/", apiHandler) // Folder view
|
|
mux.Handle("/api/notes/", apiHandler)
|
|
mux.Handle("/api/tree", apiHandler)
|
|
mux.Handle("/api/public/list", apiHandler) // List public notes
|
|
mux.Handle("/api/public/toggle", apiHandler) // Toggle public status (génère HTML statique)
|
|
|
|
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
|
|
}
|