Skip to content
Permalink
Browse files

FaaS chain as a function

Signed-off-by: s8sg <swarvanusg@gmail.com>
  • Loading branch information...
s8sg committed Aug 2, 2018
1 parent 1fe3c39 commit 41bf79f092b490cc1e7c8b80d94a88f6f0d48b76
Showing with 1,644 additions and 53,761 deletions.
  1. +0 −2 .gitignore
  2. +3 −6 Makefile
  3. +47 −57 chain.go
  4. +0 −150 faaschain/handler.go
  5. +0 −43 faaschain/jsexecutor.go
  6. +0 −43 faaschain/jsexecutor_test.go
  7. +0 −5 faaschain/vendor/github.com/robertkrimen/otto/.gitignore
  8. +0 −1 faaschain/vendor/github.com/robertkrimen/otto/DESIGN.markdown
  9. +0 −7 faaschain/vendor/github.com/robertkrimen/otto/LICENSE
  10. +0 −63 faaschain/vendor/github.com/robertkrimen/otto/Makefile
  11. +0 −871 faaschain/vendor/github.com/robertkrimen/otto/README.markdown
  12. +0 −720 faaschain/vendor/github.com/robertkrimen/otto/array_test.go
  13. +0 −1,068 faaschain/vendor/github.com/robertkrimen/otto/ast/README.markdown
  14. +0 −278 faaschain/vendor/github.com/robertkrimen/otto/ast/comments.go
  15. +0 −61 faaschain/vendor/github.com/robertkrimen/otto/ast/comments_test.go
  16. +0 −515 faaschain/vendor/github.com/robertkrimen/otto/ast/node.go
  17. +0 −217 faaschain/vendor/github.com/robertkrimen/otto/ast/walk.go
  18. +0 −52 faaschain/vendor/github.com/robertkrimen/otto/ast/walk_example_test.go
  19. +0 −82 faaschain/vendor/github.com/robertkrimen/otto/ast/walk_test.go
  20. +0 −695 faaschain/vendor/github.com/robertkrimen/otto/bug_test.go
  21. +0 −354 faaschain/vendor/github.com/robertkrimen/otto/builtin.go
  22. +0 −684 faaschain/vendor/github.com/robertkrimen/otto/builtin_array.go
  23. +0 −28 faaschain/vendor/github.com/robertkrimen/otto/builtin_boolean.go
  24. +0 −615 faaschain/vendor/github.com/robertkrimen/otto/builtin_date.go
  25. +0 −126 faaschain/vendor/github.com/robertkrimen/otto/builtin_error.go
  26. +0 −129 faaschain/vendor/github.com/robertkrimen/otto/builtin_function.go
  27. +0 −299 faaschain/vendor/github.com/robertkrimen/otto/builtin_json.go
  28. +0 −151 faaschain/vendor/github.com/robertkrimen/otto/builtin_math.go
  29. +0 −93 faaschain/vendor/github.com/robertkrimen/otto/builtin_number.go
  30. +0 −289 faaschain/vendor/github.com/robertkrimen/otto/builtin_object.go
  31. +0 −65 faaschain/vendor/github.com/robertkrimen/otto/builtin_regexp.go
  32. +0 −500 faaschain/vendor/github.com/robertkrimen/otto/builtin_string.go
  33. +0 −136 faaschain/vendor/github.com/robertkrimen/otto/builtin_test.go
  34. +0 −1,512 faaschain/vendor/github.com/robertkrimen/otto/call_test.go
  35. +0 −173 faaschain/vendor/github.com/robertkrimen/otto/clone.go
  36. +0 −18 faaschain/vendor/github.com/robertkrimen/otto/clone_test.go
  37. +0 −24 faaschain/vendor/github.com/robertkrimen/otto/cmpl.go
  38. +0 −96 faaschain/vendor/github.com/robertkrimen/otto/cmpl_evaluate.go
  39. +0 −460 faaschain/vendor/github.com/robertkrimen/otto/cmpl_evaluate_expression.go
  40. +0 −424 faaschain/vendor/github.com/robertkrimen/otto/cmpl_evaluate_statement.go
  41. +0 −656 faaschain/vendor/github.com/robertkrimen/otto/cmpl_parse.go
  42. +0 −54 faaschain/vendor/github.com/robertkrimen/otto/cmpl_test.go
  43. +0 −51 faaschain/vendor/github.com/robertkrimen/otto/console.go
  44. +0 −481 faaschain/vendor/github.com/robertkrimen/otto/date_test.go
  45. +0 −9 faaschain/vendor/github.com/robertkrimen/otto/dbg.go
  46. +0 −387 faaschain/vendor/github.com/robertkrimen/otto/dbg/dbg.go
  47. +0 −95 faaschain/vendor/github.com/robertkrimen/otto/documentation_test.go
  48. +0 −253 faaschain/vendor/github.com/robertkrimen/otto/error.go
  49. +0 −39 faaschain/vendor/github.com/robertkrimen/otto/error_native_test.go
  50. +0 −479 faaschain/vendor/github.com/robertkrimen/otto/error_test.go
  51. +0 −318 faaschain/vendor/github.com/robertkrimen/otto/evaluate.go
  52. +0 −110 faaschain/vendor/github.com/robertkrimen/otto/file/README.markdown
  53. +0 −164 faaschain/vendor/github.com/robertkrimen/otto/file/file.go
  54. +0 −55 faaschain/vendor/github.com/robertkrimen/otto/function_stack_test.go
  55. +0 −345 faaschain/vendor/github.com/robertkrimen/otto/function_test.go
  56. +0 −726 faaschain/vendor/github.com/robertkrimen/otto/functional_benchmark_test.go
  57. +0 −221 faaschain/vendor/github.com/robertkrimen/otto/global.go
  58. +0 −355 faaschain/vendor/github.com/robertkrimen/otto/global_test.go
  59. +0 −6,649 faaschain/vendor/github.com/robertkrimen/otto/inline.go
  60. +0 −1,086 faaschain/vendor/github.com/robertkrimen/otto/inline.pl
  61. +0 −183 faaschain/vendor/github.com/robertkrimen/otto/json_test.go
  62. +0 −303 faaschain/vendor/github.com/robertkrimen/otto/math_test.go
  63. +0 −111 faaschain/vendor/github.com/robertkrimen/otto/native_stack_test.go
  64. +0 −165 faaschain/vendor/github.com/robertkrimen/otto/number_test.go
  65. +0 −156 faaschain/vendor/github.com/robertkrimen/otto/object.go
  66. +0 −493 faaschain/vendor/github.com/robertkrimen/otto/object_class.go
  67. +0 −639 faaschain/vendor/github.com/robertkrimen/otto/object_test.go
  68. +0 −770 faaschain/vendor/github.com/robertkrimen/otto/otto.go
  69. +0 −4 faaschain/vendor/github.com/robertkrimen/otto/otto/Makefile
  70. +0 −48 faaschain/vendor/github.com/robertkrimen/otto/otto/main.go
  71. +0 −178 faaschain/vendor/github.com/robertkrimen/otto/otto_.go
  72. +0 −48 faaschain/vendor/github.com/robertkrimen/otto/otto_error_test.go
  73. +0 −1,749 faaschain/vendor/github.com/robertkrimen/otto/otto_test.go
  74. +0 −40 faaschain/vendor/github.com/robertkrimen/otto/panic_test.go
  75. +0 −4 faaschain/vendor/github.com/robertkrimen/otto/parser/Makefile
  76. +0 −190 faaschain/vendor/github.com/robertkrimen/otto/parser/README.markdown
  77. +0 −1,450 faaschain/vendor/github.com/robertkrimen/otto/parser/comments_test.go
  78. +0 −9 faaschain/vendor/github.com/robertkrimen/otto/parser/dbg.go
  79. +0 −175 faaschain/vendor/github.com/robertkrimen/otto/parser/error.go
  80. +0 −1,005 faaschain/vendor/github.com/robertkrimen/otto/parser/expression.go
  81. +0 −866 faaschain/vendor/github.com/robertkrimen/otto/parser/lexer.go
  82. +0 −386 faaschain/vendor/github.com/robertkrimen/otto/parser/lexer_test.go
  83. +0 −930 faaschain/vendor/github.com/robertkrimen/otto/parser/marshal_test.go
  84. +0 −344 faaschain/vendor/github.com/robertkrimen/otto/parser/parser.go
  85. +0 −1,038 faaschain/vendor/github.com/robertkrimen/otto/parser/parser_test.go
  86. +0 −358 faaschain/vendor/github.com/robertkrimen/otto/parser/regexp.go
  87. +0 −149 faaschain/vendor/github.com/robertkrimen/otto/parser/regexp_test.go
  88. +0 −44 faaschain/vendor/github.com/robertkrimen/otto/parser/scope.go
  89. +0 −940 faaschain/vendor/github.com/robertkrimen/otto/parser/statement.go
  90. +0 −42 faaschain/vendor/github.com/robertkrimen/otto/parser_test.go
  91. +0 −220 faaschain/vendor/github.com/robertkrimen/otto/property.go
  92. +0 −882 faaschain/vendor/github.com/robertkrimen/otto/reflect_test.go
  93. +0 −290 faaschain/vendor/github.com/robertkrimen/otto/regexp_test.go
  94. +0 −51 faaschain/vendor/github.com/robertkrimen/otto/registry/README.markdown
  95. +0 −47 faaschain/vendor/github.com/robertkrimen/otto/registry/registry.go
  96. +0 −57 faaschain/vendor/github.com/robertkrimen/otto/repl/autocompleter.go
  97. +0 −149 faaschain/vendor/github.com/robertkrimen/otto/repl/repl.go
  98. +0 −30 faaschain/vendor/github.com/robertkrimen/otto/result.go
  99. +0 −807 faaschain/vendor/github.com/robertkrimen/otto/runtime.go
  100. +0 −859 faaschain/vendor/github.com/robertkrimen/otto/runtime_test.go
  101. +0 −35 faaschain/vendor/github.com/robertkrimen/otto/scope.go
  102. +0 −119 faaschain/vendor/github.com/robertkrimen/otto/script.go
  103. +0 −94 faaschain/vendor/github.com/robertkrimen/otto/script_test.go
  104. +0 −163 faaschain/vendor/github.com/robertkrimen/otto/sourcemap_test.go
  105. +0 −296 faaschain/vendor/github.com/robertkrimen/otto/stash.go
  106. +0 −422 faaschain/vendor/github.com/robertkrimen/otto/string_test.go
  107. +0 −669 faaschain/vendor/github.com/robertkrimen/otto/terst/terst.go
  108. +0 −26 faaschain/vendor/github.com/robertkrimen/otto/test/Makefile
  109. +0 −196 faaschain/vendor/github.com/robertkrimen/otto/test/tester.go
  110. +0 −136 faaschain/vendor/github.com/robertkrimen/otto/testing_test.go
  111. +0 −2 faaschain/vendor/github.com/robertkrimen/otto/token/Makefile
  112. +0 −171 faaschain/vendor/github.com/robertkrimen/otto/token/README.markdown
  113. +0 −116 faaschain/vendor/github.com/robertkrimen/otto/token/token.go
  114. +0 −349 faaschain/vendor/github.com/robertkrimen/otto/token/token_const.go
  115. +0 −222 faaschain/vendor/github.com/robertkrimen/otto/token/tokenfmt
  116. +0 −106 faaschain/vendor/github.com/robertkrimen/otto/type_arguments.go
  117. +0 −109 faaschain/vendor/github.com/robertkrimen/otto/type_array.go
  118. +0 −13 faaschain/vendor/github.com/robertkrimen/otto/type_boolean.go
  119. +0 −299 faaschain/vendor/github.com/robertkrimen/otto/type_date.go
  120. +0 −24 faaschain/vendor/github.com/robertkrimen/otto/type_error.go
  121. +0 −340 faaschain/vendor/github.com/robertkrimen/otto/type_function.go
  122. +0 −134 faaschain/vendor/github.com/robertkrimen/otto/type_go_array.go
  123. +0 −95 faaschain/vendor/github.com/robertkrimen/otto/type_go_map.go
  124. +0 −40 faaschain/vendor/github.com/robertkrimen/otto/type_go_map_test.go
  125. +0 −126 faaschain/vendor/github.com/robertkrimen/otto/type_go_slice.go
  126. +0 −23 faaschain/vendor/github.com/robertkrimen/otto/type_go_slice_test.go
  127. +0 −146 faaschain/vendor/github.com/robertkrimen/otto/type_go_struct.go
  128. +0 −5 faaschain/vendor/github.com/robertkrimen/otto/type_number.go
  129. +0 −103 faaschain/vendor/github.com/robertkrimen/otto/type_reference.go
  130. +0 −146 faaschain/vendor/github.com/robertkrimen/otto/type_regexp.go
  131. +0 −112 faaschain/vendor/github.com/robertkrimen/otto/type_string.go
  132. +0 −11 faaschain/vendor/github.com/robertkrimen/otto/underscore/Makefile
  133. +0 −53 faaschain/vendor/github.com/robertkrimen/otto/underscore/README.markdown
  134. +0 −3,463 faaschain/vendor/github.com/robertkrimen/otto/underscore/source.go
  135. +0 −84 faaschain/vendor/github.com/robertkrimen/otto/underscore/testify
  136. +0 −49 faaschain/vendor/github.com/robertkrimen/otto/underscore/underscore.go
  137. +0 −344 faaschain/vendor/github.com/robertkrimen/otto/underscore_arrays_test.go
  138. +0 −95 faaschain/vendor/github.com/robertkrimen/otto/underscore_chaining_test.go
  139. +0 −698 faaschain/vendor/github.com/robertkrimen/otto/underscore_collections_test.go
  140. +0 −208 faaschain/vendor/github.com/robertkrimen/otto/underscore_functions_test.go
  141. +0 −826 faaschain/vendor/github.com/robertkrimen/otto/underscore_objects_test.go
  142. +0 −165 faaschain/vendor/github.com/robertkrimen/otto/underscore_test.go
  143. +0 −419 faaschain/vendor/github.com/robertkrimen/otto/underscore_utility_test.go
  144. +0 −1,033 faaschain/vendor/github.com/robertkrimen/otto/value.go
  145. +0 −43 faaschain/vendor/github.com/robertkrimen/otto/value_boolean.go
  146. +0 −324 faaschain/vendor/github.com/robertkrimen/otto/value_number.go
  147. +0 −23 faaschain/vendor/github.com/robertkrimen/otto/value_primitive.go
  148. +0 −103 faaschain/vendor/github.com/robertkrimen/otto/value_string.go
  149. +0 −299 faaschain/vendor/github.com/robertkrimen/otto/value_test.go
  150. +0 −16 faaschain/vendor/gopkg.in/sourcemap.v1/.travis.yml
  151. +0 −25 faaschain/vendor/gopkg.in/sourcemap.v1/LICENSE
  152. +0 −4 faaschain/vendor/gopkg.in/sourcemap.v1/Makefile
  153. +0 −35 faaschain/vendor/gopkg.in/sourcemap.v1/README.md
  154. +0 −92 faaschain/vendor/gopkg.in/sourcemap.v1/base64vlq/base64_vlq.go
  155. +0 −63 faaschain/vendor/gopkg.in/sourcemap.v1/base64vlq/base64_vlq_test.go
  156. +0 −30 faaschain/vendor/gopkg.in/sourcemap.v1/bench_test.go
  157. +0 −134 faaschain/vendor/gopkg.in/sourcemap.v1/consumer.go
  158. +0 −219 faaschain/vendor/gopkg.in/sourcemap.v1/consumer_test.go
  159. +0 −33 faaschain/vendor/gopkg.in/sourcemap.v1/example_test.go
  160. +0 −157 faaschain/vendor/gopkg.in/sourcemap.v1/sourcemap.go
  161. +6 −1 sdk/chain.go
  162. +11 −0 sdk/function.go
  163. +12 −2 sdk/request.go
  164. +0 −16 stack.yml
  165. +41 −0 template/faaschain/Dockerfile
  166. +16 −0 template/faaschain/function/handler.go
  167. +259 −0 template/faaschain/main.go
  168. +2 −0 template/faaschain/template.yml
  169. +27 −0 template/faaschain/vendor/github.com/rs/xid/.appveyor.yml
  170. +8 −0 template/faaschain/vendor/github.com/rs/xid/.travis.yml
  171. +19 −0 template/faaschain/vendor/github.com/rs/xid/LICENSE
  172. +112 −0 template/faaschain/vendor/github.com/rs/xid/README.md
  173. +1 −0 template/faaschain/vendor/github.com/rs/xid/go.mod
  174. +9 −0 template/faaschain/vendor/github.com/rs/xid/hostid_darwin.go
  175. +9 −0 template/faaschain/vendor/github.com/rs/xid/hostid_fallback.go
  176. +9 −0 template/faaschain/vendor/github.com/rs/xid/hostid_freebsd.go
  177. +10 −0 template/faaschain/vendor/github.com/rs/xid/hostid_linux.go
  178. +39 −0 template/faaschain/vendor/github.com/rs/xid/hostid_windows.go
  179. +365 −0 template/faaschain/vendor/github.com/rs/xid/id.go
  180. +396 −0 template/faaschain/vendor/github.com/rs/xid/id_test.go
  181. +139 −0 template/faaschain/vendor/github.com/s8sg/faaschain/chain.go
  182. +75 −0 template/faaschain/vendor/github.com/s8sg/faaschain/chain_test.go
  183. +6 −1 { → template}/faaschain/vendor/github.com/s8sg/faaschain/sdk/chain.go
  184. +11 −0 { → template}/faaschain/vendor/github.com/s8sg/faaschain/sdk/function.go
  185. 0 { → template}/faaschain/vendor/github.com/s8sg/faaschain/sdk/phase.go
  186. +12 −2 { → template}/faaschain/vendor/github.com/s8sg/faaschain/sdk/request.go
