Skip to content

Commit 791526f

Browse files
committed
📦 release 🚀 v0.1.5 🦐
1 parent 6b0da1b commit 791526f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+346
-667
lines changed

.idea/webResources.xml

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+6-35
Original file line numberDiff line numberDiff line change
@@ -394,48 +394,16 @@ http://localhost:8888/functions/hola/orange
394394
> - *The `default` revision is the `default` version of the function (http://localhost:8888/functions/hola)*
395395
396396
To run the **Capsule Reverse Proxy**, run the below command:
397-
```bash
398-
./capsule-reverse-proxy \
399-
-config=./config.yaml \
400-
-httpPort=8888
401-
```
402-
> *You have to define a configuration yaml file.*
403397

404-
### Define the routes in a yaml file (static mode)
405-
406-
*config.yaml*
407-
```yaml
408-
hello:
409-
default:
410-
- http://localhost:9091
411-
- http://localhost:7071
412-
413-
hey:
414-
default:
415-
- http://localhost:9092
416-
417-
hola:
418-
default:
419-
- http://localhost:9093
420-
orange:
421-
- http://localhost:6061
422-
yellow:
423-
- http://localhost:6062
424-
```
425-
> *A revision can be a set of URLs. In this case, the Capsule reverse-proxy will use randomly one of the URLs.*
426-
427-
### Use the "in memory" dynamic mode of the reverse-proxy
428-
429-
With the Capsule Reverse Proxy, you gain an **API** that allows to define routes dynamically (in memory). You can keep the yaml config file (it is loaded in memory at startup).
430-
431-
To run **Capsule** as a reverse proxy, with the "in memory" dynamic mode, add this flag: `-backend="memory"`:
432398
```bash
433-
./capsule \
399+
./capsule-reverse-proxy \
434400
-config=./config.yaml \
435401
-backend="memory" \
436402
-httpPort=8888
437403
```
438404

405+
With the Capsule Reverse Proxy, you gain an **API** that allows to define routes dynamically (in memory).
406+
439407
#### Registration API
440408

441409
##### Register a function (add a new route to a function)
@@ -551,6 +519,9 @@ The routes list will look like that:
551519
}
552520
```
553521

522+
> *A revision can be a set of URLs. In this case, the Capsule reverse-proxy will use randomly one of the URLs.*
523+
524+
554525
##### Remove a URL from the function revision
555526

556527
```bash

