forked from CurtisLusmore/ghp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ghp.go
98 lines (83 loc) · 2.12 KB
/
ghp.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"path/filepath"
)
var root string
func handler(w http.ResponseWriter, r *http.Request) {
basepath := root + r.URL.Path
filename := basepath
if fi, err := os.Stat(filename); fi != nil && fi.IsDir() {
filename = basepath + "/index.html"
} else if os.IsNotExist(err) {
filename = basepath + ".html"
if _, err := os.Stat(filename); os.IsNotExist(err) {
filename = basepath + ".md"
}
}
if _, err := os.Stat(filename); os.IsNotExist(err) {
filename = root + "/404.html"
page, _ := ioutil.ReadFile(filename)
w.WriteHeader(http.StatusNotFound)
w.Write(page)
fmt.Printf("GET %s 404 NOT FOUND\n", r.URL.Path)
return
}
filename = filepath.Clean(filename)
renderFile(w, r, filename)
fmt.Printf("GET %s (%s) 200 OK\n", r.URL.Path, filename)
}
func main() {
var port int
flag.StringVar(&root, "root", ".",
"The root directory to serve files from "+
"(your GitHub Pages repo)")
flag.IntVar(&port, "port", 8080, "The port to serve over")
flag.Parse()
root, err := filepath.Abs(root)
if err != nil {
fmt.Printf("Unable to serve directory: %s\n", root)
os.Exit(1)
}
fmt.Printf("Serving from %s on http://localhost:%d ...\n", root, port)
http.HandleFunc("/", handler)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
}
func renderFile(w http.ResponseWriter, r *http.Request, filename string) {
ext := path.Ext(filename)
switch ext {
case ".md":
content, err := ioutil.ReadFile(filename)
if err != nil {
http.ServeFile(w, r, filename)
}
htmlContent, err := convertMarkdown(content)
if err != nil {
http.ServeFile(w, r, filename)
}
w.Write(htmlContent)
default:
http.ServeFile(w, r, filename)
}
}
func convertMarkdown(mdText []byte) ([]byte, error) {
body := bytes.NewReader(mdText)
req, err := http.NewRequest("POST", "https://api.github.com/markdown/raw", body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "text/plain")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
}