generated from axetroy/go-cli-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.go
79 lines (66 loc) · 1.67 KB
/
main.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
package main
import (
"bytes"
"fmt"
"log"
"net/http"
"os"
"regexp"
"github.com/pkg/errors"
"github.com/whatchanged-community/whatchanged"
"github.com/whatchanged-community/whatchanged/option"
)
func handler(w http.ResponseWriter, r *http.Request) {
var (
err error
output = bytes.NewBuffer([]byte{})
)
// cors
w.Header().Set("Access-Control-Allow-Origin", r.Header.Get("Origin"))
w.Header().Set("Access-Control-Allow-Methods", http.MethodGet)
if r.Method == http.MethodOptions {
w.WriteHeader(200)
_, _ = w.Write([]byte{})
return
}
defer func() {
if r, ok := recover().(error); ok {
err = r
}
if err != nil {
b := []byte(fmt.Sprintf("%+v\n", err))
w.Header().Set("Content-Type", "text/plain; charset=UTF-8")
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write(b)
} else {
w.Header().Set("Content-Type", "text/markdown; charset=UTF-8")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(output.Bytes())
}
}()
query := r.URL.Query()
username := query.Get("username")
repo := query.Get("repo")
branch := query.Get("branch")
version := query.Get("version")
template := query.Get("template")
preset := query.Get("preset")
url := fmt.Sprintf("https://github.com/%s/%s", username, repo)
if err = whatchanged.Generate(url, output, &option.Options{
Version: regexp.MustCompile(`\s+`).Split(version, -1),
Branch: branch,
Template: template,
Preset: option.Preset(preset),
}); err != nil {
err = errors.WithStack(err)
}
}
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
http.HandleFunc("/", handler)
print("Listen on port ", port, "\n")
log.Fatal(http.ListenAndServe(":"+port, nil))
}