capsule-reverse-proxy/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func main() {
2727
//flags
2828
httpPortPtr := flag.String("httpPort", "8080", "http port")
2929

30-
configPtr := flag.String("config", "", "config file (reverse proxy)")
30+
configPtr := flag.String("config", "", "config file (🚧 not implemented)")
3131
backendPtr := flag.String("backend", "memory", "backend for reverse proxy, registration, discovery")
3232

3333
crtPtr := flag.String("crt", "", "certificate")
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
package reverse_proxy
22

33
import (
4-
"fmt"
5-
"github.com/bots-garden/capsule/capsule-reverse-proxy/reverse-proxy/routes"
6-
"github.com/bots-garden/capsule/commons"
7-
"github.com/gin-gonic/gin"
8-
"gopkg.in/yaml.v3"
9-
"io/ioutil"
10-
"log"
11-
"net/http"
12-
"net/http/httputil"
13-
"net/url"
4+
"fmt"
5+
"github.com/bots-garden/capsule/capsule-reverse-proxy/reverse-proxy/routes"
6+
"github.com/bots-garden/capsule/commons"
7+
"github.com/gin-gonic/gin"
8+
"net/http"
9+
"net/http/httputil"
10+
"net/url"
1411
)
1512

1613
/*
@@ -25,165 +22,123 @@ func getEnv(key, fallback string) string {
2522
var lastUrlIndex = 0
2623

2724
func redirect(functionUrls []string, c *gin.Context) {
28-
//fmt.Println("🟢🖐", functionUrls)
29-
var functionUrl = ""
25+
var functionUrl = ""
3026

31-
if len(functionUrls) == 1 {
32-
functionUrl = functionUrls[0]
33-
} else {
34-
//TODO better repartition (load balancing) handling
35-
lastUrlIndex += 1
36-
if lastUrlIndex == len(functionUrls) {
37-
lastUrlIndex = 0
38-
}
27+
if len(functionUrls) == 1 {
28+
functionUrl = functionUrls[0]
29+
} else {
30+
//TODO better repartition (load balancing) handling
31+
lastUrlIndex += 1
32+
if lastUrlIndex == len(functionUrls) {
33+
lastUrlIndex = 0
34+
}
3935

40-
functionUrl = functionUrls[lastUrlIndex]
36+
functionUrl = functionUrls[lastUrlIndex]
4137

42-
//fmt.Println("🛑 kind of load balancing", lastUrlIndex)
43-
//fmt.Println("🌍", functionUrl)
44-
}
38+
}
4539

46-
remote, err := url.Parse(functionUrl)
40+
remote, err := url.Parse(functionUrl)
4741

48-
if err != nil {
49-
panic(err)
50-
}
42+
if err != nil {
43+
panic(err)
44+
}
5145

52-
proxy := httputil.NewSingleHostReverseProxy(remote)
46+
proxy := httputil.NewSingleHostReverseProxy(remote)
5347

54-
proxy.Director = func(req *http.Request) {
55-
req.Header = c.Request.Header
56-
req.Host = remote.Host
57-
req.URL.Scheme = remote.Scheme
58-
req.URL.Host = remote.Host
59-
req.URL.Path = c.Param("proxyPath")
48+
proxy.Director = func(req *http.Request) {
49+
req.Header = c.Request.Header
50+
req.Host = remote.Host
51+
req.URL.Scheme = remote.Scheme
52+
req.URL.Host = remote.Host
53+
req.URL.Path = c.Param("proxyPath")
6054

61-
//fmt.Println("🔴", c.Request.Header)
62-
}
55+
}
6356

64-
proxy.ServeHTTP(c.Writer, c.Request)
57+
proxy.ServeHTTP(c.Writer, c.Request)
6558
}
6659

67-
// 👀 See https://github.com/bots-garden/procyon/blob/main/procyon-reverse-proxy/main.go
6860
func proxy(c *gin.Context) {
6961

70-
functionName := c.Param("function_name")
71-
functionUrls := functions[functionName]["default"]
72-
73-
//fmt.Println("👋functionName", functionName)
74-
//fmt.Println("👋functionUrls", functionUrls)
62+
functionName := c.Param("function_name")
63+
functionUrls := functions[functionName]["default"]
7564

76-
if functionUrls != nil {
77-
/*
78-
2022/08/09 14:11:44 http: panic serving 127.0.0.1:65399:
79-
interface conversion: interface {} is []string, not []interface {}
65+
if functionUrls != nil {
66+
//redirect(functionUrls.([]interface{}), c)
67+
redirect(functionUrls.([]string), c)
8068

81-
*/
82-
83-
//redirect(functionUrls.([]interface{}), c)
84-
redirect(functionUrls.([]string), c)
85-
86-
} else {
87-
c.JSON(http.StatusInternalServerError, gin.H{"code": "ERROR", "message": "😢 Houston? We have a problem 🥵"})
88-
}
69+
} else {
70+
c.JSON(http.StatusInternalServerError, gin.H{"code": "ERROR", "message": "😢 Houston? We have a problem 🥵"})
71+
}
8972

9073
}
9174

9275
func proxyRevision(c *gin.Context) {
9376

94-
functionName := c.Param("function_name")
95-
functionRevision := c.Param("function_revision")
96-
functionUrls := functions[functionName][functionRevision]
77+
functionName := c.Param("function_name")
78+
functionRevision := c.Param("function_revision")
79+
functionUrls := functions[functionName][functionRevision]
9780

98-
if functionUrls != nil {
99-
redirect(functionUrls.([]string), c)
100-
} else {
101-
c.JSON(http.StatusInternalServerError, gin.H{"code": "ERROR", "message": "😢 Houston? We have a problem 🥵"})
102-
}
81+
if functionUrls != nil {
82+
redirect(functionUrls.([]string), c)
83+
} else {
84+
c.JSON(http.StatusInternalServerError, gin.H{"code": "ERROR", "message": "😢 Houston? We have a problem 🥵"})
85+
}
10386
}
10487

105-
var yamlConfig = make(map[interface{}]map[interface{}]map[interface{}]interface{})
88+
//var yamlConfig = make(map[interface{}]map[interface{}]map[interface{}]interface{})
10689
var functions = make(map[interface{}]map[interface{}]interface{})
107-
var filters = make(map[interface{}]map[interface{}]interface{})
90+
91+
//var filters = make(map[interface{}]map[interface{}]interface{})
10892

