-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebgit.go
149 lines (123 loc) · 3.79 KB
/
webgit.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"bufio"
"crypto/subtle"
"fmt"
"io/ioutil"
"log"
"net/http"
"os/exec"
"strings"
"time"
)
var gitdir string
func readConfig(confName string) map[string]string {
m := make(map[string]string)
confFile, err := ioutil.ReadFile(confName)
if err != nil {
log.Fatal(err)
panic("Conf file cannot be read")
}
scanner := bufio.NewScanner(strings.NewReader(string(confFile)))
for scanner.Scan() {
prop := strings.Split(scanner.Text(), " ")
m[prop[0]] = prop[1]
}
fmt.Println(m)
return m
}
func handlerRoot(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, this is git utility\nPossible urls:\n/status\n/log\n/pull\n/rollback\n/branch?<branch_name>")
}
func handlerStatus(w http.ResponseWriter, r *http.Request) {
cmd := exec.Command("git", "-C", gitdir, "status")
output, err := cmd.Output()
// log.Printf("Output: %s", output)
fmt.Fprintf(w, "Done\n, %s", output)
if err != nil {
log.Printf("Command finished with error: %v", err)
}
}
func handlerPull(w http.ResponseWriter, r *http.Request) {
cmd := exec.Command("git", "-C", gitdir, "pull")
output, err := cmd.Output()
fmt.Fprintf(w, "Done\n, %s", output)
if err != nil {
log.Printf("Command finished with error: %v", err)
}
}
func handlerRollback(w http.ResponseWriter, r *http.Request) {
cmd := exec.Command("git", "-C", gitdir, "reset", "--hard", "HEAD^")
output, err := cmd.Output()
fmt.Fprintf(w, "Done\n, %s", output)
if err != nil {
log.Printf("Command finished with error: %v", err)
}
}
func handlerLog(w http.ResponseWriter, r *http.Request) {
cmd := exec.Command("git", "-C", gitdir, "log", "--oneline")
output, err := cmd.Output()
fmt.Fprintf(w, "Done\n, %s", output)
if err != nil {
log.Printf("Command finished with error: %v", err)
}
}
func handlerBranch(w http.ResponseWriter, r *http.Request) {
branch := r.URL.RawQuery
cmd := exec.Command("git", "-C", gitdir, "checkout", branch)
output, err := cmd.Output()
fmt.Fprintf(w, "Done, %s", output)
if err != nil {
log.Printf("Command finished with error: %v", err)
}
}
func main() {
configMap := readConfig("properties.conf")
port := configMap["port"]
gitdir = configMap["gitdir"]
username := configMap["username"]
password := configMap["password"]
msg := "Please enter your credentials for this site"
siteMux := http.NewServeMux()
siteMux.HandleFunc("/", handlerRoot)
siteMux.HandleFunc("/status", handlerStatus)
siteMux.HandleFunc("/pull", handlerPull)
siteMux.HandleFunc("/rollback", handlerRollback)
siteMux.HandleFunc("/log", handlerLog)
siteMux.HandleFunc("/branch", handlerBranch)
siteHandler := accessLogMiddleware(siteMux)
siteHandler = basicAuth(siteHandler, username, password, msg)
siteHandler = errorMiddleware(siteHandler)
http.ListenAndServe(port, siteHandler)
}
func accessLogMiddleware(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
h.ServeHTTP(w, r)
fmt.Printf("[%s] %s, %s %s, %s\n",
r.Method, r.RemoteAddr, r.URL.Path, time.Since(start), time.Now())
})
}
func errorMiddleware(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// var err error
defer func() {
if err := recover(); err != nil {
log.Printf("Recovered from panic: %v", err)
}
}()
h.ServeHTTP(w, r)
})
}
func basicAuth(h http.Handler, username, password, realm string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user, pass, ok := r.BasicAuth()
if !ok || subtle.ConstantTimeCompare([]byte(user), []byte(username)) != 1 || subtle.ConstantTimeCompare([]byte(pass), []byte(password)) != 1 {
w.Header().Set("WWW-Authenticate", `Basic realm="`+realm+`"`)
w.WriteHeader(401)
w.Write([]byte("Unauthorised.\n"))
return
}
h.ServeHTTP(w, r)
})
}