-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.go
81 lines (69 loc) · 1.63 KB
/
serve.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"context"
"errors"
"html/template"
"log"
"net/http"
"os"
"os/signal"
"path/filepath"
"github.com/FurqanSoftware/blink/pipe"
"github.com/adrg/frontmatter"
"github.com/spf13/cobra"
)
var serveCmd = &cobra.Command{
Use: "serve",
Short: "Serve serves scraped programming language references",
Run: func(cmd *cobra.Command, args []string) {
exitCh := make(chan struct{})
errCh := make(chan error)
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
http.HandleFunc("/style.css", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "serve/style.css")
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
f, err := os.Open(filepath.Join("out", r.URL.Path, "index.html"))
if errors.Is(err, os.ErrNotExist) {
http.Error(w, "Not Found", http.StatusNotFound)
return
}
tpl, err := template.ParseGlob("serve/*.html")
catch(err)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
meta := pipe.Meta{}
body, err := frontmatter.Parse(f, &meta)
catch(err)
err = tpl.Execute(w, struct {
Meta pipe.Meta
Body template.HTML
}{
Meta: meta,
Body: template.HTML(body),
})
catch(err)
})
go func() {
err := http.ListenAndServe(":8080", nil)
if err != nil {
errCh <- err
}
close(exitCh)
}()
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt)
select {
case err := <-errCh:
log.Fatal(err)
case <-exitCh:
case sig := <-sigCh:
log.Printf("Received %s; exiting", sig)
}
log.Print("Bye")
},
}
func init() {
rootCmd.AddCommand(serveCmd)
}