Skip to content

Commit d716087

Browse files
authored
Merge pull request #86 from mutablelogic/v5
V5
2 parents df5f07b + b174dfd commit d716087

File tree

199 files changed

+16875
-487
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

199 files changed

+16875
-487
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ build
22
_old
33
._*
44
.DS_Store
5+
node_modules
6+
package-lock.json
7+
dist

Makefile

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
# Executables
22
GO ?= $(shell which go 2>/dev/null)
33
DOCKER ?= $(shell which docker 2>/dev/null)
4+
NPM ?= $(shell which npm 2>/dev/null)
45

56
# Locations
67
BUILD_DIR ?= build
78
CMD_DIR := $(wildcard cmd/*)
89
PLUGIN_DIR := $(wildcard plugin/*)
10+
NPM_DIR := $(wildcard npm/*)
911

1012
# VERBOSE=1
1113
ifneq ($(VERBOSE),)
@@ -44,17 +46,20 @@ all: clean build
4446

4547
# Build the commands in the cmd directory
4648
.PHONY: build
47-
build: tidy $(CMD_DIR) $(PLUGIN_DIR)
49+
build: tidy $(NPM_DIR) $(PLUGIN_DIR) $(CMD_DIR)
4850

4951
$(CMD_DIR): go-dep mkdir
5052
@echo Build command $(notdir $@) GOOS=${OS} GOARCH=${ARCH}
5153
@GOOS=${OS} GOARCH=${ARCH} ${GO} build ${BUILD_FLAGS} -o ${BUILD_DIR}/$(notdir $@) ./$@
5254

53-
5455
$(PLUGIN_DIR): go-dep mkdir
5556
@echo Build plugin $(notdir $@) GOOS=${OS} GOARCH=${ARCH}
5657
@GOOS=${OS} GOARCH=${ARCH} ${GO} build -buildmode=plugin ${BUILD_FLAGS} -o ${BUILD_DIR}/$(notdir $@).plugin ./$@
5758

59+
$(NPM_DIR): npm-dep
60+
@echo Build npm $(notdir $@)
61+
@cd $@ && npm install && npm run prod
62+
5863
# Build the docker image
5964
.PHONY: docker
6065
docker: docker-dep
@@ -122,3 +127,7 @@ go-dep:
122127
.PHONY: docker-dep
123128
docker-dep:
124129
@test -f "${DOCKER}" && test -x "${DOCKER}" || (echo "Missing docker binary" && exit 1)
130+
131+
.PHONY: npm-dep
132+
npm-dep:
133+
@test -f "${NPM}" && test -x "${NPM}" || (echo "Missing npm binary" && exit 1)

cmd.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package server
2+
3+
import (
4+
"context"
5+
"net/url"
6+
7+
// Packages
8+
"github.com/mutablelogic/go-client"
9+
)
10+
11+
///////////////////////////////////////////////////////////////////////////////
12+
// INTERFACES
13+
14+
// Cmd represents the command line interface context
15+
type Cmd interface {
16+
// Return the context
17+
Context() context.Context
18+
19+
// Return the debug mode
20+
GetDebug() DebugLevel
21+
22+
// Return the endpoint
23+
GetEndpoint(paths ...string) *url.URL
24+
25+
// Return the HTTP client options
26+
GetClientOpts() []client.ClientOpt
27+
}
28+
29+
///////////////////////////////////////////////////////////////////////////////
30+
// TYPES
31+
32+
type DebugLevel uint
33+
34+
type CmdOffsetLimit struct {
35+
Offset uint64 `name:"offset" help:"List item offset"`
36+
Limit *uint64 `name:"limit" help:"List item limit"`
37+
}
38+
39+
///////////////////////////////////////////////////////////////////////////////
40+
// GLOBALS
41+
42+
const (
43+
None DebugLevel = iota
44+
Debug
45+
Trace
46+
)

cmd/server/app.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/url"
7+
"os"
8+
"os/signal"
9+
"path/filepath"
10+
"syscall"
11+
12+
// Packages
13+
kong "github.com/alecthomas/kong"
14+
client "github.com/mutablelogic/go-client"
15+
server "github.com/mutablelogic/go-server"
16+
types "github.com/mutablelogic/go-server/pkg/types"
17+
version "github.com/mutablelogic/go-server/pkg/version"
18+
)
19+
20+
///////////////////////////////////////////////////////////////////////////////
21+
// TYPES
22+
23+
type Globals struct {
24+
Endpoint string `env:"ENDPOINT" default:"http://localhost/" help:"Service endpoint"`
25+
Debug bool `help:"Enable debug output"`
26+
Trace bool `help:"Enable trace output"`
27+
28+
vars kong.Vars `kong:"-"` // Variables for kong
29+
ctx context.Context
30+
cancel context.CancelFunc
31+
}
32+
33+
var _ server.Cmd = (*Globals)(nil)
34+
35+
///////////////////////////////////////////////////////////////////////////////
36+
// LIFECYCLE
37+
38+
func NewApp(app Globals, vars kong.Vars) (*Globals, error) {
39+
// Set the vars
40+
app.vars = vars
41+
42+
// Create the context
43+
// This context is cancelled when the process receives a SIGINT or SIGTERM
44+
app.ctx, app.cancel = signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
45+
46+
// Return the app
47+
return &app, nil
48+
}
49+
50+
func (app *Globals) Close() error {
51+
app.cancel()
52+
return nil
53+
}
54+
55+
///////////////////////////////////////////////////////////////////////////////
56+
// PUBLIC METHODS
57+
58+
func (app *Globals) Context() context.Context {
59+
return app.ctx
60+
}
61+
62+
func (app *Globals) GetDebug() server.DebugLevel {
63+
if app.Debug {
64+
return server.Debug
65+
}
66+
if app.Trace {
67+
return server.Trace
68+
}
69+
return server.None
70+
}
71+
72+
func (app *Globals) GetEndpoint(paths ...string) *url.URL {
73+
url, err := url.Parse(app.Endpoint)
74+
if err != nil {
75+
return nil
76+
}
77+
for _, path := range paths {
78+
url.Path = types.JoinPath(url.Path, os.Expand(path, func(key string) string {
79+
return app.vars[key]
80+
}))
81+
}
82+
return url
83+
}
84+
85+
func (app *Globals) GetClientOpts() []client.ClientOpt {
86+
opts := []client.ClientOpt{}
87+
88+
// Trace mode
89+
if app.Debug || app.Trace {
90+
opts = append(opts, client.OptTrace(os.Stderr, app.Trace))
91+
}
92+
93+
// Append user agent
94+
source := version.GitSource
95+
version := version.GitTag
96+
if source == "" {
97+
source = "go-server"
98+
}
99+
if version == "" {
100+
version = "v0.0.0"
101+
}
102+
opts = append(opts, client.OptUserAgent(fmt.Sprintf("%v/%v", filepath.Base(source), version)))
103+
104+
// Return options
105+
return opts
106+
}

cmd/server/main.go

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,98 @@
11
package main
22

3-
import "fmt"
3+
import (
4+
"os"
5+
"os/user"
6+
"path/filepath"
7+
8+
// Packages
9+
kong "github.com/alecthomas/kong"
10+
server "github.com/mutablelogic/go-server"
11+
auth "github.com/mutablelogic/go-server/pkg/auth/cmd"
12+
certmanager "github.com/mutablelogic/go-server/pkg/cert/cmd"
13+
pgmanager "github.com/mutablelogic/go-server/pkg/pgmanager/cmd"
14+
pgqueue "github.com/mutablelogic/go-server/pkg/pgqueue/cmd"
15+
)
16+
17+
///////////////////////////////////////////////////////////////////////////////
18+
// TYPES
19+
20+
type CLI struct {
21+
Globals
22+
ServiceCommands
23+
pgmanager.DatabaseCommands
24+
pgmanager.SchemaCommands
25+
pgmanager.ObjectCommands
26+
pgmanager.RoleCommands
27+
pgmanager.ConnectionCommands
28+
pgmanager.TablespaceCommands
29+
pgqueue.QueueCommands
30+
pgqueue.TaskCommands
31+
pgqueue.TickerCommands
32+
certmanager.NameCommands
33+
certmanager.CertCommands
34+
auth.UserCommands
35+
auth.TokenCommands
36+
auth.AuthCommands
37+
}
38+
39+
///////////////////////////////////////////////////////////////////////////////
40+
// LIFECYCLE
441

542
func main() {
6-
fmt.Println("Hello, World!")
43+
// Parse command-line flags
44+
var cli CLI
45+
kong := kong.Parse(&cli,
46+
kong.Name(execName()),
47+
kong.Description("command-line tool"),
48+
kong.UsageOnError(),
49+
kong.ConfigureHelp(kong.HelpOptions{Compact: true}),
50+
kong.Vars{
51+
"HOST": hostName(),
52+
"USER": userName(),
53+
"CERT_PREFIX": "/cert/v1",
54+
"PG_PREFIX": "/pg/v1",
55+
"AUTH_PREFIX": "/auth/v1",
56+
"QUEUE_PREFIX": "/queue/v1",
57+
},
58+
)
59+
60+
// Create the app
61+
app, err := NewApp(cli.Globals, kong.Model.Vars())
62+
if err != nil {
63+
kong.FatalIfErrorf(err)
64+
}
65+
defer app.Close()
66+
67+
// Run
68+
kong.BindTo(app, (*server.Cmd)(nil))
69+
kong.FatalIfErrorf(kong.Run())
70+
}
71+
72+
///////////////////////////////////////////////////////////////////////////////
73+
// PRIVATE METHODS
74+
75+
func hostName() string {
76+
name, err := os.Hostname()
77+
if err != nil {
78+
panic(err)
79+
}
80+
return name
81+
}
82+
83+
func userName() string {
84+
user, err := user.Current()
85+
if err != nil {
86+
panic(err)
87+
}
88+
return user.Username
89+
}
90+
91+
func execName() string {
92+
// The name of the executable
93+
name, err := os.Executable()
94+
if err != nil {
95+
panic(err)
96+
}
97+
return filepath.Base(name)
798
}

0 commit comments

Comments
 (0)