Files
micromdm/workflow/webhook/command.go
Victor Vrantchan 91c236c8c3 organize essential APIs into platform, workflow and pkg folders (#337)
Add more logic to the way code is organized.

	/pkg -- library code not directly connected to micromdm
	/mdm -- packages meant for the services devices interract with. The MDM protocol.
	/dep -- DEP API and related packages.
	/platform -- Core APIs the server provides. Commands API, Devices API, queue, pubsub etc.
	/workflow -- Packages/API that build on top of platform. Today that's the webhook package.
		     Depending on what ends up here, the workflow folder might become its own repository.
2017-11-23 22:07:57 -05:00

61 lines
1.3 KiB
Go

package webhook
import (
"bytes"
"context"
"fmt"
"net/http"
"github.com/micromdm/micromdm/mdm/connect"
"github.com/micromdm/micromdm/platform/pubsub"
"github.com/pkg/errors"
)
const contentType = "application/x-apple-aspen-mdm"
type CommandWebhook struct {
Topic string
CallbackURL string
HTTPClient *http.Client
}
func NewCommandWebhook(httpClient *http.Client, topic, callbackURL string) (*CommandWebhook, error) {
if topic == "" {
return nil, errors.New("webhook: topic should not be empty")
}
if callbackURL == "" {
return nil, errors.New("webhook: callbackURL should not be empty")
}
return &CommandWebhook{HTTPClient: httpClient, Topic: topic, CallbackURL: callbackURL}, nil
}
func (cw CommandWebhook) StartListener(sub pubsub.Subscriber) error {
connectEvents, err := sub.Subscribe(context.TODO(), "commandWebhook", cw.Topic)
if err != nil {
return errors.Wrapf(err,
"subscribing commandWebhook to %s topic", cw.Topic)
}
go func() {
for {
select {
case event := <-connectEvents:
var ev connect.Event
if err := connect.UnmarshalEvent(event.Message, &ev); err != nil {
fmt.Println(err)
continue
}
_, err := cw.HTTPClient.Post(cw.CallbackURL, contentType, bytes.NewBuffer(ev.Raw))
if err != nil {
fmt.Printf("error sending command response: %s\n", err)
}
}
}
}()
return nil
}