-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #124 from bots-garden/122-create-a-response-structure
📝 update start changes with Response, Request
- Loading branch information
Showing
88 changed files
with
470 additions
and
377 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,122 +1,133 @@ | ||
package capsulehttp | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/bots-garden/capsule/capsule-launcher/hostfunctions" | ||
"github.com/bots-garden/capsule/capsule-launcher/services/wasmrt" | ||
"github.com/bots-garden/capsule/commons" | ||
"github.com/gin-gonic/gin" | ||
"github.com/shirou/gopsutil/v3/mem" | ||
"net/http" | ||
"encoding/json" | ||
"fmt" | ||
"github.com/bots-garden/capsule/capsule-launcher/hostfunctions" | ||
"github.com/bots-garden/capsule/capsule-launcher/services/wasmrt" | ||
"github.com/bots-garden/capsule/commons" | ||
"github.com/gin-gonic/gin" | ||
"github.com/shirou/gopsutil/v3/mem" | ||
"net/http" | ||
) | ||
|
||
func Serve(httpPort string, wasmFile []byte, crt, key string) { | ||
//fmt.Println("👋[checking]capsulehttp.Serve") | ||
//fmt.Println("👋[checking]capsulehttp.Serve") | ||
|
||
hostfunctions.HostInformation = `{"httpPort":` + httpPort + `}` | ||
hostfunctions.HostInformation = `{"httpPort":` + httpPort + `}` | ||
|
||
v, _ := mem.VirtualMemory() | ||
v, _ := mem.VirtualMemory() | ||
|
||
if commons.GetEnv("DEBUG", "false") == "false" { | ||
gin.SetMode(gin.ReleaseMode) | ||
} else { | ||
gin.SetMode(gin.DebugMode) | ||
} | ||
if commons.GetEnv("DEBUG", "false") == "false" { | ||
gin.SetMode(gin.ReleaseMode) | ||
} else { | ||
gin.SetMode(gin.DebugMode) | ||
} | ||
|
||
router := gin.New() | ||
router := gin.New() | ||
|
||
router.GET("/host-metrics", func(c *gin.Context) { | ||
jsonMap := make(map[string]interface{}) | ||
json.Unmarshal([]byte(v.String()), &jsonMap) | ||
c.JSON(http.StatusOK, jsonMap) | ||
}) | ||
router.GET("/host-metrics", func(c *gin.Context) { | ||
jsonMap := make(map[string]interface{}) | ||
json.Unmarshal([]byte(v.String()), &jsonMap) | ||
c.JSON(http.StatusOK, jsonMap) | ||
}) | ||
|
||
router.GET("/health", func(c *gin.Context) { | ||
c.String(http.StatusOK, "OK") | ||
}) | ||
router.GET("/health", func(c *gin.Context) { | ||
c.String(http.StatusOK, "OK") | ||
}) | ||
|
||
//TODO: be able to get the query string from the wasm module | ||
// we need to be able to return json, html, txt | ||
router.GET("/", func(c *gin.Context) { | ||
wasmRuntime, wasmModule, wasmFunction, ctx := capsule.GetNewWasmRuntimeForHttp(wasmFile) | ||
defer wasmRuntime.Close(ctx) | ||
router.GET("/", func(c *gin.Context) { | ||
|
||
query := "empty" //🚧 | ||
queryPos, queryLen, free, err := capsule.ReserveMemorySpaceFor(query, wasmModule, ctx) | ||
defer free.Call(ctx, queryPos) | ||
jsonStr, _ := GetJsonStringFromPayloadRequest(c) | ||
headersStr := GetHeadersStringFromHeadersRequest(c) | ||
uri := c.Request.RequestURI | ||
method := c.Request.Method | ||
|
||
headersStr := GetHeadersStringFromHeadersRequest(c) | ||
headersStrPos, headersStrLen, free, err := capsule.ReserveMemorySpaceFor(headersStr, wasmModule, ctx) | ||
defer free.Call(ctx, headersStrPos) | ||
wasmRuntime, wasmModule, wasmFunction, ctx := capsule.GetNewWasmRuntimeForHttp(wasmFile) | ||
defer wasmRuntime.Close(ctx) | ||
|
||
bytes, err := capsule.ExecHandleFunction(wasmFunction, wasmModule, ctx, queryPos, queryLen, headersStrPos, headersStrLen) | ||
if err != nil { | ||
c.String(500, "out of range of memory size") | ||
} | ||
bodyStr, headers := GetBodyAndHeaders(bytes, c) | ||
uriPos, uriLen, free, err := capsule.ReserveMemorySpaceFor(uri, wasmModule, ctx) | ||
defer free.Call(ctx, uriPos) | ||
|
||
// check the return value | ||
if commons.IsErrorString(bodyStr) { | ||
SendErrorMessage(bodyStr, headers, c) | ||
} else if IsBodyString(bodyStr) { | ||
SendBodyMessage(bodyStr, headers, c) | ||
} else { | ||
c.String(http.StatusOK, bodyStr) | ||
} | ||
jsonStrPos, jsonStrLen, free, err := capsule.ReserveMemorySpaceFor(jsonStr, wasmModule, ctx) | ||
defer free.Call(ctx, jsonStrPos) | ||
|
||
}) | ||
headersStrPos, headersStrLen, free, err := capsule.ReserveMemorySpaceFor(headersStr, wasmModule, ctx) | ||
defer free.Call(ctx, headersStrPos) | ||
|
||
router.POST("/", func(c *gin.Context) { | ||
methodPos, methodLen, free, err := capsule.ReserveMemorySpaceFor(method, wasmModule, ctx) | ||
defer free.Call(ctx, methodPos) | ||
|
||
// Parameters "setup" | ||
jsonStr, _ := GetJsonStringFromPayloadRequest(c) | ||
headersStr := GetHeadersStringFromHeadersRequest(c) | ||
bytes, err := capsule.ExecHandleFunction(wasmFunction, wasmModule, ctx, jsonStrPos, jsonStrLen, uriPos, uriLen, headersStrPos, headersStrLen, methodPos, methodLen) | ||
if err != nil { | ||
c.String(500, "out of range of memory size") | ||
} | ||
bodyStr, headers := GetBodyAndHeaders(bytes, c) | ||
|
||
//time.Sleep(500 * time.Millisecond) | ||
// check the return value | ||
if commons.IsErrorString(bodyStr) { | ||
SendErrorMessage(bodyStr, headers, c) | ||
} else if IsBodyString(bodyStr) { | ||
SendBodyMessage(bodyStr, headers, c) | ||
} else { | ||
c.String(http.StatusOK, bodyStr) | ||
} | ||
|
||
wasmRuntime, wasmModule, wasmFunction, ctx := capsule.GetNewWasmRuntimeForHttp(wasmFile) | ||
}) | ||
|
||
defer wasmRuntime.Close(ctx) | ||
router.POST("/", func(c *gin.Context) { | ||
|
||
jsonStrPos, jsonStrLen, free, err := capsule.ReserveMemorySpaceFor(jsonStr, wasmModule, ctx) | ||
// Parameters "setup" | ||
jsonStr, _ := GetJsonStringFromPayloadRequest(c) | ||
headersStr := GetHeadersStringFromHeadersRequest(c) | ||
uri := c.Request.RequestURI | ||
method := c.Request.Method | ||
|
||
defer free.Call(ctx, jsonStrPos) | ||
wasmRuntime, wasmModule, wasmFunction, ctx := capsule.GetNewWasmRuntimeForHttp(wasmFile) | ||
defer wasmRuntime.Close(ctx) | ||
|
||
headersStrPos, headersStrLen, free, err := capsule.ReserveMemorySpaceFor(headersStr, wasmModule, ctx) | ||
uriPos, uriLen, free, err := capsule.ReserveMemorySpaceFor(uri, wasmModule, ctx) | ||
defer free.Call(ctx, uriPos) | ||
|
||
defer free.Call(ctx, headersStrPos) | ||
jsonStrPos, jsonStrLen, free, err := capsule.ReserveMemorySpaceFor(jsonStr, wasmModule, ctx) | ||
defer free.Call(ctx, jsonStrPos) | ||
|
||
bytes, err := capsule.ExecHandleFunction(wasmFunction, wasmModule, ctx, jsonStrPos, jsonStrLen, headersStrPos, headersStrLen) | ||
headersStrPos, headersStrLen, free, err := capsule.ReserveMemorySpaceFor(headersStr, wasmModule, ctx) | ||
defer free.Call(ctx, headersStrPos) | ||
|
||
if err != nil { | ||
c.String(500, "out of range of memory size") | ||
} | ||
methodPos, methodLen, free, err := capsule.ReserveMemorySpaceFor(method, wasmModule, ctx) | ||
defer free.Call(ctx, methodPos) | ||
|
||
bodyStr, headers := GetBodyAndHeaders(bytes, c) | ||
bytes, err := capsule.ExecHandleFunction(wasmFunction, wasmModule, ctx, jsonStrPos, jsonStrLen, uriPos, uriLen, headersStrPos, headersStrLen, methodPos, methodLen) | ||
|
||
// check the return value | ||
if commons.IsErrorString(bodyStr) { | ||
SendErrorMessage(bodyStr, headers, c) | ||
} else if IsBodyString(bodyStr) { | ||
SendJsonMessage(bodyStr, headers, c) | ||
} else { | ||
c.String(http.StatusOK, bodyStr) | ||
} | ||
if err != nil { | ||
c.String(500, "out of range of memory size") | ||
} | ||
|
||
//c.String(http.StatusOK, bodyStr) | ||
bodyStr, headers := GetBodyAndHeaders(bytes, c) | ||
|
||
}) | ||
// check the return value | ||
if commons.IsErrorString(bodyStr) { | ||
SendErrorMessage(bodyStr, headers, c) | ||
} else if IsBodyString(bodyStr) { | ||
SendJsonMessage(bodyStr, headers, c) | ||
} else { | ||
c.String(http.StatusOK, bodyStr) | ||
} | ||
|
||
if crt != "" { | ||
// certs/procyon-registry.local.crt | ||
// certs/procyon-registry.local.key | ||
fmt.Println("💊 Capsule (", commons.CapsuleVersion(), ") http server is listening on:", httpPort, "🔐🌍") | ||
//c.String(http.StatusOK, bodyStr) | ||
|
||
router.RunTLS(":"+httpPort, crt, key) | ||
} else { | ||
fmt.Println("💊 Capsule (", commons.CapsuleVersion(), ") http server is listening on:", httpPort, "🌍") | ||
router.Run(":" + httpPort) | ||
} | ||
}) | ||
|
||
if crt != "" { | ||
// certs/procyon-registry.local.crt | ||
// certs/procyon-registry.local.key | ||
fmt.Println("💊 Capsule (", commons.CapsuleVersion(), ") http server is listening on:", httpPort, "🔐🌍") | ||
|
||
router.RunTLS(":"+httpPort, crt, key) | ||
} else { | ||
fmt.Println("💊 Capsule (", commons.CapsuleVersion(), ") http server is listening on:", httpPort, "🌍") | ||
router.Run(":" + httpPort) | ||
} | ||
|
||
} |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +0,0 @@ | ||
github.com/bots-garden/capsule/commons v0.0.0-20220821060842-d1dc9f030021 h1:cBeILASaSUdrsImLP6wR6a747SHYBJqIwuADm2BMDO4= | ||
github.com/bots-garden/capsule/commons v0.0.0-20220821060842-d1dc9f030021/go.mod h1:5ctHSZAwy3GEi4tR9YKFTfWtBUXDcOMFHNiJYXbV61c= | ||
github.com/bots-garden/capsule/commons v0.0.0-20220903062354-1c48dd250b77 h1:CyywCzVyAuT7fHf5iHgjx0IqUvEVevtbVMwbSkS2VnA= | ||
github.com/bots-garden/capsule/commons v0.0.0-20220903062354-1c48dd250b77/go.mod h1:5ctHSZAwy3GEi4tR9YKFTfWtBUXDcOMFHNiJYXbV61c= | ||
Oops, something went wrong.