working commit
This commit is contained in:
117
cmd/minilbadm/hash/hashcmd.go
Normal file
117
cmd/minilbadm/hash/hashcmd.go
Normal file
@@ -0,0 +1,117 @@
|
||||
package hash
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"helmet/pkg/passwd"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
type Tool struct {
|
||||
cmd *cobra.Command
|
||||
createHashParams CreateHashParams
|
||||
checkHashParams CheckHashParams
|
||||
}
|
||||
|
||||
func NewTool() *Tool {
|
||||
tool := &Tool{}
|
||||
tool.cmd = &cobra.Command{
|
||||
Use: "hash",
|
||||
Short: "Hash opeation",
|
||||
}
|
||||
createHashCmd := &cobra.Command{
|
||||
Use: "create password",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Short: "Create authentification hash",
|
||||
RunE: tool.CreateHash,
|
||||
}
|
||||
checkHashCmd := &cobra.Command{
|
||||
Use: "check password hash",
|
||||
Args: cobra.ExactArgs(2),
|
||||
Short: "Check authentification hash",
|
||||
RunE: tool.CheckHash,
|
||||
}
|
||||
|
||||
tool.cmd.AddCommand(createHashCmd)
|
||||
tool.cmd.AddCommand(checkHashCmd)
|
||||
return tool
|
||||
}
|
||||
|
||||
func (tool *Tool) GetCmd() *cobra.Command {
|
||||
return tool.cmd
|
||||
}
|
||||
|
||||
type CreateHashParams struct {
|
||||
Password string
|
||||
}
|
||||
type CreateHashResult struct {
|
||||
Hash string `json:"hash"`
|
||||
}
|
||||
|
||||
func (tool *Tool) CreateHash(cmd *cobra.Command, args []string) error {
|
||||
tool.createHashParams.Password = args[0]
|
||||
res, err := tool.createHash(&tool.createHashParams)
|
||||
printResponse(res, err)
|
||||
return err
|
||||
}
|
||||
|
||||
func (tool *Tool) createHash(params *CreateHashParams) (*CreateHashResult, error) {
|
||||
var err error
|
||||
res := &CreateHashResult{}
|
||||
if params.Password == "" {
|
||||
err = fmt.Errorf("Empty password")
|
||||
return res, err
|
||||
}
|
||||
res.Hash = passwd.MakeSHA256Hash([]byte(params.Password))
|
||||
if !passwd.PasswordMatch([]byte(params.Password), res.Hash) {
|
||||
err = fmt.Errorf("Error hash validation")
|
||||
return res, err
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
|
||||
type CheckHashParams struct {
|
||||
Password string
|
||||
Hash string
|
||||
}
|
||||
type CheckHashResult struct {
|
||||
Match bool `json:"match"`
|
||||
}
|
||||
|
||||
func (tool *Tool) CheckHash(cmd *cobra.Command, args []string) error {
|
||||
tool.checkHashParams.Password = args[0]
|
||||
tool.checkHashParams.Hash = args[1]
|
||||
res, err := tool.checkHash(&tool.checkHashParams)
|
||||
printResponse(res, err)
|
||||
return err
|
||||
}
|
||||
|
||||
func (tool *Tool) checkHash(params *CheckHashParams) (*CheckHashResult, error) {
|
||||
var err error
|
||||
res := &CheckHashResult{}
|
||||
if params.Password == "" {
|
||||
err = fmt.Errorf("Empty password")
|
||||
return res, err
|
||||
}
|
||||
res.Match = passwd.PasswordMatch([]byte(params.Password), params.Hash)
|
||||
return res, err
|
||||
}
|
||||
|
||||
func printResponse(res any, err error) {
|
||||
type Response struct {
|
||||
Error bool `json:"error" json:"error"`
|
||||
Message string `json:"message,omitempty" json:"message,omitempty"`
|
||||
Result any `json:"result,omitempty" json:"result,omitempty"`
|
||||
}
|
||||
resp := Response{}
|
||||
if err != nil {
|
||||
resp.Error = true
|
||||
resp.Message = err.Error()
|
||||
} else {
|
||||
resp.Result = res
|
||||
}
|
||||
respBytes, _ := yaml.Marshal(resp)
|
||||
fmt.Printf("---\n%s\n", string(respBytes))
|
||||
}
|
||||
22
cmd/minilbadm/main.go
Normal file
22
cmd/minilbadm/main.go
Normal file
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
|
||||
*
|
||||
* This work is published and licensed under a Creative Commons
|
||||
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
|
||||
*
|
||||
* Distribution of this work is permitted, but commercial use and
|
||||
* modifications are strictly prohibited.
|
||||
*/
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
tool := NewTool()
|
||||
err := tool.Exec(os.Args[1:])
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
44
cmd/minilbadm/tool.go
Normal file
44
cmd/minilbadm/tool.go
Normal file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
|
||||
*
|
||||
* This work is published and licensed under a Creative Commons
|
||||
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
|
||||
*
|
||||
* Distribution of this work is permitted, but commercial use and
|
||||
* modifications are strictly prohibited.
|
||||
*/
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"helmet/cmd/minilbadm/hash"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
type Tool struct {
|
||||
cmd *cobra.Command
|
||||
hashTool *hash.Tool
|
||||
}
|
||||
|
||||
func NewTool() *Tool {
|
||||
execName := filepath.Base(os.Args[0])
|
||||
tool := &Tool{}
|
||||
tool.cmd = &cobra.Command{
|
||||
Use: execName,
|
||||
Short: "\nHash tool",
|
||||
SilenceUsage: true,
|
||||
}
|
||||
tool.cmd.CompletionOptions.DisableDefaultCmd = true
|
||||
|
||||
tool.hashTool = hash.NewTool()
|
||||
tool.cmd.AddCommand(tool.hashTool.GetCmd())
|
||||
return tool
|
||||
}
|
||||
|
||||
func (tool *Tool) Exec(args []string) error {
|
||||
tool.cmd.SetArgs(args)
|
||||
return tool.cmd.Execute()
|
||||
}
|
||||
51
cmd/minilbctl/forwarder/createforw.go
Normal file
51
cmd/minilbctl/forwarder/createforw.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package forwarder
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"helmet/pkg/client"
|
||||
"helmet/pkg/mlbctl"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
type CreateForwarderParams struct {
|
||||
Hostname string
|
||||
Lport uint32
|
||||
Dport uint32
|
||||
Addresses []string
|
||||
}
|
||||
type CreateForwarderResult struct{}
|
||||
|
||||
func (tool *Tool) CreateForwarder(cmd *cobra.Command, args []string) {
|
||||
tool.createForwarderParams.Hostname = args[0]
|
||||
res, err := tool.createForwarder(&tool.createForwarderParams)
|
||||
printResponse(res, err)
|
||||
}
|
||||
|
||||
func (tool *Tool) createForwarder(params *CreateForwarderParams) (*CreateForwarderResult, error) {
|
||||
var err error
|
||||
res := &CreateForwarderResult{}
|
||||
ref, err := client.NewReferer(params.Hostname)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
authCred := client.NewAuthCredential(ref.Userinfo())
|
||||
conn, cli, err := client.NewClient(ref.Hostinfo(), authCred)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
defer conn.Close()
|
||||
ctx, _ := context.WithTimeout(context.Background(), 1*time.Second)
|
||||
opReq := &mlbctl.CreateForwarderParams{
|
||||
Lport: params.Lport,
|
||||
Dport: params.Dport,
|
||||
Destinations: params.Addresses,
|
||||
}
|
||||
_, err = cli.CreateForwarder(ctx, opReq)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
47
cmd/minilbctl/forwarder/deleteforw.go
Normal file
47
cmd/minilbctl/forwarder/deleteforw.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package forwarder
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"helmet/pkg/client"
|
||||
"helmet/pkg/mlbctl"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
type DeleteForwarderParams struct {
|
||||
Hostname string
|
||||
Lport uint32
|
||||
}
|
||||
type DeleteForwarderResult struct{}
|
||||
|
||||
func (tool *Tool) DeleteForwarder(cmd *cobra.Command, args []string) {
|
||||
tool.deleteForwarderParams.Hostname = args[0]
|
||||
res, err := tool.deleteForwarder(&tool.deleteForwarderParams)
|
||||
printResponse(res, err)
|
||||
}
|
||||
|
||||
func (tool *Tool) deleteForwarder(params *DeleteForwarderParams) (*DeleteForwarderResult, error) {
|
||||
var err error
|
||||
res := &DeleteForwarderResult{}
|
||||
ref, err := client.NewReferer(params.Hostname)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
authCred := client.NewAuthCredential(ref.Userinfo())
|
||||
conn, cli, err := client.NewClient(ref.Hostinfo(), authCred)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
defer conn.Close()
|
||||
ctx, _ := context.WithTimeout(context.Background(), 1*time.Second)
|
||||
opReq := &mlbctl.DeleteForwarderParams{
|
||||
Lport: params.Lport,
|
||||
}
|
||||
_, err = cli.DeleteForwarder(ctx, opReq)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
78
cmd/minilbctl/forwarder/forwcmd.go
Normal file
78
cmd/minilbctl/forwarder/forwcmd.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package forwarder
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
type Tool struct {
|
||||
cmd *cobra.Command
|
||||
listForwardersParams ListForwardersParams
|
||||
createForwarderParams CreateForwarderParams
|
||||
deleteForwarderParams DeleteForwarderParams
|
||||
}
|
||||
|
||||
func NewTool() *Tool {
|
||||
tool := &Tool{}
|
||||
tool.cmd = &cobra.Command{
|
||||
Use: "forwarder",
|
||||
Short: "Forwarder operation",
|
||||
Aliases: []string{"forw"},
|
||||
}
|
||||
listForwardersCmd := &cobra.Command{
|
||||
Use: "list",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Short: "List forwarders",
|
||||
Run: tool.ListForwarders,
|
||||
}
|
||||
tool.cmd.AddCommand(listForwardersCmd)
|
||||
|
||||
createForwarderCmd := &cobra.Command{
|
||||
Use: "create",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Short: "Create forwarder",
|
||||
Run: tool.CreateForwarder,
|
||||
}
|
||||
createForwarderCmd.Flags().Uint32VarP(&tool.createForwarderParams.Lport, "lport", "L", 0, "Listening port")
|
||||
createForwarderCmd.Flags().Uint32VarP(&tool.createForwarderParams.Dport, "dport", "D", 0, "Destination port")
|
||||
createForwarderCmd.Flags().StringArrayVarP(&tool.createForwarderParams.Addresses, "dests", "A", []string{}, "Destination address list")
|
||||
createForwarderCmd.MarkFlagRequired("lport")
|
||||
createForwarderCmd.MarkFlagRequired("dport")
|
||||
createForwarderCmd.MarkFlagRequired("dests")
|
||||
tool.cmd.AddCommand(createForwarderCmd)
|
||||
|
||||
deleteForwarderCmd := &cobra.Command{
|
||||
Use: "delete",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Short: "Delete forwarder",
|
||||
Run: tool.CreateForwarder,
|
||||
}
|
||||
deleteForwarderCmd.Flags().Uint32VarP(&tool.deleteForwarderParams.Lport, "lport", "L", 0, "Listening port")
|
||||
deleteForwarderCmd.MarkFlagRequired("lport")
|
||||
tool.cmd.AddCommand(deleteForwarderCmd)
|
||||
|
||||
return tool
|
||||
}
|
||||
|
||||
func (tool *Tool) GetCmd() *cobra.Command {
|
||||
return tool.cmd
|
||||
}
|
||||
|
||||
func printResponse(res any, err error) {
|
||||
type Response struct {
|
||||
Error bool `json:"error" json:"error"`
|
||||
Message string `json:"message,omitempty" json:"message,omitempty"`
|
||||
Result any `json:"result,omitempty" json:"result,omitempty"`
|
||||
}
|
||||
resp := Response{}
|
||||
if err != nil {
|
||||
resp.Error = true
|
||||
resp.Message = err.Error()
|
||||
} else {
|
||||
resp.Result = res
|
||||
}
|
||||
respBytes, _ := yaml.Marshal(resp)
|
||||
fmt.Printf("---\n%s\n", string(respBytes))
|
||||
}
|
||||
47
cmd/minilbctl/forwarder/listforw.go
Normal file
47
cmd/minilbctl/forwarder/listforw.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package forwarder
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"helmet/pkg/client"
|
||||
"helmet/pkg/mlbctl"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
type ListForwardersParams struct {
|
||||
Hostname string
|
||||
}
|
||||
type ListForwardersResult struct {
|
||||
Forwarders []*mlbctl.Forwarder `json:"forwarders,omitempty"`
|
||||
}
|
||||
|
||||
func (tool *Tool) ListForwarders(cmd *cobra.Command, args []string) {
|
||||
tool.listForwardersParams.Hostname = args[0]
|
||||
res, err := tool.listForwarders(&tool.listForwardersParams)
|
||||
printResponse(res, err)
|
||||
}
|
||||
|
||||
func (tool *Tool) listForwarders(params *ListForwardersParams) (*ListForwardersResult, error) {
|
||||
var err error
|
||||
res := &ListForwardersResult{}
|
||||
ref, err := client.NewReferer(params.Hostname)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
authCred := client.NewAuthCredential(ref.Userinfo())
|
||||
conn, cli, err := client.NewClient(ref.Hostinfo(), authCred)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
defer conn.Close()
|
||||
ctx, _ := context.WithTimeout(context.Background(), 1*time.Second)
|
||||
opReq := &mlbctl.ListForwardersParams{}
|
||||
opRes, err := cli.ListForwarders(ctx, opReq)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
res.Forwarders = opRes.Forwarders
|
||||
return res, err
|
||||
}
|
||||
22
cmd/minilbctl/main.go
Normal file
22
cmd/minilbctl/main.go
Normal file
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
|
||||
*
|
||||
* This work is published and licensed under a Creative Commons
|
||||
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
|
||||
*
|
||||
* Distribution of this work is permitted, but commercial use and
|
||||
* modifications are strictly prohibited.
|
||||
*/
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
tool := NewTool()
|
||||
err := tool.Exec(os.Args[1:])
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
91
cmd/minilbctl/service/srvcmd.go
Normal file
91
cmd/minilbctl/service/srvcmd.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"helmet/pkg/client"
|
||||
"helmet/pkg/mlbctl"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
type Tool struct {
|
||||
cmd *cobra.Command
|
||||
serviceHelloParams ServiceHelloParams
|
||||
}
|
||||
|
||||
func NewTool() *Tool {
|
||||
tool := &Tool{}
|
||||
tool.cmd = &cobra.Command{
|
||||
Use: "service",
|
||||
Short: "Service operation",
|
||||
}
|
||||
serviceHelloCmd := &cobra.Command{
|
||||
Use: "hello",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Short: "Send and receive hello",
|
||||
Run: tool.ServiceHello,
|
||||
}
|
||||
tool.cmd.AddCommand(serviceHelloCmd)
|
||||
return tool
|
||||
}
|
||||
|
||||
func (tool *Tool) GetCmd() *cobra.Command {
|
||||
return tool.cmd
|
||||
}
|
||||
|
||||
type ServiceHelloParams struct {
|
||||
Hostname string
|
||||
}
|
||||
type ServiceHelloResult struct {
|
||||
Message string `json:"message,omitempty"`
|
||||
}
|
||||
|
||||
func (tool *Tool) ServiceHello(cmd *cobra.Command, args []string) {
|
||||
tool.serviceHelloParams.Hostname = args[0]
|
||||
res, err := tool.serviceHello(&tool.serviceHelloParams)
|
||||
printResponse(res, err)
|
||||
}
|
||||
|
||||
func (tool *Tool) serviceHello(params *ServiceHelloParams) (*ServiceHelloResult, error) {
|
||||
var err error
|
||||
res := &ServiceHelloResult{}
|
||||
ref, err := client.NewReferer(params.Hostname)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
authCred := client.NewAuthCredential(ref.Userinfo())
|
||||
conn, cli, err := client.NewClient(ref.Hostinfo(), authCred)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
defer conn.Close()
|
||||
ctx, _ := context.WithTimeout(context.Background(), 1*time.Second)
|
||||
opReq := &mlbctl.GetHelloParams{}
|
||||
opRes, err := cli.GetHello(ctx, opReq)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
res.Message = opRes.Message
|
||||
return res, err
|
||||
}
|
||||
|
||||
func printResponse(res any, err error) {
|
||||
type Response struct {
|
||||
Error bool `json:"error" json:"error"`
|
||||
Message string `json:"message,omitempty" json:"message,omitempty"`
|
||||
Result any `json:"result,omitempty" json:"result,omitempty"`
|
||||
}
|
||||
resp := Response{}
|
||||
if err != nil {
|
||||
resp.Error = true
|
||||
resp.Message = err.Error()
|
||||
} else {
|
||||
resp.Result = res
|
||||
}
|
||||
respBytes, _ := yaml.Marshal(resp)
|
||||
fmt.Printf("---\n%s\n", string(respBytes))
|
||||
}
|
||||
49
cmd/minilbctl/tool.go
Normal file
49
cmd/minilbctl/tool.go
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
|
||||
*
|
||||
* This work is published and licensed under a Creative Commons
|
||||
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
|
||||
*
|
||||
* Distribution of this work is permitted, but commercial use and
|
||||
* modifications are strictly prohibited.
|
||||
*/
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"helmet/cmd/minilbctl/forwarder"
|
||||
"helmet/cmd/minilbctl/service"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
type Tool struct {
|
||||
cmd *cobra.Command
|
||||
forwarderTool *forwarder.Tool
|
||||
serviceTool *service.Tool
|
||||
}
|
||||
|
||||
func NewTool() *Tool {
|
||||
execName := filepath.Base(os.Args[0])
|
||||
tool := &Tool{}
|
||||
tool.cmd = &cobra.Command{
|
||||
Use: execName,
|
||||
Short: "\nService tools",
|
||||
SilenceUsage: true,
|
||||
}
|
||||
tool.cmd.CompletionOptions.DisableDefaultCmd = true
|
||||
|
||||
tool.serviceTool = service.NewTool()
|
||||
tool.cmd.AddCommand(tool.serviceTool.GetCmd())
|
||||
|
||||
tool.forwarderTool = forwarder.NewTool()
|
||||
tool.cmd.AddCommand(tool.forwarderTool.GetCmd())
|
||||
return tool
|
||||
}
|
||||
|
||||
func (tool *Tool) Exec(args []string) error {
|
||||
tool.cmd.SetArgs(args)
|
||||
return tool.cmd.Execute()
|
||||
}
|
||||
28
cmd/minilbd/main.go
Normal file
28
cmd/minilbd/main.go
Normal file
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
|
||||
*
|
||||
* This work is published and licensed under a Creative Commons
|
||||
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
|
||||
*
|
||||
* Distribution of this work is permitted, but commercial use and
|
||||
* modifications are strictly prohibited.
|
||||
*/
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"helmet/app/logger"
|
||||
"helmet/cmd/minilbd/starter"
|
||||
)
|
||||
|
||||
func main() {
|
||||
log := logger.NewLogger("main")
|
||||
sta := starter.NewStarter()
|
||||
err := sta.Exec()
|
||||
if err != nil {
|
||||
log.Errorf("%v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
os.Exit(0)
|
||||
}
|
||||
83
cmd/minilbd/starter/starter.go
Normal file
83
cmd/minilbd/starter/starter.go
Normal file
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
|
||||
*
|
||||
* This work is published and licensed under a Creative Commons
|
||||
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
|
||||
*
|
||||
* Distribution of this work is permitted, but commercial use and
|
||||
* modifications are strictly prohibited.
|
||||
*/
|
||||
package starter
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"helmet/app/server"
|
||||
"helmet/pkg/client"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
type Starter struct {
|
||||
runAsDaemon bool
|
||||
port uint32
|
||||
cmd *cobra.Command
|
||||
srv *server.Server
|
||||
}
|
||||
|
||||
func NewStarter() *Starter {
|
||||
execName := filepath.Base(os.Args[0])
|
||||
sta := &Starter{}
|
||||
cmd := &cobra.Command{
|
||||
Use: execName,
|
||||
Short: "\nProxy service",
|
||||
SilenceUsage: true,
|
||||
RunE: sta.run,
|
||||
}
|
||||
cmd.CompletionOptions.DisableDefaultCmd = true
|
||||
cmd.Flags().BoolVarP(&sta.runAsDaemon, "asDaemon", "D", false, "Run service as daemon")
|
||||
cmd.Flags().Uint32VarP(&sta.port, "port", "P", client.DefaultServicePort, "Service port")
|
||||
|
||||
sta.cmd = cmd
|
||||
return sta
|
||||
}
|
||||
|
||||
func (sta *Starter) GetCmd() *cobra.Command {
|
||||
return sta.cmd
|
||||
}
|
||||
|
||||
func (sta *Starter) run(cmd *cobra.Command, args []string) error {
|
||||
var err error
|
||||
srv, err := server.NewServer()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = srv.Configure()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
srv.Config().AsDaemon = sta.runAsDaemon
|
||||
srv.Config().Service.Port = sta.port
|
||||
|
||||
err = srv.Daemonize()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = srv.Build()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
srv.Rotator()
|
||||
err = srv.Run()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sta.srv = srv
|
||||
return err
|
||||
}
|
||||
|
||||
func (sta *Starter) Exec() error {
|
||||
return sta.cmd.Execute()
|
||||
}
|
||||
Reference in New Issue
Block a user