mirror of
https://github.com/micromdm/micromdm/
synced 2026-08-05 17:05:51 +08:00
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.
This commit is contained in:
60
workflow/webhook/command.go
Normal file
60
workflow/webhook/command.go
Normal file
@@ -0,0 +1,60 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user