-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathowlbert.go
More file actions
68 lines (59 loc) · 1.49 KB
/
owlbert.go
File metadata and controls
68 lines (59 loc) · 1.49 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
package main
import (
"log"
"net/http"
"os"
"sync"
"./pinlike"
"./webhook"
)
var (
pin pinlike.PinLike
mutex *sync.Mutex
)
const webhooksMapping = "/webhooks/"
func main() {
if len(os.Args) != 2 {
log.Fatal("Usage: owlbert <port>")
os.Exit(1)
}
port := os.Args[1]
var conn pinlike.Connection
conn = new(pinlike.GpioConnection)
//conn = new(pinlike.StubConnection)
if err := conn.Open(); err != nil {
panic(err)
}
// Unmap gpio memory when done
defer conn.Close()
// Pin 10 here is exposed on the pin header as physical pin 19
pin = pinlike.NewGpioPinLike(14)
//pin = new(pinlike.StubPinLike)
mutex = &sync.Mutex{}
http.HandleFunc(webhooksMapping, webhooksHandler)
log.Fatal(http.ListenAndServe(":"+port, nil))
}
func webhooksHandler(w http.ResponseWriter, r *http.Request) {
configName := r.URL.Path[len(webhooksMapping):]
metaData, err := webhook.ReadGitlabHookMetaData(r)
if err != nil {
log.Println("Could not read request body.")
return
}
action, err := webhook.LoadWebhookAction(configName, metaData.ObjectKind)
if err != nil {
log.Println("Error while loading action for "+configName, err)
return
}
if action == nil {
log.Printf("Could not find action '%v' in config '%v'.", metaData.ObjectKind, configName)
return
}
if !action.IsAuthorized(r) {
log.Printf("Denied unauthorized access to config '%v'.", configName)
return
}
mutex.Lock()
defer mutex.Unlock()
action.Run(pin)
}