blob: 812104348833483f70cdd43c97696a1e9c8052c7 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
package blog
import (
"os"
"io"
"fmt"
"net/http"
"github.com/gorilla/mux"
)
type FileReader struct{}
type SlugReader interface {
Read(slug string) (string, error)
}
func (fsr FileReader) Read(slug string) (string, error) {
f, err := os.Open(slug + ".md")
if err != nil {
return "", err
}
defer f.Close()
b, err := io.ReadAll(f)
if err != nil {
return "", err
}
return string(b), nil
}
func PostHandler(sl SlugReader) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) // Correctly retrieve vars from the request
slug := vars["slug"] // Access the slug
postMarkdown, err := sl.Read(slug)
if err != nil {
http.Error(w, "Post not found", http.StatusNotFound)
return
}
fmt.Fprint(w, postMarkdown)
}
}
|