-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
123 lines (99 loc) · 2.39 KB
/
main.go
File metadata and controls
123 lines (99 loc) · 2.39 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"net/http"
"os"
"os/exec"
"log"
"fmt"
"strings"
)
const (
Route = "/build/"
DefaultPort = "8080"
)
var queue = make(chan string, 100)
func JekyllBuild(path string) {
url := strings.Split(path, "/")
host, user, name := url[2], url[3], url[4]
tmp := "/tmp"
dir := tmp + "/src/" + name
dest := tmp + "/build/" + name
// Default to using git+ssh for cloning.
// The commented out line uses https which is great for public repos
repo := "git@" + host + ":" + user + "/" + name + ".git"
// repo := "https://" + host + "/" + user + "/" + name + ".git"
cmd := []string{
"git clone %[2]s %[1]s &&",
"cd %[1]s;",
// "git checkout master",
"[ -f Gemfile ] && bundle install;",
"jekyll build -s %[1]s -d %[3]s;",
"rm -Rf %[1]s;"}
info("Cloning " + repo)
info("Building Jekyll site ...")
out, err := exec.Command("sh", "-c", fmt.Sprintf(strings.Join(cmd, " "), dir, repo, dest)).Output()
log.Printf("%s\n", out)
if err != nil {
fail("Jekyll Error", err)
return
}
info("Jekyll site built successfully.")
JekyllPublish(dest)
}
func JekyllPublish(dir string) {
// Determine deployment method from DEPLOY file
out, err := exec.Command("sh", "-c", fmt.Sprintf("cd %s && tr -d '\r\n' < DEPLOY", dir)).Output()
method := string(out)
if err != nil {
method = "amazon" // default to Amazon S3
}
if method == "amazon" {
err = PublishAmazon(dir)
}
if method == "surge" {
err = PublishSurge(dir)
}
if method == "rsync" {
err = PublishRsync(dir)
}
// remove build
os.RemoveAll(dir)
if err == nil {
info("Success! Jekyll site published.")
}
}
/*
* POST /build/:host/:user/:name
*/
func routeHandler(rw http.ResponseWriter, r *http.Request) {
// must be POST
if r.Method != "POST" {
rw.WriteHeader(http.StatusNotFound)
return
}
queue <- r.URL.Path
rw.WriteHeader(http.StatusCreated)
return
}
// listen for url paths from queue channel
// and process builds one at a time.
func workOnQueue() {
go func() {
for {
select {
case path := <-queue:
info("Processing POST " + path)
JekyllBuild(path)
}
}
}()
}
func main() {
// start the queue
workOnQueue()
// listen for requests
http.HandleFunc(Route, routeHandler)
port := os.Getenv("PORT")
if port == "" { port = DefaultPort }
http.ListenAndServe(":"+port, nil)
}