Skip to content

Commit

Permalink
✨ create skill with morty controller
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvain-reynaud committed Apr 26, 2023
1 parent 4e9ff55 commit 90c6bf4
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 32 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ Flow without NLU:

```bash
# Example values
export POLYXIA_GATEWAY_MORTY_API_ENDPOINT="http://localhost:8081"
export POLYXIA_GATEWAY_NLU_API_ENDPOINT="http://localhost:8082"
export POLYXIA_GATEWAY_MORTYREGISTRYENDPOINT="http://localhost:8081"
export POLYXIA_GATEWAY_MORTYCONTROLLERENDPOINT="http://localhost:8081"
export POLYXIA_GATEWAY_NLUAPIENDPOINT="http://localhost:8082"
export POLYXIA_GATEWAY_ADDR="localhost"
export POLYXIA_GATEWAY_PORT="8080"
```
Expand Down
13 changes: 9 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ go 1.20
require github.com/aws/aws-sdk-go v1.44.225

require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-ini/ini v1.67.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/morty-faas/controller v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/redis/go-redis/v9 v9.0.3 // indirect
github.com/rik-org/rik-go-client v0.1.4 // indirect
github.com/spf13/afero v1.9.3 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
Expand All @@ -23,7 +28,7 @@ require (
)

require (
github.com/bytedance/sonic v1.8.0 // indirect
github.com/bytedance/sonic v1.8.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
Expand All @@ -37,7 +42,7 @@ require (
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.15.15 // indirect
github.com/klauspost/cpuid/v2 v2.2.3 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/minio/md5-simd v1.1.2 // indirect
Expand All @@ -51,8 +56,8 @@ require (
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/thomasgouveia/go-config v1.0.0
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.9 // indirect
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect
github.com/ugorji/go/codec v1.2.10 // indirect
golang.org/x/arch v0.2.0 // indirect
golang.org/x/crypto v0.6.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.5.0 // indirect
Expand Down
18 changes: 18 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ github.com/aws/aws-sdk-go v1.44.225/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
github.com/bytedance/sonic v1.8.0 h1:ea0Xadu+sHlu7x5O3gKhRpQ1IKiMrSiHttPF0ybECuA=
github.com/bytedance/sonic v1.8.0/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U=
github.com/bytedance/sonic v1.8.1 h1:NqAHCaGaTzro0xMmnTCLUyRlbEP6r8MCA1cJUrH3Pu4=
github.com/bytedance/sonic v1.8.1/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
Expand All @@ -57,6 +61,8 @@ github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnht
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
Expand Down Expand Up @@ -167,6 +173,8 @@ github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBF
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.2.3 h1:sxCkb+qR91z4vsqw4vGGZlDgPz3G7gjaLyK3V8y70BU=
github.com/klauspost/cpuid/v2 v2.2.3/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY=
github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk=
github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
Expand Down Expand Up @@ -195,13 +203,19 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/morty-faas/controller v1.0.0 h1:tnM8txfJdBEBGKNCtt+vq1DbnjKl7NfVD5BrdwPLPhY=
github.com/morty-faas/controller v1.0.0/go.mod h1:yLV98RCayNbcJRGAeim2bhJuQ4G0h+XprSIyyjHL9Fg=
github.com/pelletier/go-toml/v2 v2.0.6 h1:nrzqCb7j9cDFj2coyLNLaZuJTLjWjlaz6nvTvIwycIU=
github.com/pelletier/go-toml/v2 v2.0.6/go.mod h1:eumQOmlWiOPt5WriQQqoM5y18pDHwha2N+QD+EUNTek=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/redis/go-redis/v9 v9.0.3 h1:+7mmR26M0IvyLxGZUHxu4GiBkJkVDid0Un+j4ScYu4k=
github.com/redis/go-redis/v9 v9.0.3/go.mod h1:WqMKv5vnQbRuZstUwxQI195wHy+t4PuXDOjzMvcuQHk=
github.com/rik-org/rik-go-client v0.1.4 h1:DieQNgnbWVKkc8C6IkO6T/bZG+Zm2XOiXqCTqLpJx3w=
github.com/rik-org/rik-go-client v0.1.4/go.mod h1:7EhBAgTNZ72AlNfKR4E+AzWtVP6uzRyoz1+aE09Z6aM=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rs/xid v1.4.0 h1:qd7wPTDkN6KQx2VmMBLrpHkiyQwgFXRnkOLacUiaSNY=
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
Expand Down Expand Up @@ -239,6 +253,8 @@ github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/ugorji/go/codec v1.2.9 h1:rmenucSohSTiyL09Y+l2OCk+FrMxGMzho2+tjr5ticU=
github.com/ugorji/go/codec v1.2.9/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
github.com/ugorji/go/codec v1.2.10 h1:eimT6Lsr+2lzmSZxPhLFoOWFmQqwk0fllJJ5hEbTXtQ=
github.com/ugorji/go/codec v1.2.10/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand All @@ -252,6 +268,8 @@ go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 h1:18EFjUmQOcUvxNYSkA6jO9VAiXCnxFY6NyDX0bHDmkU=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.2.0 h1:W1sUEHXiJTfjaFJ5SLo0N6lZn+0eO5gWD1MFeTGqQEY=
golang.org/x/arch v0.2.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand Down
27 changes: 15 additions & 12 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ import (
)

const (
DEFAULT_MORTY_API_ENDPOINT = "http://localhost:8081"
DEFAULT_NLU_API_ENDPOINT = "http://localhost:8082"
DEFAULT_ADDRESS = "0.0.0.0"
DEFAULT_PORT = 8080
DEFAULT_MORTY_CONTROLLER_ENDPOINT = "http://localhost:8083"
DEFAULT_MORTY_REGISTRY_ENDPOINT = "http://localhost:8081"
DEFAULT_NLU_API_ENDPOINT = "http://localhost:8082"
DEFAULT_ADDRESS = "0.0.0.0"
DEFAULT_PORT = 8080
)

type Config struct {
Addr string `yaml:"addr"`
Port int `yaml:"port"`
MortyRegistryEndpoint string `yaml:"mortyregistryendpoint"`
NluApiEndpoint string `yaml:"nluapiendpoint"`
Addr string `yaml:"addr"`
Port int `yaml:"port"`
MortyControllerEndpoint string `yaml:"mortycontrollerendpoint"`
MortyRegistryEndpoint string `yaml:"mortyregistryendpoint"`
NluApiEndpoint string `yaml:"nluapiendpoint"`
}

func Load() (*Config, error) {
Expand All @@ -34,10 +36,11 @@ func Load() (*Config, error) {
EnvPrefix: "POLYXIA_GATEWAY",

Default: &Config{
MortyRegistryEndpoint: DEFAULT_MORTY_API_ENDPOINT,
NluApiEndpoint: DEFAULT_NLU_API_ENDPOINT,
Addr: DEFAULT_ADDRESS,
Port: DEFAULT_PORT,
MortyRegistryEndpoint: DEFAULT_MORTY_REGISTRY_ENDPOINT,
MortyControllerEndpoint: DEFAULT_MORTY_CONTROLLER_ENDPOINT,
NluApiEndpoint: DEFAULT_NLU_API_ENDPOINT,
Addr: DEFAULT_ADDRESS,
Port: DEFAULT_PORT,
},
})
if err != nil {
Expand Down
19 changes: 17 additions & 2 deletions internal/gateway/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/morty-faas/controller/pkg/client"
)

const (
Expand All @@ -25,7 +26,8 @@ const (

type (
Server struct {
cfg *config.Config
cfg *config.Config
mortyClient *client.APIClient
}

APIError struct {
Expand All @@ -45,8 +47,21 @@ func NewServer() (*Server, error) {
return nil, err
}

// log cfg
log.Debugf("Morty Controller Endpoint", cfg.MortyControllerEndpoint)
log.Debugf("Morty Registry Endpoint", cfg.MortyRegistryEndpoint)

// Init Morty client to communicate with the Morty controller
morty := client.NewAPIClient(&client.Configuration{
Servers: client.ServerConfigurations{
client.ServerConfiguration{
URL: cfg.MortyControllerEndpoint,
},
}})

server := &Server{
cfg: cfg,
cfg: cfg,
mortyClient: morty,
}

return server, nil
Expand Down
40 changes: 28 additions & 12 deletions internal/gateway/skills.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"strings"

mortyClient "github.com/morty-faas/controller/pkg/client"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -53,6 +54,10 @@ func (s *Server) SkillsHandler(w http.ResponseWriter, r *http.Request) {
log.Error(err)
s.APIErrorResponse(w, makeAPIError(http.StatusInternalServerError, err))
}
bodyBuf := new(bytes.Buffer)
bodyBuf.ReadFrom(mortyFunctionRegistryResp.Body)
body := bodyBuf.String()
log.Debugf("Morty registry response: %s", body)

if mortyFunctionRegistryResp.StatusCode != http.StatusOK {
log.Warnf("Morty registry creation failed!")
Expand All @@ -66,10 +71,31 @@ func (s *Server) SkillsHandler(w http.ResponseWriter, r *http.Request) {
log.Debugf("DELETE", s.cfg.NluApiEndpoint+NLU_SKILLS_ENDPOINT+"/"+name)
}
log.Debugf(mortyFunctionRegistryResp.Status)

s.JSONResponse(w, mortyFunctionRegistryResp.StatusCode, bodyBuf.String())
return
}

imagePath := s.cfg.MortyControllerEndpoint + body
imagePath = strings.Replace(imagePath, "\"", "", -1)

log.Debugf("Asking to Morty client to create the function (skill)")
log.Debugf("Name: %s", name)
log.Debugf("Image: %s", imagePath)
request := s.mortyClient.FunctionApi.CreateFunction(r.Context()).CreateFunctionRequest(mortyClient.CreateFunctionRequest{
Name: &name,
Image: &imagePath,
})
_, res, err := request.Execute()
if err != nil {
log.Debugf(res.Status)
// log body
bodyBuf := new(bytes.Buffer)
bodyBuf.ReadFrom(mortyFunctionRegistryResp.Body)
bodyBuf.ReadFrom(res.Body)
log.Debugf(bodyBuf.String())
s.JSONResponse(w, mortyFunctionRegistryResp.StatusCode, bodyBuf.String())

log.Errorf("Morty client failed to create the function (skill): %s", err)
s.APIErrorResponse(w, makeAPIError(http.StatusInternalServerError, err))
return
}

Expand Down Expand Up @@ -169,16 +195,6 @@ func handleArchive(MORTY_API_ENDPOINT string, r *http.Request, name string) (*ht
if err != nil {
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
bodyString := string(bodyBytes)
log.Debugf(bodyString)
}

return resp, nil
}

0 comments on commit 90c6bf4

Please sign in to comment.