-
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.
- Loading branch information
Showing
30 changed files
with
348 additions
and
202 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
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
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,80 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"os/signal" | ||
"strconv" | ||
"strings" | ||
"syscall" | ||
"time" | ||
|
||
cfg "hk4e/common/config" | ||
|
||
"github.com/nats-io/nats-server/v2/server" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NatsCmd() *cobra.Command { | ||
var configFile string | ||
c := &cobra.Command{ | ||
Use: "nats", | ||
Short: "nats server", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return RunNatsServer(configFile) | ||
}, | ||
} | ||
c.Flags().StringVar(&configFile, "config", "application.toml", "config file") | ||
return c | ||
} | ||
|
||
func RunNatsServer(configFile string) error { | ||
cfg.InitConfig(configFile) | ||
natsAddr := strings.ReplaceAll(cfg.GetConfig().MQ.NatsUrl, "nats://", "") | ||
if strings.Contains(natsAddr, ",") { | ||
return errors.New("not support nats cluster") | ||
} | ||
split := strings.Split(natsAddr, ":") | ||
if len(split) != 2 { | ||
return errors.New("nats addr format error") | ||
} | ||
host := split[0] | ||
port, err := strconv.Atoi(split[1]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
opts := &server.Options{ | ||
Host: host, | ||
Port: port, | ||
NoLog: false, | ||
NoSigs: true, | ||
MaxControlLine: 4096, | ||
DisableShortFirstPing: true, | ||
Trace: true, | ||
Debug: true, | ||
} | ||
natsServer, err := server.NewServer(opts) | ||
if err != nil { | ||
return err | ||
} | ||
natsServer.ConfigureLogger() | ||
go natsServer.Start() | ||
ok := natsServer.ReadyForConnections(time.Second * 5) | ||
if !ok { | ||
return errors.New("nats server start error") | ||
} | ||
|
||
c := make(chan os.Signal, 1) | ||
signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT) | ||
for { | ||
s := <-c | ||
switch s { | ||
case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT: | ||
return nil | ||
case syscall.SIGHUP: | ||
default: | ||
return 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,23 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
|
||
"context" | ||
|
||
"hk4e/robot/app" | ||
) | ||
|
||
func RobotCmd() *cobra.Command { | ||
var configFile string | ||
app.APPVERSION = VERSION | ||
c := &cobra.Command{ | ||
Use: "robot", | ||
Short: "robot server", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return app.Run(context.Background(), configFile) | ||
}, | ||
} | ||
c.Flags().StringVar(&configFile, "config", "application.toml", "config file") | ||
return c | ||
} |
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
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
Oops, something went wrong.