Change d'interface plus légére, modification side barre
This commit is contained in:
@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
@ -10,6 +11,7 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
@ -20,21 +22,82 @@ import (
|
||||
)
|
||||
|
||||
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 {
|
||||
if err := ensureDir(notesDir); err != nil {
|
||||
logger.Fatalf("repertoire notes invalide: %v", err)
|
||||
}
|
||||
|
||||
idx := indexer.New()
|
||||
if err := idx.Load(*notesDir); err != nil {
|
||||
if err := idx.Load(notesDir); err != nil {
|
||||
logger.Fatalf("echec de l indexation initiale: %v", err)
|
||||
}
|
||||
|
||||
@ -45,7 +108,7 @@ func main() {
|
||||
}
|
||||
logger.Printf("traductions chargees: %v", translator.GetAvailableLanguages())
|
||||
|
||||
w, err := watcher.Start(ctx, *notesDir, idx, logger)
|
||||
w, err := watcher.Start(ctx, notesDir, idx, logger)
|
||||
if err != nil {
|
||||
logger.Fatalf("echec du watcher: %v", err)
|
||||
}
|
||||
@ -63,6 +126,12 @@ func main() {
|
||||
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)
|
||||
@ -77,7 +146,7 @@ func main() {
|
||||
}
|
||||
})
|
||||
|
||||
apiHandler := api.NewHandler(*notesDir, idx, templates, logger, translator)
|
||||
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
|
||||
@ -93,16 +162,18 @@ func main() {
|
||||
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,
|
||||
Addr: addr,
|
||||
Handler: mux,
|
||||
ReadTimeout: 15 * time.Second,
|
||||
WriteTimeout: 15 * time.Second,
|
||||
IdleTimeout: 60 * time.Second,
|
||||
}
|
||||
|
||||
logger.Printf("demarrage du serveur sur %s", *addr)
|
||||
logger.Printf("demarrage du serveur sur %s", addr)
|
||||
|
||||
go func() {
|
||||
<-ctx.Done()
|
||||
|
||||
Reference in New Issue
Block a user