-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
planner-api: Split the main into commands #57
Merged
+165
−80
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -1,85 +1,10 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
|
||
"github.com/kubev2v/migration-planner/internal/api_server/agentserver" | ||
"github.com/kubev2v/migration-planner/internal/config" | ||
"github.com/kubev2v/migration-planner/pkg/log" | ||
"github.com/sirupsen/logrus" | ||
|
||
apiserver "github.com/kubev2v/migration-planner/internal/api_server" | ||
|
||
"github.com/kubev2v/migration-planner/internal/store" | ||
) | ||
import "os" | ||
|
||
func main() { | ||
log := log.InitLogs() | ||
log.Println("Starting API service") | ||
defer log.Println("API service stopped") | ||
|
||
cfg, err := config.LoadOrGenerate(config.ConfigFile()) | ||
err := rootCmd.Execute() | ||
if err != nil { | ||
log.Fatalf("reading configuration: %v", err) | ||
} | ||
log.Printf("Using config: %s", cfg) | ||
|
||
logLvl, err := logrus.ParseLevel(cfg.Service.LogLevel) | ||
if err != nil { | ||
logLvl = logrus.InfoLevel | ||
} | ||
log.SetLevel(logLvl) | ||
|
||
log.Println("Initializing data store") | ||
db, err := store.InitDB(cfg, log) | ||
if err != nil { | ||
log.Fatalf("initializing data store: %v", err) | ||
} | ||
|
||
store := store.NewStore(db, log.WithField("pkg", "store")) | ||
defer store.Close() | ||
|
||
if err := store.InitialMigration(); err != nil { | ||
log.Fatalf("running initial migration: %v", err) | ||
} | ||
|
||
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGHUP, syscall.SIGTERM, syscall.SIGQUIT) | ||
go func() { | ||
listener, err := newListener(cfg.Service.Address) | ||
if err != nil { | ||
log.Fatalf("creating listener: %s", err) | ||
} | ||
|
||
server := apiserver.New(log, cfg, store, listener) | ||
if err := server.Run(ctx); err != nil { | ||
log.Fatalf("Error running server: %s", err) | ||
} | ||
cancel() | ||
}() | ||
|
||
go func() { | ||
listener, err := newListener(cfg.Service.AgentEndpointAddress) | ||
if err != nil { | ||
log.Fatalf("creating listener: %s", err) | ||
} | ||
|
||
agentserver := agentserver.New(log, cfg, store, listener) | ||
if err := agentserver.Run(ctx); err != nil { | ||
log.Fatalf("Error running server: %s", err) | ||
} | ||
cancel() | ||
}() | ||
|
||
<-ctx.Done() | ||
} | ||
|
||
func newListener(address string) (net.Listener, error) { | ||
if address == "" { | ||
address = "localhost:0" | ||
os.Exit(1) | ||
} | ||
return net.Listen("tcp", address) | ||
} |
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,50 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/kubev2v/migration-planner/internal/config" | ||
"github.com/kubev2v/migration-planner/internal/store" | ||
"github.com/kubev2v/migration-planner/pkg/log" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var migrateCmd = &cobra.Command{ | ||
Use: "migrate", | ||
Short: "Migrate the db", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
log := log.InitLogs() | ||
log.Println("Starting API service") | ||
defer log.Println("Db migrated") | ||
|
||
if configFile == "" { | ||
configFile = config.ConfigFile() | ||
} | ||
|
||
cfg, err := config.Load(configFile) | ||
if err != nil { | ||
log.Fatalf("reading configuration: %v", err) | ||
} | ||
log.Printf("Using config: %s", cfg) | ||
|
||
logLvl, err := logrus.ParseLevel(cfg.Service.LogLevel) | ||
if err != nil { | ||
logLvl = logrus.InfoLevel | ||
} | ||
log.SetLevel(logLvl) | ||
|
||
log.Println("Initializing data store") | ||
db, err := store.InitDB(cfg, log) | ||
if err != nil { | ||
log.Fatalf("initializing data store: %v", err) | ||
} | ||
|
||
store := store.NewStore(db, log.WithField("pkg", "store")) | ||
defer store.Close() | ||
|
||
if err := store.InitialMigration(); err != nil { | ||
log.Fatalf("running initial migration: %v", err) | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package main | ||
|
||
import "github.com/spf13/cobra" | ||
|
||
var ( | ||
configFile string | ||
) | ||
|
||
var rootCmd = &cobra.Command{ | ||
Use: "planner-api", | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(migrateCmd) | ||
rootCmd.AddCommand(runCmd) | ||
|
||
rootCmd.PersistentFlags().StringVarP(&configFile, "config", "c", "", "Path to configuration file") | ||
} |
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,92 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
|
||
apiserver "github.com/kubev2v/migration-planner/internal/api_server" | ||
"github.com/kubev2v/migration-planner/internal/api_server/agentserver" | ||
"github.com/kubev2v/migration-planner/internal/config" | ||
"github.com/kubev2v/migration-planner/internal/store" | ||
"github.com/kubev2v/migration-planner/pkg/log" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var runCmd = &cobra.Command{ | ||
Use: "run", | ||
Short: "Run the planner api", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
log := log.InitLogs() | ||
log.Println("Starting API service") | ||
defer log.Println("API service stopped") | ||
|
||
if configFile == "" { | ||
configFile = config.ConfigFile() | ||
} | ||
cfg, err := config.NewFromFile(configFile) | ||
if err != nil { | ||
log.Fatalf("reading configuration: %v", err) | ||
} | ||
log.Printf("Using config: %s", cfg) | ||
|
||
logLvl, err := logrus.ParseLevel(cfg.Service.LogLevel) | ||
if err != nil { | ||
logLvl = logrus.InfoLevel | ||
} | ||
log.SetLevel(logLvl) | ||
|
||
log.Println("Initializing data store") | ||
db, err := store.InitDB(cfg, log) | ||
if err != nil { | ||
log.Fatalf("initializing data store: %v", err) | ||
} | ||
|
||
store := store.NewStore(db, log.WithField("pkg", "store")) | ||
defer store.Close() | ||
|
||
if err := store.InitialMigration(); err != nil { | ||
log.Fatalf("running initial migration: %v", err) | ||
} | ||
|
||
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGHUP, syscall.SIGTERM, syscall.SIGQUIT) | ||
go func() { | ||
defer cancel() | ||
listener, err := newListener(cfg.Service.Address) | ||
if err != nil { | ||
log.Fatalf("creating listener: %s", err) | ||
} | ||
|
||
server := apiserver.New(log, cfg, store, listener) | ||
if err := server.Run(ctx); err != nil { | ||
log.Fatalf("Error running server: %s", err) | ||
} | ||
}() | ||
|
||
go func() { | ||
defer cancel() | ||
listener, err := newListener(cfg.Service.AgentEndpointAddress) | ||
if err != nil { | ||
log.Fatalf("creating listener: %s", err) | ||
} | ||
|
||
agentserver := agentserver.New(log, cfg, store, listener) | ||
if err := agentserver.Run(ctx); err != nil { | ||
log.Fatalf("Error running server: %s", err) | ||
} | ||
}() | ||
|
||
<-ctx.Done() | ||
return nil | ||
}, | ||
} | ||
|
||
func newListener(address string) (net.Listener, error) { | ||
if address == "" { | ||
address = "localhost:0" | ||
} | ||
return net.Listen("tcp", address) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry that I didn't notice before, but wouldn't be better to use
cmd/planner-api/...
instead ofcmd/planner-api/*.go
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does not work for me.