Skip to content

Commit 23e15f3

Browse files
committed
restructured files and directories to be more organised and easier to navigate
1 parent ee65631 commit 23e15f3

35 files changed

+363
-332
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
.idea
2-
cmd/base.meta.go
3-
service/base.meta.go
2+
service/_base/meta.go
43
build

cmd/_base/.gitkeep

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The base uniform implementation of the command layer, don't add, edit or remove any of these files

cmd/_base/execute.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package _base
2+
3+
import (
4+
"fmt"
5+
"github.com/nats-io/go-nats"
6+
"github.com/spf13/cobra"
7+
"os"
8+
service "service/service/_base"
9+
)
10+
11+
var NatsUri string
12+
var NatsCert string
13+
var NatsKey string
14+
var DisableTls bool
15+
16+
var RootCmd = &cobra.Command{
17+
Use: service.AppName,
18+
Short: service.AppDescription,
19+
Long: service.AppDescription,
20+
Run: func(cmd *cobra.Command, args []string) {
21+
if err := cmd.Help(); err != nil {
22+
panic(err)
23+
}
24+
},
25+
}
26+
27+
func init() {
28+
// future: enable support for AWS EventBridge, Azure Event Grid and Google Cloud Pub/Sub support as well to migrate to simplify cloud migration
29+
30+
// base infrastructure command-line arguments should be included on all cobra commands hence we link it to the RootCmd
31+
RootCmd.PersistentFlags().StringVarP(&NatsUri, "nats", "n", nats.DefaultURL, "The nats cluster URI")
32+
RootCmd.PersistentFlags().StringVarP(&NatsCert, "nats-cert", "", "/etc/ssl/certs/ssl-bundle.crt", "The nats cluster TLS certificate file path")
33+
RootCmd.PersistentFlags().StringVarP(&NatsKey, "nats-key", "", "/etc/ssl/private/ssl.key", "The nats cluster TLS key file path")
34+
RootCmd.PersistentFlags().BoolVar(&DisableTls, "disable-tls", false, "A flag indicating if service should disable tls encryption")
35+
}
36+
37+
func Execute() {
38+
if err := RootCmd.Execute(); err != nil {
39+
fmt.Println(err)
40+
os.Exit(1)
41+
}
42+
}

cmd/base.helpers.go renamed to cmd/_base/helpers.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
package cmd
1+
package _base
22

33
import (
44
"crypto/tls"
55
"fmt"
66
"github.com/nats-io/go-nats"
77
"github.com/spf13/cobra"
8-
"service/service"
8+
service "service/service/_base"
99
)
1010

