forked from avelino/awesome-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repo.go
59 lines (48 loc) · 1007 Bytes
/
repo.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
package main
import (
"io/ioutil"
"net/http"
"os"
"os/exec"
"text/template"
"github.com/gorilla/mux"
gfm "github.com/shurcooL/github_flavored_markdown"
)
// memory usage optimizations
const (
emtyStr = ""
git = "git"
checkout = "checkout"
force = "-f"
pull = "pull"
// options
readmePath = "./README.md"
tplPath = "tmpl/tmpl.html"
idxPath = "tmpl/index.html"
)
var (
doneResp = []byte("Done!\n")
)
type content struct {
Body string
}
func generateHTML() {
// Update repo
exec.Command(git, checkout, force).Output()
exec.Command(git, pull).Output()
input, _ := ioutil.ReadFile(readmePath)
body := string(gfm.Markdown(input))
c := &content{Body: body}
t := template.Must(template.ParseFiles(tplPath))
f, _ := os.Create(idxPath)
t.Execute(f, c)
}
func hookHandler(w http.ResponseWriter, r *http.Request) {
go generateHTML()
w.Write(doneResp)
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/hook", hookHandler)
http.ListenAndServe(":9000", r)
}