10993
func Serve(httpPort, config, backend, crt, key string) {
11094

111-
if config != "" {
112-
fmt.Println("📝 config file:", config)
113-
yamlFile, errFile := ioutil.ReadFile(config)
114-
if errFile != nil {
115-
log.Fatal(errFile)
116-
}
117-
118-
errYaml := yaml.Unmarshal(yamlFile, &yamlConfig)
119-
120-
if errYaml != nil {
121-
log.Fatal(errYaml)
122-
}
123-
124-
functions = yamlConfig["functions"]
125-
filters = yamlConfig["filters"]
126-
127-
// The code below is for testing
128-
/*
129-
functions["sandbox"] = map[interface{}]interface{}{"default": []string{"http://localhost:5050"}, "blue": []string{"http://localhost:5051"}}
130-
131-
functions["sandbox"]["green"] = []string{"http://localhost:5052"}
132-
133-
urlsList := functions["sandbox"]["green"].([]string)
134-
urlsList = append(urlsList, "http://localhost:5053")
135-
136-
functions["sandbox"]["green"] = urlsList
137-
*/
138-
}
139-
140-
switch backend {
141-
case "yaml":
142-
fmt.Println("👋 routes are defined in", config)
143-
case "memory":
144-
// it's possible to mix memory and yaml for the functions
145-
fmt.Println("👋 routes are defined in memory")
146-
case "redis":
147-
default:
148-
fmt.Println("👋 routes are defined in a Redis backend")
149-
//TODO check the environment variables
150-
fmt.Println("👋 use environment variable for the Redis configuration ")
151-
}
152-
153-
if commons.GetEnv("DEBUG", "false") == "false" {
154-
gin.SetMode(gin.ReleaseMode)
155-
} else {
156-
gin.SetMode(gin.DebugMode)
157-
}
158-
//router := gin.Default()
159-
router := gin.New()
160-
161-
router.NoRoute(func(c *gin.Context) {
162-
c.JSON(404, gin.H{"code": "PAGE_NOT_FOUND", "message": "😢 Page not found 🥵"})
163-
})
164-
165-
/*
166-
You need to use a header with this key: CAPSULE_REVERSE_PROXY_ADMIN_TOKEN
167-
*/
168-
reverseProxyAdminToken := commons.GetEnv("CAPSULE_REVERSE_PROXY_ADMIN_TOKEN", "")
169-
170-
reverse_proxy_memory_routes.DefineFunctionsRoutes(router, functions, reverseProxyAdminToken)
171-
reverse_proxy_memory_routes.DefineRevisionsRoutes(router, functions, reverseProxyAdminToken)
172-
reverse_proxy_memory_routes.DefineUrlsRoutes(router, functions, reverseProxyAdminToken)
173-
174-
// Call the functions
175-
router.Any("/functions/:function_name", proxy)
176-
router.Any("/functions/:function_name/:function_revision", proxyRevision)
177-
178-
if crt != "" {
179-
// certs/procyon-registry.local.crt
180-
// certs/procyon-registry.local.key
181-
fmt.Println("💊 Capsule (", commons.CapsuleVersion(), ") Reverse-Proxy is listening on:", httpPort, "🔐🌍")
182-
183-
router.RunTLS(":"+httpPort, crt, key)
184-
} else {
185-
fmt.Println("💊 Capsule (", commons.CapsuleVersion(), ") Reverse-Proxy is listening on:", httpPort, "🌍")
186-
router.Run(":" + httpPort)
187-
}
95+
switch backend {
96+
case "memory":
97+
// it's possible to mix memory and yaml for the functions
98+
fmt.Println("👋 routes are defined in memory")
99+
case "redis":
100+
fmt.Println("👋 Redis backend (🚧 not implemented)")
101+
fmt.Println("👋 routes are defined in a Redis backend")
102+
fmt.Println("👋 use environment variable for the Redis configuration ")
103+
default:
104+
fmt.Println("👋 routes are defined in a Redis backend")
105+
fmt.Println("👋 use environment variable for the Redis configuration ")
106+
}
107+
108+
if commons.GetEnv("DEBUG", "false") == "false" {
109+
gin.SetMode(gin.ReleaseMode)
110+
} else {
111+
gin.SetMode(gin.DebugMode)
112+
}
113+
//router := gin.Default()
114+
router := gin.New()
115+
116+
router.NoRoute(func(c *gin.Context) {
117+
c.JSON(404, gin.H{"code": "PAGE_NOT_FOUND", "message": "😢 Page not found 🥵"})
118+
})
119+
120+
/*
121+
You need to use a header with this key: CAPSULE_REVERSE_PROXY_ADMIN_TOKEN
122+
*/
123+
reverseProxyAdminToken := commons.GetEnv("CAPSULE_REVERSE_PROXY_ADMIN_TOKEN", "")
124+
125+
reverse_proxy_memory_routes.DefineFunctionsRoutes(router, functions, reverseProxyAdminToken)
126+
reverse_proxy_memory_routes.DefineRevisionsRoutes(router, functions, reverseProxyAdminToken)
127+
reverse_proxy_memory_routes.DefineUrlsRoutes(router, functions, reverseProxyAdminToken)
128+
129+
// Call the functions
130+
router.Any("/functions/:function_name", proxy)
131+
router.Any("/functions/:function_name/:function_revision", proxyRevision)
132+
133+
if crt != "" {
134+
// certs/procyon-registry.local.crt
135+
// certs/procyon-registry.local.key
136+
fmt.Println("💊 Capsule (", commons.CapsuleVersion(), ") Reverse-Proxy is listening on:", httpPort, "🔐🌍")
137+
138+
router.RunTLS(":"+httpPort, crt, key)
139+
} else {
140+
fmt.Println("💊 Capsule (", commons.CapsuleVersion(), ") Reverse-Proxy is listening on:", httpPort, "🌍")
141+
router.Run(":" + httpPort)
142+
}
188143

189144
}

