-
Notifications
You must be signed in to change notification settings - Fork 0
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 #34 from mutablelogic/v1
Added a JSON streaming callback
- Loading branch information
Showing
8 changed files
with
174 additions
and
6 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
// Packages | ||
tablewriter "github.com/djthorpe/go-tablewriter" | ||
client "github.com/mutablelogic/go-client" | ||
auth "github.com/mutablelogic/go-server/pkg/handler/auth/client" | ||
) | ||
|
||
var ( | ||
authClient *auth.Client | ||
authName = "tokenauth" | ||
authDuration time.Duration | ||
) | ||
|
||
func authRegister(flags *Flags) { | ||
// Register flags | ||
flags.String(authName, "tokenauth-endpoint", "${TOKENAUTH_ENDPOINT}", "tokenauth endpoint (ie, http://host/api/auth/)") | ||
flags.String(authName, "tokenauth-token", "${TOKENAUTH_TOKEN}", "tokenauth token") | ||
flags.Duration(authName, "expiry", 0, "token expiry duration") | ||
|
||
// Register commands | ||
flags.Register(Cmd{ | ||
Name: authName, | ||
Description: "Manage token authentication", | ||
Parse: authParse, | ||
Fn: []Fn{ | ||
// Default caller | ||
{Call: authList, Description: "List authentication tokens"}, | ||
{Name: "list", Call: authList, Description: "List authentication tokens"}, | ||
{Name: "create", Call: authCreate, Description: "Create a token", MinArgs: 1}, | ||
{Name: "delete", Call: authDelete, Description: "Delete a token", MinArgs: 1, MaxArgs: 1}, | ||
}, | ||
}) | ||
} | ||
|
||
func authParse(flags *Flags, opts ...client.ClientOpt) error { | ||
endpoint := flags.GetString("tokenauth-endpoint") | ||
if token := flags.GetString("tokenauth-token"); token != "" { | ||
opts = append(opts, client.OptReqToken(client.Token{ | ||
Scheme: "Bearer", | ||
Value: token, | ||
})) | ||
} | ||
|
||
if duration := flags.GetString("expiry"); duration != "" { | ||
if d, err := time.ParseDuration(duration); err != nil { | ||
return err | ||
} else { | ||
authDuration = d | ||
} | ||
} | ||
|
||
if client, err := auth.New(endpoint, opts...); err != nil { | ||
return err | ||
} else { | ||
authClient = client | ||
} | ||
return nil | ||
} | ||
|
||
func authList(_ context.Context, w *tablewriter.Writer, _ []string) error { | ||
tokens, err := authClient.List() | ||
if err != nil { | ||
return err | ||
} | ||
return w.Write(tokens) | ||
} | ||
|
||
func authCreate(_ context.Context, w *tablewriter.Writer, params []string) error { | ||
name := params[0] | ||
scopes := params[1:] | ||
token, err := authClient.Create(name, authDuration, scopes...) | ||
if err != nil { | ||
return err | ||
} | ||
return w.Write(token) | ||
} | ||
|
||
func authDelete(ctx context.Context, w *tablewriter.Writer, params []string) error { | ||
name := params[0] | ||
err := authClient.Delete(name) | ||
if err != nil { | ||
return err | ||
} | ||
return authList(ctx, w, nil) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
|
||
// Packages | ||
tablewriter "github.com/djthorpe/go-tablewriter" | ||
client "github.com/mutablelogic/go-client" | ||
nginx "github.com/mutablelogic/go-server/pkg/handler/nginx/client" | ||
) | ||
|
||
var ( | ||
nginxClient *nginx.Client | ||
nginxName = "nginx" | ||
nginxEndpoint string | ||
) | ||
|
||
func nginxRegister(flags *Flags) { | ||
flags.Register(Cmd{ | ||
Name: nginxName, | ||
Description: "Manage nginx instances", | ||
Parse: nginxParse, | ||
Fn: []Fn{ | ||
// Default caller | ||
{Call: nginxGetVersion, Description: "Get the nginx version that is running"}, | ||
}, | ||
}) | ||
} | ||
|
||
func nginxParse(flags *Flags, opts ...client.ClientOpt) error { | ||
// Register flags | ||
flags.String(nginxName, "nginx-endpoint", "${NGINX_ENDPOINT}", "nginx endpoint") | ||
|
||
if client, err := nginx.New(nginxEndpoint, opts...); err != nil { | ||
return err | ||
} else { | ||
nginxClient = client | ||
} | ||
return nil | ||
} | ||
|
||
func nginxGetVersion(_ context.Context, w *tablewriter.Writer, _ []string) error { | ||
version, _, err := nginxClient.Health() | ||
if err != nil { | ||
return err | ||
} | ||
return w.Write(version) | ||
} |
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