Skip to content

Commit

Permalink
refactoring: reorganize logic for better future extending
Browse files Browse the repository at this point in the history
  • Loading branch information
reddec committed May 29, 2020
1 parent 84fbd82 commit 8999655
Show file tree
Hide file tree
Showing 34 changed files with 4,044 additions and 2,917 deletions.
116 changes: 116 additions & 0 deletions api/client/lambda_api_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package client

import (
"context"
client "github.com/reddec/jsonrpc2/client"
api "github.com/reddec/trusted-cgi/api"
application "github.com/reddec/trusted-cgi/application"
stats "github.com/reddec/trusted-cgi/stats"
types "github.com/reddec/trusted-cgi/types"
"sync/atomic"
)

func DefaultLambdaAPI() *LambdaAPIClient {
return &LambdaAPIClient{BaseURL: "https://127.0.0.1:3434/u/"}
}

type LambdaAPIClient struct {
BaseURL string
sequence uint64
}

// Upload content from .tar.gz archive to app and call Install handler (if defined)
func (impl *LambdaAPIClient) Upload(ctx context.Context, token *api.Token, uid string, tarGz []byte) (reply bool, err error) {
err = client.CallHTTP(ctx, impl.BaseURL, "LambdaAPI.Upload", atomic.AddUint64(&impl.sequence, 1), &reply, token, uid, tarGz)
return
}

// Download content as .tar.gz archive from app
func (impl *LambdaAPIClient) Download(ctx context.Context, token *api.Token, uid string) (reply []byte, err error) {
err = client.CallHTTP(ctx, impl.BaseURL, "LambdaAPI.Download", atomic.AddUint64(&impl.sequence, 1), &reply, token, uid)
return
}

// Push single file to app
func (impl *LambdaAPIClient) Push(ctx context.Context, token *api.Token, uid string, file string, content []byte) (reply bool, err error) {
err = client.CallHTTP(ctx, impl.BaseURL, "LambdaAPI.Push", atomic.AddUint64(&impl.sequence, 1), &reply, token, uid, file, content)
return
}

// Pull single file from app
func (impl *LambdaAPIClient) Pull(ctx context.Context, token *api.Token, uid string, file string) (reply []byte, err error) {
err = client.CallHTTP(ctx, impl.BaseURL, "LambdaAPI.Pull", atomic.AddUint64(&impl.sequence, 1), &reply, token, uid, file)
return
}

// Remove app and call Uninstall handler (if defined)
func (impl *LambdaAPIClient) Remove(ctx context.Context, token *api.Token, uid string) (reply bool, err error) {
err = client.CallHTTP(ctx, impl.BaseURL, "LambdaAPI.Remove", atomic.AddUint64(&impl.sequence, 1), &reply, token, uid)
return
}

// Files in func dir
func (impl *LambdaAPIClient) Files(ctx context.Context, token *api.Token, uid string, dir string) (reply []*api.File, err error) {
err = client.CallHTTP(ctx, impl.BaseURL, "LambdaAPI.Files", atomic.AddUint64(&impl.sequence, 1), &reply, token, uid, dir)
return
}

// Info about application
func (impl *LambdaAPIClient) Info(ctx context.Context, token *api.Token, uid string) (reply *application.App, err error) {
err = client.CallHTTP(ctx, impl.BaseURL, "LambdaAPI.Info", atomic.AddUint64(&impl.sequence, 1), &reply, token, uid)
return
}

// Update application manifest
func (impl *LambdaAPIClient) Update(ctx context.Context, token *api.Token, uid string, manifest types.Manifest) (reply *application.App, err error) {
err = client.CallHTTP(ctx, impl.BaseURL, "LambdaAPI.Update", atomic.AddUint64(&impl.sequence, 1), &reply, token, uid, manifest)
return
}

// Create file or directory inside app
func (impl *LambdaAPIClient) CreateFile(ctx context.Context, token *api.Token, uid string, path string, dir bool) (reply bool, err error) {
err = client.CallHTTP(ctx, impl.BaseURL, "LambdaAPI.CreateFile", atomic.AddUint64(&impl.sequence, 1), &reply, token, uid, path, dir)
return
}

// Remove file or directory
func (impl *LambdaAPIClient) RemoveFile(ctx context.Context, token *api.Token, uid string, path string) (reply bool, err error) {
err = client.CallHTTP(ctx, impl.BaseURL, "LambdaAPI.RemoveFile", atomic.AddUint64(&impl.sequence, 1), &reply, token, uid, path)
return
}

// Rename file or directory
func (impl *LambdaAPIClient) RenameFile(ctx context.Context, token *api.Token, uid string, oldPath string, newPath string) (reply bool, err error) {
err = client.CallHTTP(ctx, impl.BaseURL, "LambdaAPI.RenameFile", atomic.AddUint64(&impl.sequence, 1), &reply, token, uid, oldPath, newPath)
return
}

// Stats for the app
func (impl *LambdaAPIClient) Stats(ctx context.Context, token *api.Token, uid string, limit int) (reply []stats.Record, err error) {
err = client.CallHTTP(ctx, impl.BaseURL, "LambdaAPI.Stats", atomic.AddUint64(&impl.sequence, 1), &reply, token, uid, limit)
return
}

// Actions available for the app
func (impl *LambdaAPIClient) Actions(ctx context.Context, token *api.Token, uid string) (reply []string, err error) {
err = client.CallHTTP(ctx, impl.BaseURL, "LambdaAPI.Actions", atomic.AddUint64(&impl.sequence, 1), &reply, token, uid)
return
}

