/** * microblog in go * * get goldmark md parser * go get github.com/yuin/goldmark **/ package main import ( "html/template" "io/ioutil" "net/http" "os" "path/filepath" "strings" "github.com/yuin/goldmark" ) const articlesDir = "data/articles" func main() { http.HandleFunc("/", handlePage) println("Server läuft auf http://localhost:8085") http.ListenAndServe(":8085", nil) } func handlePage(w http.ResponseWriter, r *http.Request) { path := r.URL.Path if path == "/" { path = "/index" } // Entferne führenden Slash pagePath := strings.TrimPrefix(path, "/") // Baue den Pfad zur Markdown-Datei mdPath := filepath.Join(articlesDir, filepath.FromSlash(pagePath)+".md") // Überprüfe, ob die Datei existiert if _, err := os.Stat(mdPath); os.IsNotExist(err) { http.NotFound(w, r) return } // Datei lesen data, err := ioutil.ReadFile(mdPath) if err != nil { http.Error(w, "Fehler beim Lesen der Seite", 500) return } // Markdown in HTML umwandeln mit Goldmark htmlContent, err := markdownToHTML(data) if err != nil { http.Error(w, "Fehler bei der Konvertierung", 500) return } // Seiten-Titel aus Dateiname oder erster Überschrift (optional) title := filepath.Base(pagePath) title = strings.ReplaceAll(title, "_", " ") title = strings.Title(title) // HTML-Template tmpl := `