@@ -1,2 +0,0 @@
template
build
@@ -1,13 +1,10 @@
.PHONY: build

all: build-lib build-func deploy
all: build-lib build-template


build-lib:
./buildlib.sh

build-func:
faas-cli build -f stack.yml

deploy:
faas-cli deploy -f stack.yml
build-template:
docker build -t faaschain:test template/faaschain
104 chain.go
@@ -1,33 +1,55 @@
package faaschain

import (
"bytes"
"context"
"fmt"
// "bytes"
// "context"
// "fmt"
"github.com/s8sg/faaschain/sdk"
"io"
"io/ioutil"
"net/http"
// "io"
// "io/ioutil"
// "net/http"
"net/url"
"path"
)

type Faaschain struct {
type Fchain struct {
chain *sdk.Chain
id string
url string
asyncUrl string
chainDef []byte
}

func NewFaaschain(faasurl string) *Faaschain {
fchain := &Faaschain{}
func NewFaaschain(faasurl string) *Fchain {
fchain := &Fchain{}
fchain.chain = sdk.CreateChain()
u, _ := url.Parse(faasurl)
u.Path = path.Join(u.Path, "function/faaschain")
fchain.url = u.String()
u, _ = url.Parse(faasurl)
u.Path = path.Join(u.Path, "async-function/faaschain")
fchain.asyncUrl = u.String()
return fchain
}

func (fchain *Faaschain) ApplyFunction(function *sdk.Function) *Faaschain {
func (fchain *Fchain) SetId(id string) {
fchain.id = id
}

func (fchain *Fchain) ApplyModifier(mod sdk.Modifier) *Fchain {
var phase *sdk.Phase
newfunc := sdk.CreateModifier(mod)
if len(fchain.chain.Phases) == 0 {
phase = sdk.CreateExecutionPhase()
fchain.chain.AddPhase(phase)
} else {
phase = fchain.chain.GetLastPhase()
}
phase.AddFunction(newfunc)
return fchain
}

func (fchain *Fchain) ApplyFunction(function *sdk.Function) *Fchain {
var phase *sdk.Phase
if len(fchain.chain.Phases) == 0 {
phase = sdk.CreateExecutionPhase()
@@ -39,7 +61,7 @@ func (fchain *Faaschain) ApplyFunction(function *sdk.Function) *Faaschain {
return fchain
}

func (fchain *Faaschain) Apply(function string, header map[string]string, param map[string][]string) *Faaschain {
func (fchain *Fchain) Apply(function string, header map[string]string, param map[string][]string) *Fchain {
newfunc := sdk.CreateFunction(function)
if header != nil {
for key, value := range header {
@@ -64,14 +86,14 @@ func (fchain *Faaschain) Apply(function string, header map[string]string, param
return fchain
}

func (fchain *Faaschain) ApplyFunctionAsync(function *sdk.Function) *Faaschain {
func (fchain *Fchain) ApplyFunctionAsync(function *sdk.Function) *Fchain {
phase := sdk.CreateExecutionPhase()
fchain.chain.AddPhase(phase)
phase.AddFunction(function)
return fchain
}

func (fchain *Faaschain) ApplyAsync(function string, header map[string]string, param map[string][]string) *Faaschain {
func (fchain *Fchain) ApplyAsync(function string, header map[string]string, param map[string][]string) *Fchain {
newfunc := sdk.CreateFunction(function)
if header != nil {
for key, value := range header {
@@ -91,59 +113,27 @@ func (fchain *Faaschain) ApplyAsync(function string, header map[string]string, p
return fchain
}

func (fchain *Faaschain) Build() (err error) {
func (fchain *Fchain) Build() (err error) {
fchain.chainDef, err = fchain.chain.Encode()
return err
}

func (fchain *Faaschain) GetDefinition() string {
func (fchain *Fchain) GetDefinition() string {
return string(fchain.chainDef)
}

func buildUpstreamRequest(url string, chaindef string, data []byte) (*http.Request, error) {
request := sdk.BuildRequest(chaindef, data)

data, err := request.Encode()
if err != nil {
return nil, err
}
req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(data))
if err != nil {
return nil, err
}
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")

return req, nil
func (fchain *Fchain) GetChain() *sdk.Chain {
return fchain.chain
}

func (fchain *Faaschain) Invoke(ctx context.Context, reader io.Reader) (io.ReadCloser, error) {
var res *http.Response
var resErr error

data, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}

url := fchain.url
client := &http.Client{}
upstreamReq, err := buildUpstreamRequest(url, string(fchain.chainDef), data)
if err != nil {
return nil, err
}
func (fchain *Fchain) GetId() string {
return fchain.id
}

if ctx != nil {
res, resErr = client.Do(upstreamReq.WithContext(ctx))
} else {
res, resErr = client.Do(upstreamReq)
}
if resErr != nil {
return nil, resErr
}
func (fchain *Fchain) GetUrl() string {
return fchain.url
}

if res.StatusCode < 200 || res.StatusCode > 299 {
return nil, fmt.Errorf("status code: %d", res.StatusCode)
}
return res.Body, nil
func (fchain *Fchain) GetAsyncUrl() string {
return fchain.asyncUrl
}

This file was deleted.

This file was deleted.

0 comments on commit 41bf79f

Please sign in to comment.
You can’t perform that action at this time.