-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.go
85 lines (68 loc) · 1.51 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
80
81
82
83
84
85
package main
import (
"bytes"
"flag"
"fmt"
"log"
"net/http"
"os"
"strings"
)
var (
// Version represents app version (injected from ldflags)
Version string
// Revision represents app revision (injected from ldflags)
Revision string
)
var port int
var isPrintVersion bool
func init() {
flag.BoolVar(&isPrintVersion, "version", false, "Whether showing version")
flag.Parse()
}
func main() {
if isPrintVersion {
printVersion()
return
}
port := os.Getenv("PORT")
if port == "" {
port = "8000"
}
fmt.Printf("gitpanda started: port=%s\n", port)
http.HandleFunc("/", handler)
http.ListenAndServe(":"+port, nil)
}
func printVersion() {
fmt.Printf("gitpanda %s, build %s\n", Version, Revision)
}
func handler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text")
switch r.Method {
case http.MethodGet:
w.Write([]byte("It works"))
case http.MethodPost:
buf := new(bytes.Buffer)
buf.ReadFrom(r.Body)
body := strings.TrimSpace(buf.String())
s := NewSlackWebhook(
os.Getenv("SLACK_OAUTH_ACCESS_TOKEN"),
&GitLabURLParserParams{
APIEndpoint: os.Getenv("GITLAB_API_ENDPOINT"),
BaseURL: os.Getenv("GITLAB_BASE_URL"),
PrivateToken: os.Getenv("GITLAB_PRIVATE_TOKEN"),
},
)
response, err := s.Request(
body,
false,
)
if err != nil {
log.Printf("[ERROR] body=%s, response=%s, error=%v", body, response, err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Write([]byte(response))
}
}