2016-10-11 00:21:46 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"text/template"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2017-04-27 14:08:18 +00:00
|
|
|
gfm "github.com/shurcooL/github_flavored_markdown"
|
2016-10-11 00:21:46 +00:00
|
|
|
)
|
|
|
|
|
2016-10-11 01:30:20 +00:00
|
|
|
// 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")
|
|
|
|
)
|
|
|
|
|
2016-10-11 00:21:46 +00:00
|
|
|
type content struct {
|
|
|
|
Body string
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateHTML() {
|
|
|
|
// Update repo
|
2016-10-11 01:30:20 +00:00
|
|
|
exec.Command(git, checkout, force).Output()
|
|
|
|
exec.Command(git, pull).Output()
|
2016-10-11 00:21:46 +00:00
|
|
|
|
2016-10-11 01:30:20 +00:00
|
|
|
input, _ := ioutil.ReadFile(readmePath)
|
2017-04-27 14:08:18 +00:00
|
|
|
body := string(gfm.Markdown(input))
|
2016-10-11 00:21:46 +00:00
|
|
|
c := &content{Body: body}
|
|
|
|
|
2016-10-11 01:30:20 +00:00
|
|
|
t := template.Must(template.ParseFiles(tplPath))
|
|
|
|
f, _ := os.Create(idxPath)
|
2016-10-11 00:21:46 +00:00
|
|
|
t.Execute(f, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func hookHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
go generateHTML()
|
2016-10-11 01:30:20 +00:00
|
|
|
w.Write(doneResp)
|
2016-10-11 00:21:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
r := mux.NewRouter()
|
|
|
|
r.HandleFunc("/hook", hookHandler)
|
|
|
|
http.ListenAndServe(":9000", r)
|
|
|
|
}
|