-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
41 lines (33 loc) · 898 Bytes
/
main.go
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
package main
import (
"context"
"log"
"net/http"
"os"
"path/filepath"
"github.com/mwaurathealex/static-generator/views/home"
)
func main() {
f, err := os.Create("../index.html")
if err != nil {
log.Fatalf("failed to create output file: %v", err)
}
err = home.Index().Render(context.Background(), f)
if err != nil {
log.Fatalf("failed to write output file: %v", err)
}
indexPath := filepath.Join("..", "index.html")
assetPath := filepath.Join("..", "assets")
// Handler to serve the index.html file
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
http.ServeFile(w, r, indexPath)
})
// File server to serve static files from the assets directory
fs := http.FileServer(http.Dir(assetPath))
http.Handle("/assets/", http.StripPrefix("/assets/", fs))
http.ListenAndServe(":3000", nil)
}