chore: refactor to new server.go file dccfdbcb
Steve · 2026-11-14 16:30 2 file(s) · +63 −55
main.go +4 −55
1 1
package main
2 2
3 3
import (
4 -
	"embed"
5 4
	"fmt"
6 -
	"html/template"
7 5
	"log"
8 6
	"net/http"
9 -
	"os"
10 7
)
11 8
12 9
// Plan
15 12
// - [x] Serve dynamic path that takes in path param to serve babbler and more links
16 13
// - [ ] Slow down responses
17 14
18 -
var portEnv = os.Getenv("PORT")
19 -
20 -
//go:embed templates/*.html
21 -
var templatesFS embed.FS
22 -
var tmpl = template.Must(template.ParseFS(templatesFS, "templates/*.html"))
23 -
24 -
//go:embed robots.txt
25 -
var robotsTxt string
26 -
27 -
type PageData struct {
28 -
	Title   string
29 -
	Content string
30 -
	Links   []string
31 -
}
32 -
33 -
func robotsHandler(writer http.ResponseWriter, req *http.Request) {
34 -
	writer.Header().Set("Content-Type", "text/plain; charset=utf-8")
35 -
	fmt.Fprint(writer, robotsTxt)
36 -
}
37 -
38 -
func indexHandler(writer http.ResponseWriter, req *http.Request) {
39 -
	babbler := NewBabbler()
40 -
	var title string
41 -
	if req.PathValue("slug") != "" {
42 -
		title = req.PathValue("slug")
43 -
	} else {
44 -
		title = babbler.Generate(2)
45 -
	}
46 -
	content := babbler.Generate(500)
47 -
	links := babbler.Links(5)
48 -
	data := PageData{
49 -
		Title:   title,
50 -
		Content: content,
51 -
		Links:   links,
52 -
	}
53 -
	if err := tmpl.ExecuteTemplate(writer, "index.html", data); err != nil {
54 -
		http.Error(writer, "Internal server error", http.StatusInternalServerError)
55 -
	}
56 -
}
57 -
58 -
func requestMiddleware(handler http.Handler) http.Handler {
59 -
	return http.HandlerFunc(func(writer http.ResponseWriter, req *http.Request) {
60 -
		log.Printf("%s %s %s\n", req.Method, req.URL, req.UserAgent())
61 -
		writer.Header().Set("X-Robots-Tag", "noindex, nofollow")
62 -
		handler.ServeHTTP(writer, req)
63 -
	})
64 -
}
65 -
66 15
func main() {
67 16
	if portEnv == "" {
68 17
		portEnv = "8090"
69 18
	}
70 19
71 -
	http.HandleFunc("/robots.txt", robotsHandler)
72 -
	http.HandleFunc("/", indexHandler)
73 -
	http.HandleFunc("/{slug}", indexHandler)
20 +
	http.HandleFunc("/robots.txt", RobotsHandler)
21 +
	http.HandleFunc("/", IndexHandler)
22 +
	http.HandleFunc("/{slug}", IndexHandler)
74 23
75 24
	port := fmt.Sprintf(":%s", portEnv)
76 25
	log.Println("Listening on", port)
77 -
	log.Fatal(http.ListenAndServe(port, requestMiddleware(http.DefaultServeMux)))
26 +
	log.Fatal(http.ListenAndServe(port, RequestMiddleware(http.DefaultServeMux)))
78 27
}
server.go (added) +59 −0
1 +
package main 
2 +
3 +
import (
4 +
	"embed"
5 +
	"fmt"
6 +
	"html/template"
7 +
	"log"
8 +
	"net/http"
9 +
	"os"
10 +
)
11 +
12 +
var portEnv = os.Getenv("PORT")
13 +
14 +
//go:embed templates/*.html
15 +
var templatesFS embed.FS
16 +
var tmpl = template.Must(template.ParseFS(templatesFS, "templates/*.html"))
17 +
18 +
//go:embed robots.txt
19 +
var robotsTxt string
20 +
21 +
type PageData struct {
22 +
	Title   string
23 +
	Content string
24 +
	Links   []string
25 +
}
26 +
27 +
func RobotsHandler(writer http.ResponseWriter, req *http.Request) {
28 +
	writer.Header().Set("Content-Type", "text/plain; charset=utf-8")
29 +
	fmt.Fprint(writer, robotsTxt)
30 +
}
31 +
32 +
func IndexHandler(writer http.ResponseWriter, req *http.Request) {
33 +
	babbler := NewBabbler()
34 +
	var title string
35 +
	if req.PathValue("slug") != "" {
36 +
		title = req.PathValue("slug")
37 +
	} else {
38 +
		title = babbler.Generate(2)
39 +
	}
40 +
	content := babbler.Generate(500)
41 +
	links := babbler.Links(5)
42 +
	data := PageData{
43 +
		Title:   title,
44 +
		Content: content,
45 +
		Links:   links,
46 +
	}
47 +
	if err := tmpl.ExecuteTemplate(writer, "index.html", data); err != nil {
48 +
		http.Error(writer, "Internal server error", http.StatusInternalServerError)
49 +
	}
50 +
}
51 +
52 +
func RequestMiddleware(handler http.Handler) http.Handler {
53 +
	return http.HandlerFunc(func(writer http.ResponseWriter, req *http.Request) {
54 +
		log.Printf("%s %s %s\n", req.Method, req.URL, req.UserAgent())
55 +
		writer.Header().Set("X-Robots-Tag", "noindex, nofollow")
56 +
		handler.ServeHTTP(writer, req)
57 +
	})
58 +
}
59 +