-
Notifications
You must be signed in to change notification settings - Fork 0
/
e5e.go
142 lines (116 loc) · 5.24 KB
/
e5e.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Package e5e helps Anexia customers who want to use the e5e Functions-as-a-Service (FaaS)
// offering in the [Anexia Engine].
//
// It provides a simple runtime which properly handles the input from the runtime.
//
// [Anexia Engine]: https://engine.anexia-it.com/docs/en/module/e5e/
package e5e // import "go.anx.io/e5e/v2"
// EventDataType tells more information about the type of the data inside an [Event].
type EventDataType string
const (
// EventDataTypeText tells us that the data is a plain string.
//
// Equivalent to the `text/plain` content type.
EventDataTypeText EventDataType = "text"
// EventDataTypeObject tells us that the data is a JSON object.
//
// The data is either a primitive data type (bool, int) or contains structured
// data like a struct or a map.
//
// Equivalent to the `application/*` content type.
EventDataTypeObject EventDataType = "object"
// EventDataTypeBinary tells us that the data is a base64 encoded string.
//
// The data contains a binary object representation of the response body.
EventDataTypeBinary EventDataType = "binary"
// EventDataTypeMixed tells us about mixed data.
//
// The data contains a `map[string][]any` where each key represents a field
// name submitted by the client. Since a field name may occur multiple times
// within one request, the values of a field are always given as a list.
// Each value might be of a primitive data type such as string, int, bool, nil
// or it might be a binary object representation.
//
// Equivalent to the `multipart/form-data` content type.
EventDataTypeMixed EventDataType = "mixed"
)
// The Data constraint is used to constrain the incoming data for [Event] and [Context].
// It's equal to any, because there's not a good constraint which would be equivalent to `Serializable`.
type Data any
// Event contains all sorts of information about the event that triggered the
// execution, such as GET parameters, request headers, input data and the type of the input data.
type Event[T Data] struct {
// Params contains the GET parameters of the request.
// As GET parameters can occur multiple times within a single request,
// the values are given as a list.
Params map[string][]string `json:"params,omitempty"`
// Contains the HTTP headers that were sent with this request.
RequestHeaders map[string]string `json:"request_headers,omitempty"`
// The type of the data in [Data].
Type EventDataType `json:"type,omitempty"`
// The data that's submitted with the request.
Data T `json:"data,omitempty"`
}
// Context represents the passed `context` object of an e5e function.
type Context[T Data] struct {
// Set to true if the event was triggered in an asynchronous way,
// meaning that the event trigger does not wait for the return of the
// function execution.
Async bool `json:"async,omitempty"`
// The time the event was triggered.
Date string `json:"date,omitempty"`
// The kind of trigger that triggered the execution.
// Fallback is `generic`, if the trigger is unknown.
Type string `json:"type,omitempty"`
// Additional data about the context.
Data T `json:"data,omitempty"`
}
// Request contains the whole request information.
type Request[T, TContext Data] struct {
Context Context[TContext] `json:"context"`
Event Event[T] `json:"event"`
}
// Data provides a shortcut to the Data of the inner [Event].
func (r Request[T, TContext]) Data() T { return r.Event.Data }
// Result represents the function result value passed back to E5E.
type Result struct {
Status int `json:"status,omitempty"`
ResponseHeaders map[string]string `json:"response_headers,omitempty"`
Data any `json:"data"`
Type ResultDataType `json:"type,omitempty"`
}
// ResultDataType tells more information about the type of the data inside a [Result].
type ResultDataType string
const (
// ResultDataTypeText tells E5E that the data is a plain string.
//
// Equivalent to the `text/plain` content type.
ResultDataTypeText ResultDataType = "text"
// ResultDataTypeObject tells E5E that the data is a JSON object.
//
// The data is either a primitive data type (bool, int) or contains structured
// data like a struct or a map.
//
// Equivalent to the `application/*` content type.
ResultDataTypeObject ResultDataType = "object"
// ResultDataTypeBinary tells E5E that the data is a base64 encoded string.
//
// The data contains a binary object representation of the response body.
ResultDataTypeBinary ResultDataType = "binary"
)
// LibraryVersion represents the implemented custom binary interface version.
//
//goland:noinspection GoUnusedConst
const LibraryVersion = "2.0.0"
// options contains all the runtime options that determine the behaviour of the [mux].
// It is usually read at runtime using [parseArguments], but can be overridden for testing.
type options struct {
// The name of the entrypoint that is executed on incoming events.
Entrypoint string
// The termination sequence that should be written on shutdown.
DaemonExecutionTerminationSequence string
// The execution sequence that separates generic output on [os.Stdout] from the encoded responses.
StdoutExecutionSequence string
// If set to true, the application is kept alive after the first execution and responds to ping events.
KeepAlive bool
}