/
events.go
83 lines (70 loc) · 2.42 KB
/
events.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
package app
import (
"encoding/json"
"net/http"
"github.com/astaxie/beego"
"github.com/snagles/docker-registry-manager/app/models"
)
// EventsController handles all posting of envelopes from the registries configured endpoints
// in addition to all retrieval of the in memory store of posted registry events
type EventsController struct {
beego.Controller
}
func (c *EventsController) Get() {
manager.AllEvents.Lock()
defer manager.AllEvents.Unlock()
c.Data["events"] = manager.AllEvents.Events
c.TplName = "events.tpl"
}
// GetRegistryEvents returns the JSON representation of all events for the given registry
func (c *EventsController) GetRegistryEvents() {
// Get the registry
registryName := c.Ctx.Input.Param(":registryName")
manager.AllEvents.Lock()
defer manager.AllEvents.Unlock()
// make sure the registry exists in the map
if _, ok := manager.AllEvents.Events[registryName]; !ok {
c.CustomAbort(404, "No events found for registry.")
return
}
c.Data["json"] = manager.AllEvents.Events[registryName]
c.ServeJSON()
}
// GetRegistryEventID uses the passed registry and unique event id to return the json
// representation of the event
func (c *EventsController) GetRegistryEventID() {
// Get the registry
registryName := c.Ctx.Input.Param(":registryName")
manager.AllEvents.Lock()
defer manager.AllEvents.Unlock()
// make sure the registry exists in the map
if _, ok := manager.AllEvents.Events[registryName]; !ok {
c.CustomAbort(404, "No events found for registry.")
return
}
// get the registry id
eventID := c.Ctx.Input.Param(":eventID")
if _, ok := manager.AllEvents.Events[registryName][eventID]; !ok {
c.CustomAbort(404, "Event ID not found.")
return
}
c.Data["json"] = manager.AllEvents.Events[registryName][eventID]
c.ServeJSON()
}
// GetEvents returns the JSON representation of all events received from ALL of the registries
func (c *EventsController) GetEvents() {
manager.AllEvents.Lock()
defer manager.AllEvents.Unlock()
c.Data["json"] = manager.AllEvents.Events
c.ServeJSON()
}
// PostEnvelope is th endpoint used by the notification systems of the given registry.
// see https://docs.docker.com/registry/notifications/#configuration for details
func (c *EventsController) PostEnvelope() {
e := manager.Envelope{}
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &e); err != nil {
c.CustomAbort(http.StatusNotFound, "Unable to parse envelope.")
}
e.Process()
c.CustomAbort(200, "")
}