Last active
September 20, 2019 14:44
-
-
Save ripienaar/37b9d26c90835d14fd1b3f86a001138d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/yaegi | |
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"os" | |
) | |
type activationCheck struct { | |
Schema string `json:"$schema"` | |
Protocol string `json:"protocol"` | |
Agent string `json:"agent"` | |
} | |
type activationReply struct { | |
ShouldActivate bool `json:"activate"` | |
} | |
type request struct { | |
Data json.RawMessage `json:"data"` | |
} | |
type reply struct { | |
Statuscode int `json:"statuscode"` | |
Statusmsg string `json:"statusmsg"` | |
Data interface{} `json:"data"` | |
DisableResponse bool `json:"-"` | |
} | |
type rpcReq struct { | |
Message string `json:"message"` | |
} | |
type rpcRep struct { | |
Message string `json:"message"` | |
} | |
func writeReply(j []byte) { | |
f, err := os.Create(os.Getenv("CHORIA_EXTERNAL_REPLY")) | |
panicIfError(err, "writing reply failed") | |
defer f.Close() | |
_, err = fmt.Fprint(f, string(j)) | |
panicIfError(err, "writing reply failed") | |
} | |
func failIfError(err error, msg string) { | |
if err != nil { | |
rep := &reply{ | |
Statuscode: 1, | |
Statusmsg: fmt.Sprintf("%s: %s", msg, err), | |
Data: make(map[string]string), | |
} | |
j, err := json.Marshal(rep) | |
panicIfError(err, "could not write reply") | |
writeReply(j) | |
} | |
} | |
func panicIfError(err error, msg string) { | |
if err != nil { | |
fmt.Fprintf(os.Stderr, "%s: %s", msg, err) | |
os.Exit(0) | |
} | |
} | |
func handleActivation() { | |
j, err := json.Marshal(activationReply{true}) | |
panicIfError(err, "activation failed") | |
f, err := os.Create(os.Getenv("CHORIA_EXTERNAL_REPLY")) | |
panicIfError(err, "activation failed") | |
defer f.Close() | |
_, err = fmt.Fprint(f, string(j)) | |
panicIfError(err, "activation failed") | |
} | |
func handleRequest() { | |
req := &request{} | |
rep := &reply{} | |
rprcreq := &rpcReq{} | |
jreq, err := ioutil.ReadFile(os.Getenv("CHORIA_EXTERNAL_REQUEST")) | |
failIfError(err, "could not read request") | |
failIfError(json.Unmarshal(jreq, req), "could not parse request") | |
failIfError(json.Unmarshal(req.Data, rprcreq), "could not parse request") | |
rep.Data = rpcRep{ | |
Message: rprcreq.Message, | |
} | |
jrep, err := json.Marshal(rep) | |
panicIfError(err, "encoding reply failed") | |
f, err := os.Create(os.Getenv("CHORIA_EXTERNAL_REPLY")) | |
panicIfError(err, "writing reply failed") | |
defer f.Close() | |
_, err = fmt.Fprint(f, string(jrep)) | |
panicIfError(err, "writing reply failed") | |
} | |
func main() { | |
proto := os.Getenv("CHORIA_EXTERNAL_PROTOCOL") | |
switch proto { | |
case `io.choria.mcorpc.external.v1.activation_request`: | |
handleActivation() | |
case `io.choria.mcorpc.external.v1.rpc_request`: | |
handleRequest() | |
default: | |
fmt.Fprintf(os.Stderr, "Unknown protocol '%s'\n", proto) | |
os.Exit(1) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment