Skip to content

Commit

Permalink
fix: ensure that https protocol is used when not on localhost
Browse files Browse the repository at this point in the history
Signed-off-by: Donnie Adams <[email protected]>
  • Loading branch information
thedadams committed Nov 26, 2024
1 parent 82e7db2 commit 000d526
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 25 deletions.
5 changes: 2 additions & 3 deletions pkg/api/handlers/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/otto8-ai/otto8/apiclient/types"
"github.com/otto8-ai/otto8/pkg/alias"
"github.com/otto8-ai/otto8/pkg/api"
"github.com/otto8-ai/otto8/pkg/api/server"
"github.com/otto8-ai/otto8/pkg/render"
v1 "github.com/otto8-ai/otto8/pkg/storage/apis/otto.otto8.ai/v1"
"github.com/otto8-ai/otto8/pkg/system"
Expand Down Expand Up @@ -108,12 +107,12 @@ func (a *AgentHandler) Create(req api.Context) error {

func convertAgent(agent v1.Agent, req api.Context) (*types.Agent, error) {
var links []string
if prefix := server.GetURLPrefix(req); prefix != "" {
if req.APIBaseURL != "" {
alias := agent.Name
if agent.Status.AliasAssigned && agent.Spec.Manifest.Alias != "" {
alias = agent.Spec.Manifest.Alias
}
links = []string{"invoke", prefix + "/invoke/" + alias}
links = []string{"invoke", req.APIBaseURL + "/invoke/" + alias}
}

var embeddingModel string
Expand Down
11 changes: 5 additions & 6 deletions pkg/api/handlers/webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/otto8-ai/otto8/apiclient/types"
"github.com/otto8-ai/otto8/pkg/alias"
"github.com/otto8-ai/otto8/pkg/api"
"github.com/otto8-ai/otto8/pkg/api/server"
v1 "github.com/otto8-ai/otto8/pkg/storage/apis/otto.otto8.ai/v1"
"github.com/otto8-ai/otto8/pkg/system"
"github.com/otto8-ai/otto8/pkg/wait"
Expand Down Expand Up @@ -86,7 +85,7 @@ func (a *WebhookHandler) Update(req api.Context) error {
return fmt.Errorf("failed to update webhook: %w", err)
}

return req.Write(convertWebhook(*processedWh, server.GetURLPrefix(req)))
return req.Write(convertWebhook(*processedWh, req.APIBaseURL))
}

func (a *WebhookHandler) Delete(req api.Context) error {
Expand Down Expand Up @@ -138,7 +137,7 @@ func (a *WebhookHandler) Create(req api.Context) error {
return fmt.Errorf("failed to create webhook: %w", err)
}

return req.WriteCreated(convertWebhook(*wh, server.GetURLPrefix(req)))
return req.WriteCreated(convertWebhook(*wh, req.APIBaseURL))
}

func convertWebhook(webhook v1.Webhook, urlPrefix string) *types.Webhook {
Expand Down Expand Up @@ -173,7 +172,7 @@ func (a *WebhookHandler) ByID(req api.Context) error {
return err
}

return req.Write(convertWebhook(wh, server.GetURLPrefix(req)))
return req.Write(convertWebhook(wh, req.APIBaseURL))
}

func (a *WebhookHandler) List(req api.Context) error {
Expand All @@ -184,7 +183,7 @@ func (a *WebhookHandler) List(req api.Context) error {

var resp types.WebhookList
for _, wh := range webhookList.Items {
resp.Items = append(resp.Items, *convertWebhook(wh, server.GetURLPrefix(req)))
resp.Items = append(resp.Items, *convertWebhook(wh, req.APIBaseURL))
}

return req.Write(resp)
Expand All @@ -207,7 +206,7 @@ func (a *WebhookHandler) RemoveToken(req api.Context) error {
return fmt.Errorf("failed to remove token: %w", err)
}

return req.Write(convertWebhook(wh, server.GetURLPrefix(req)))
return req.Write(convertWebhook(wh, req.APIBaseURL))
}

func (a *WebhookHandler) Execute(req api.Context) error {
Expand Down
5 changes: 2 additions & 3 deletions pkg/api/handlers/workflows.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/otto8-ai/otto8/apiclient/types"
"github.com/otto8-ai/otto8/pkg/alias"
"github.com/otto8-ai/otto8/pkg/api"
"github.com/otto8-ai/otto8/pkg/api/server"
"github.com/otto8-ai/otto8/pkg/controller/handlers/workflow"
"github.com/otto8-ai/otto8/pkg/invoke"
"github.com/otto8-ai/otto8/pkg/render"
Expand Down Expand Up @@ -152,12 +151,12 @@ func (a *WorkflowHandler) Create(req api.Context) error {

func convertWorkflow(workflow v1.Workflow, req api.Context) (*types.Workflow, error) {
var links []string
if prefix := server.GetURLPrefix(req); prefix != "" {
if req.APIBaseURL != "" {
alias := workflow.Name
if workflow.Status.AliasAssigned && workflow.Spec.Manifest.Alias != "" {
alias = workflow.Spec.Manifest.Alias
}
links = []string{"invoke", prefix + "/invoke/" + alias}
links = []string{"invoke", req.APIBaseURL + "/invoke/" + alias}
}

var embeddingModel string
Expand Down
7 changes: 4 additions & 3 deletions pkg/api/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ import (
type Context struct {
http.ResponseWriter
*http.Request
GPTClient *gptscript.GPTScript
Storage storage.Client
User user.Info
GPTClient *gptscript.GPTScript
Storage storage.Client
User user.Info
APIBaseURL string
}

type (
Expand Down
13 changes: 4 additions & 9 deletions pkg/api/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@ type Server struct {
authenticator *authn.Authenticator
authorizer *authz.Authorizer
proxyServer *proxy.Proxy
baseURL string

mux *http.ServeMux
}

func NewServer(storageClient storage.Client, gptClient *gptscript.GPTScript, authn *authn.Authenticator, authz *authz.Authorizer, proxyServer *proxy.Proxy) *Server {
func NewServer(storageClient storage.Client, gptClient *gptscript.GPTScript, authn *authn.Authenticator, authz *authz.Authorizer, proxyServer *proxy.Proxy, baseURL string) *Server {
return &Server{
storageClient: storageClient,
gptClient: gptClient,
authenticator: authn,
authorizer: authz,
proxyServer: proxyServer,
baseURL: baseURL + "/api",

mux: http.NewServeMux(),
}
Expand Down Expand Up @@ -92,8 +94,8 @@ func (s *Server) wrap(f api.HandlerFunc) http.HandlerFunc {
GPTClient: s.gptClient,
Storage: s.storageClient,
User: user,
APIBaseURL: s.baseURL,
})

if errHttp := (*types.ErrHTTP)(nil); errors.As(err, &errHttp) {
http.Error(rw, errHttp.Message, errHttp.Code)
} else if errStatus := (*apierrors.StatusError)(nil); errors.As(err, &errStatus) {
Expand All @@ -103,10 +105,3 @@ func (s *Server) wrap(f api.HandlerFunc) http.HandlerFunc {
}
}
}

func GetURLPrefix(req api.Context) string {
if req.Request.TLS == nil {
return "http://" + req.Request.Host + "/api"
}
return "https://" + req.Request.Host + "/api"
}
2 changes: 1 addition & 1 deletion pkg/services/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func New(ctx context.Context, config Config) (*Services, error) {
StorageClient: storageClient,
Router: r,
GPTClient: c,
APIServer: server.NewServer(storageClient, c, authn.NewAuthenticator(authenticators), authz.NewAuthorizer(), proxyServer),
APIServer: server.NewServer(storageClient, c, authn.NewAuthenticator(authenticators), authz.NewAuthorizer(), proxyServer, config.Hostname),
TokenServer: tokenServer,
Invoker: invoker,
AIHelper: aihelper.New(c, config.HelperModel),
Expand Down

0 comments on commit 000d526

Please sign in to comment.