wasm_modules/capsule-files/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
capsule
2+

wasm_modules/capsule-files/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ This wasm module is used by the `cli` mode
44

55
```bash
66
#!/bin/bash
7-
MESSAGE="🎉 Hello World" go run main.go \
8-
-wasm=../wasm_modules/capsule-function-template/hello.wasm \
7+
MESSAGE="🎉 Hello World" ./capsule \
8+
-wasm=./hello.wasm \
99
-mode=cli \
1010
"👋 hello world 🌍🎃" 1234 "Bob Morane"
1111

wasm_modules/capsule-files/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ module github.com/bots-garden/capsule/wasm_modules/capsule-files
22

33
go 1.18
44

5-
require github.com/bots-garden/capsule/capsulemodule v0.0.0-20220815080256-b30492298265
5+
require github.com/bots-garden/capsule/capsulemodule v0.0.0-20220815100856-6b0da1ba4aad
66

77
require github.com/bots-garden/capsule v0.0.0-20220815052111-84fb610d29f3 // indirect

wasm_modules/capsule-files/go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
github.com/bots-garden/capsule v0.0.0-20220815052111-84fb610d29f3 h1:jd6GhVtU6hJh2YhTOal46CxbSfGALQOw+QdwC4N/7eg=
22
github.com/bots-garden/capsule v0.0.0-20220815052111-84fb610d29f3/go.mod h1:PBhXY24j6vjuYghjEt+9mXzDb2ZwD9m9PAh9e6FTXck=
3-
github.com/bots-garden/capsule/capsulemodule v0.0.0-20220815080256-b30492298265 h1:dwYIdkYYkkxDubatShvKT+nGNKHEtIzTx2dhuuGY8jQ=
4-
github.com/bots-garden/capsule/capsulemodule v0.0.0-20220815080256-b30492298265/go.mod h1:tu9DYBPXGSfiDaCN1opAFZZ3FFt3TVFH5jkYnPypY64=
3+
github.com/bots-garden/capsule/capsulemodule v0.0.0-20220815100856-6b0da1ba4aad h1:DYsWXiMW++fY7MqJeUM2dGJRgyBTQSgWsRo2hy/1nMQ=
4+
github.com/bots-garden/capsule/capsulemodule v0.0.0-20220815100856-6b0da1ba4aad/go.mod h1:tu9DYBPXGSfiDaCN1opAFZZ3FFt3TVFH5jkYnPypY64=

0 commit comments

Comments
 (0)