11-
func compileNatsOptions() []nats.Option {
11+
func CompileNatsOptions() []nats.Option {
1212
var natsOptions = make([]nats.Option, 0)
13-
if !disableTls {
14-
cert, err := tls.LoadX509KeyPair(natsCert, natsKey)
13+
if !DisableTls {
14+
cert, err := tls.LoadX509KeyPair(NatsCert, NatsKey)
1515
if err != nil {
1616
panic(err)
1717
}
@@ -27,7 +27,7 @@ func compileNatsOptions() []nats.Option {
2727
return natsOptions
2828
}
2929

30-
func command(name string, handler func(cmd *cobra.Command, args []string), description string) *cobra.Command {
30+
func Command(name string, handler func(cmd *cobra.Command, args []string), description string) *cobra.Command {
3131
if description == "" {
3232
description = fmt.Sprintf("Request the running %s to execute the %s command", service.AppName, name)
3333
}

cmd/_base/version.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package _base
2+
3+
import (
4+
"fmt"
5+
"github.com/spf13/cobra"
6+
service "service/service/_base"
7+
)
8+
9+
var VersionCmd = &cobra.Command{
10+
Use: "version",
11+
Short: "Show " + service.AppName + " version information",
12+
Long: "Show " + service.AppName + " version information",
13+
Run:VersionHandler,
14+
}
15+
16+
// we expose the version handler so that each service may modify how their version information is displayed
17+
var VersionHandler = func(cmd *cobra.Command, args []string) {
18+
fmt.Printf("%s version %s, build %s\n", service.AppName, service.AppVersion, service.AppCommit)
19+
}
20+
21+
func init() {
22+
RootCmd.AddCommand(VersionCmd)
23+
}

cmd/base.execute.go

Lines changed: 0 additions & 43 deletions
This file was deleted.

cmd/base.version.go

Lines changed: 0 additions & 20 deletions
This file was deleted.

cmd/command.ping.go

Lines changed: 0 additions & 13 deletions
This file was deleted.

cmd/commands/.gitkeep

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add your custom command-line-interface (CLI) interactions here for things like master data setup, cronjob routines, etc.

cmd/commands/ping.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package commands
2+
3+
/* Example
4+
This is just an example command for reference
5+
*/
6+
7+
import (
8+
"github.com/spf13/cobra"
9+
"service/cmd/_base"
10+
service "service/service/_base"
11+
"time"
12+
)
13+
14+
func init() {
15+
pingCmd := _base.Command("ping", func(cmd *cobra.Command, args []string) {
16+
service.Command("ping", time.Second, _base.NatsUri, _base.CompileNatsOptions(), map[string]string{
17+
// todo: link custom flags to arg values here, example: "custom": custom,
18+
}, func(data []byte) {
19+
// todo: handle response data, if any is received
20+
})
21+
}, "Ping the currently running service instance")
22+
23+
// todo: add custom CLI flags here
24+
25+
_base.RootCmd.AddCommand(pingCmd)
26+
}

cmd/execute.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package cmd
2+
3+
import "service/cmd/_base"
4+
5+
func Execute() {
6+
_base.Execute()
7+
}

cmd/run.go

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,35 @@ package cmd
22

33
import (
44
"github.com/spf13/cobra"
5-
"service/service"
5+
"service/cmd/_base"
6+
service "service/service/_base"
67
)
78

8-
var runCmd = &cobra.Command{
9-
Use: "run",
10-
Short: "Run " + service.AppName + " service",
11-
Long: "Run " + service.AppName + " service",
12-
Run: func(cmd *cobra.Command, args []string) {
13-
service.InitializeDiary(test, level, rate)
14-
service.Execute(limit, test, natsUri, compileNatsOptions(), service.M{
15-
// todo: add custom args here
16-
})
17-
},
18-
}
19-
209
func init() {
10+
var level string
11+
var rate int
12+
var limit int
13+
var test bool
14+
// todo: add custom flag variables here
15+
16+
var runCmd = &cobra.Command{
17+
Use: "run",
18+
Short: "Run " + service.AppName + " service",
19+
Long: "Run " + service.AppName + " service",
20+
Run: func(cmd *cobra.Command, args []string) {
21+
service.InitializeDiary(test, level, rate)
22+
service.Execute(limit, test, _base.NatsUri, _base.CompileNatsOptions(), service.M{
23+
// todo: link custom flags to arg values here, example: "custom": custom,
24+
})
25+
},
26+
}
27+
28+
// set the service's environment configurations via many command-line-interface (CLI) arguments
2129
runCmd.Flags().StringVarP(&level, "lvl", "l", "trace", "The logging level ['trace', 'debug', 'info', 'notice', 'warning', 'error', 'fatal'] that service is running in")
2230
runCmd.Flags().IntVarP(&rate, "rate", "r", 1000, "The sample rate of the trace logs used for performance auditing [set to -1 to log every trace]")
2331
runCmd.Flags().IntVarP(&limit, "limit", "x", 1000, "The messages per second that each topic worker will be limited to [set to 0 or less for maximum throughput]")
2432
runCmd.Flags().BoolVar(&test, "test", false, "A flag indicating if service should enter into test mode")
25-
runCmd.Flags().BoolVar(&disableTls, "disable-tls", false, "A flag indicating if service should disable tls encryption")
33+
// todo: add custom CLI flags here
2634

27-
rootCmd.AddCommand(runCmd)
35+
_base.RootCmd.AddCommand(runCmd)
2836
}

generator.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package main
22

3-
import "service/cmd"
3+
import (
4+
"service/cmd"
5+
)
46

57
func main() {
68
cmd.Execute()

service/_base/.gitkeep

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The base uniform implementation of the service layer, don't add, edit or remove any of these files

service/base.command.go renamed to service/_base/command.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
package service
1+
package _base
22

33
import (
44
"fmt"
55
"github.com/go-diary/diary"
66
"github.com/go-uniform/uniform"
77
"github.com/nats-io/go-nats"
8+
"time"
89
)
910

10-
func Command(cmd, natsUri string, natsOptions []nats.Option, args map[string]string) {
11+
func Command(cmd string, timeout time.Duration, natsUri string, natsOptions []nats.Option, args map[string]string, responseHandler func([]byte)) {
1112
defer func() {
1213
if r := recover(); r != nil {
1314
if _, e := fmt.Printf("%v", r); e != nil {
@@ -28,9 +29,14 @@ func Command(cmd, natsUri string, natsOptions []nats.Option, args map[string]str
2829
defer c.Close()
2930

3031
d.Page(-1, traceRate, true, AppName, nil, "", "", nil, func(p diary.IPage) {
31-
p.Info(cmd, nil)
32-
if err := c.Publish(p, local(command(cmd)), uniform.Request{
32+
if err := c.Request(p, TargetCommand(cmd), timeout, uniform.Request{
3333
Parameters: args,
34+
}, func(r uniform.IRequest, p diary.IPage) {
35+
if responseHandler != nil {
36+
var data []byte
37+
r.Read(&data)
38+
responseHandler(data)
39+
}
3440
}); err != nil {
3541
panic(err)
3642
}

0 commit comments

Comments
 (0)