// Invoke action in the app (if make installed)
func (impl *LambdaAPIClient) Invoke(ctx context.Context, token *api.Token, uid string, action string) (reply string, err error) {
err = client.CallHTTP(ctx, impl.BaseURL, "LambdaAPI.Invoke", atomic.AddUint64(&impl.sequence, 1), &reply, token, uid, action)
return
}

// Make link/alias for app
func (impl *LambdaAPIClient) Link(ctx context.Context, token *api.Token, uid string, alias string) (reply *application.App, err error) {
err = client.CallHTTP(ctx, impl.BaseURL, "LambdaAPI.Link", atomic.AddUint64(&impl.sequence, 1), &reply, token, uid, alias)
return
}

// Remove link
func (impl *LambdaAPIClient) Unlink(ctx context.Context, token *api.Token, alias string) (reply *application.App, err error) {
err = client.CallHTTP(ctx, impl.BaseURL, "LambdaAPI.Unlink", atomic.AddUint64(&impl.sequence, 1), &reply, token, alias)
return
}
67 changes: 67 additions & 0 deletions api/client/project_api_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package client

import (
"context"
client "github.com/reddec/jsonrpc2/client"
api "github.com/reddec/trusted-cgi/api"
application "github.com/reddec/trusted-cgi/application"
stats "github.com/reddec/trusted-cgi/stats"
"sync/atomic"
)

func DefaultProjectAPI() *ProjectAPIClient {
return &ProjectAPIClient{BaseURL: "https://127.0.0.1:3434/u/"}
}

type ProjectAPIClient struct {
BaseURL string
sequence uint64
}

// Get global configuration
func (impl *ProjectAPIClient) Config(ctx context.Context, token *api.Token) (reply *api.Settings, err error) {
err = client.CallHTTP(ctx, impl.BaseURL, "ProjectAPI.Config", atomic.AddUint64(&impl.sequence, 1), &reply, token)
return
}

// Change effective user
func (impl *ProjectAPIClient) SetUser(ctx context.Context, token *api.Token, user string) (reply *api.Settings, err error) {
err = client.CallHTTP(ctx, impl.BaseURL, "ProjectAPI.SetUser", atomic.AddUint64(&impl.sequence, 1), &reply, token, user)
return
}

// Get all templates without filtering
func (impl *ProjectAPIClient) AllTemplates(ctx context.Context, token *api.Token) (reply []*api.TemplateStatus, err error) {
err = client.CallHTTP(ctx, impl.BaseURL, "ProjectAPI.AllTemplates", atomic.AddUint64(&impl.sequence, 1), &reply, token)
return
}

// List available apps (lambdas) in a project
func (impl *ProjectAPIClient) List(ctx context.Context, token *api.Token) (reply []*application.App, err error) {
err = client.CallHTTP(ctx, impl.BaseURL, "ProjectAPI.List", atomic.AddUint64(&impl.sequence, 1), &reply, token)
return
}

// Templates with filter by availability including embedded
func (impl *ProjectAPIClient) Templates(ctx context.Context, token *api.Token) (reply []*api.Template, err error) {
err = client.CallHTTP(ctx, impl.BaseURL, "ProjectAPI.Templates", atomic.AddUint64(&impl.sequence, 1), &reply, token)
return
}

// Global last records
func (impl *ProjectAPIClient) Stats(ctx context.Context, token *api.Token, limit int) (reply []stats.Record, err error) {
err = client.CallHTTP(ctx, impl.BaseURL, "ProjectAPI.Stats", atomic.AddUint64(&impl.sequence, 1), &reply, token, limit)
return
}

// Create new app (lambda)
func (impl *ProjectAPIClient) Create(ctx context.Context, token *api.Token) (reply *application.App, err error) {
err = client.CallHTTP(ctx, impl.BaseURL, "ProjectAPI.Create", atomic.AddUint64(&impl.sequence, 1), &reply, token)
return
}

// Create new app/lambda/function using pre-defined template
func (impl *ProjectAPIClient) CreateFromTemplate(ctx context.Context, token *api.Token, templateName string) (reply *application.App, err error) {
err = client.CallHTTP(ctx, impl.BaseURL, "ProjectAPI.CreateFromTemplate", atomic.AddUint64(&impl.sequence, 1), &reply, token, templateName)
return
}
29 changes: 29 additions & 0 deletions api/client/user_api_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package client

import (
"context"
client "github.com/reddec/jsonrpc2/client"
api "github.com/reddec/trusted-cgi/api"
"sync/atomic"
)

func DefaultUserAPI() *UserAPIClient {
return &UserAPIClient{BaseURL: "https://127.0.0.1:3434/u/"}
}

type UserAPIClient struct {
BaseURL string
sequence uint64
}

// Login user by username and password. Returns signed JWT
func (impl *UserAPIClient) Login(ctx context.Context, login string, password string) (reply *api.Token, err error) {
err = client.CallHTTP(ctx, impl.BaseURL, "UserAPI.Login", atomic.AddUint64(&impl.sequence, 1), &reply, login, password)
return
}

// Change password for the user
func (impl *UserAPIClient) ChangePassword(ctx context.Context, token *api.Token, password string) (reply bool, err error) {
err = client.CallHTTP(ctx, impl.BaseURL, "UserAPI.ChangePassword", atomic.AddUint64(&impl.sequence, 1), &reply, token, password)
return
}
Loading

0 comments on commit 8999655

Please sign in to comment.