Add new cli command serve to start the server
- Introduced NewServeCommand in CLI - Added error handling for server initialization - Removed checkserver.go - Cmd push as pseudocode for now
- commit
- e322989f33bd63f7c1b22df441cab9e59f9c1437
- author
- bnema · · 2023-10-25 09:10 UTC
- parent
- cf93cff
- signature
- unsigned
9 files · +87 -11
M cmd/cli/main.go +11 -5
| @@ -5,18 +5,19 @@ import ( | |||
| 5 | 5 | ||
| 6 | 6 | "github.com/bnema/gordon/internal/cli" | |
| 7 | 7 | "github.com/bnema/gordon/internal/cli/cmd" | |
| 8 | + | "github.com/bnema/gordon/internal/server" | |
| 8 | 9 | "github.com/spf13/cobra" | |
| 9 | 10 | ) | |
| 10 | 11 | ||
| 11 | 12 | var rootCmd = &cobra.Command{Use: "gordon"} | |
| 12 | 13 | ||
| 13 | - | func InitializeCommands(a *cli.App) { | |
| 14 | - | // rootCmd.AddCommand(cmd.NewHelloCommand(a)) | |
| 14 | + | func InitializeCommands(a *cli.App, s *server.App) { | |
| 15 | + | rootCmd.AddCommand(cmd.NewServeCommand(s)) | |
| 15 | 16 | rootCmd.AddCommand(cmd.NewPingCommand(a)) | |
| 16 | 17 | } | |
| 17 | 18 | ||
| 18 | - | func Execute(a *cli.App) { | |
| 19 | - | InitializeCommands(a) | |
| 19 | + | func Execute(a *cli.App, s *server.App) { | |
| 20 | + | InitializeCommands(a, s) | |
| 20 | 21 | cobra.CheckErr(rootCmd.Execute()) | |
| 21 | 22 | } | |
| 22 | 23 | ||
| @@ -25,5 +26,10 @@ func main() { | |||
| 25 | 26 | if err != nil { | |
| 26 | 27 | fmt.Println("Error initializing app:", err) | |
| 27 | 28 | } | |
| 28 | - | Execute(a) | |
| 29 | + | s, err := server.NewServerApp() | |
| 30 | + | if err != nil { | |
| 31 | + | fmt.Println("Error initializing app:", err) | |
| 32 | + | } | |
| 33 | + | ||
| 34 | + | Execute(a, s) | |
| 29 | 35 | } | |
| @@ -5,18 +5,19 @@ import ( | |||||
| 5 | 5 | ||||
| 6 | "github.com/bnema/gordon/internal/cli" | 6 | "github.com/bnema/gordon/internal/cli" | ||
| 7 | "github.com/bnema/gordon/internal/cli/cmd" | 7 | "github.com/bnema/gordon/internal/cli/cmd" | ||
| 8 | + | "github.com/bnema/gordon/internal/server" | |||
| 8 | "github.com/spf13/cobra" | 9 | "github.com/spf13/cobra" | ||
| 9 | ) | 10 | ) | ||
| 10 | 11 | ||||
| 11 | var rootCmd = &cobra.Command{Use: "gordon"} | 12 | var rootCmd = &cobra.Command{Use: "gordon"} | ||
| 12 | 13 | ||||
| 13 | - | func InitializeCommands(a *cli.App) { | 14 | + | func InitializeCommands(a *cli.App, s *server.App) { |
| 14 | - | // rootCmd.AddCommand(cmd.NewHelloCommand(a)) | 15 | + | rootCmd.AddCommand(cmd.NewServeCommand(s)) |
| 15 | rootCmd.AddCommand(cmd.NewPingCommand(a)) | 16 | rootCmd.AddCommand(cmd.NewPingCommand(a)) | ||
| 16 | } | 17 | } | ||
| 17 | 18 | ||||
| 18 | - | func Execute(a *cli.App) { | 19 | + | func Execute(a *cli.App, s *server.App) { |
| 19 | - | InitializeCommands(a) | 20 | + | InitializeCommands(a, s) |
| 20 | cobra.CheckErr(rootCmd.Execute()) | 21 | cobra.CheckErr(rootCmd.Execute()) | ||
| 21 | } | 22 | } | ||
| 22 | 23 | ||||
| @@ -25,5 +26,10 @@ func main() { | |||||
| 25 | if err != nil { | 26 | if err != nil { | ||
| 26 | fmt.Println("Error initializing app:", err) | 27 | fmt.Println("Error initializing app:", err) | ||
| 27 | } | 28 | } | ||
| 28 | - | Execute(a) | 29 | + | s, err := server.NewServerApp() |
| 30 | + | if err != nil { | |||
| 31 | + | fmt.Println("Error initializing app:", err) | |||
| 32 | + | } | |||
| 33 | + | ||||
| 34 | + | Execute(a, s) | |||
| 29 | } | 35 | } | ||
M cmd/srv/main.go +5 -2
| @@ -21,9 +21,12 @@ func cleanup(a *server.App) { | |||
| 21 | 21 | } | |
| 22 | 22 | ||
| 23 | 23 | func main() { | |
| 24 | - | a := server.NewServerApp() | |
| 24 | + | a, err := server.NewServerApp() | |
| 25 | + | if err != nil { | |
| 26 | + | log.Fatal("Failed to initialize app:", err) | |
| 27 | + | } | |
| 25 | 28 | ||
| 26 | - | _, err := server.InitializeDB(a) | |
| 29 | + | _, err = server.InitializeDB(a) | |
| 27 | 30 | if err != nil { | |
| 28 | 31 | log.Fatal("Failed to initialize database:", err) | |
| 29 | 32 | } | |
| @@ -21,9 +21,12 @@ func cleanup(a *server.App) { | |||||
| 21 | } | 21 | } | ||
| 22 | 22 | ||||
| 23 | func main() { | 23 | func main() { | ||
| 24 | - | a := server.NewServerApp() | 24 | + | a, err := server.NewServerApp() |
| 25 | + | if err != nil { | |||
| 26 | + | log.Fatal("Failed to initialize app:", err) | |||
| 27 | + | } | |||
| 25 | 28 | ||||
| 26 | - | _, err := server.InitializeDB(a) | 29 | + | _, err = server.InitializeDB(a) |
| 27 | if err != nil { | 30 | if err != nil { | ||
| 28 | log.Fatal("Failed to initialize database:", err) | 31 | log.Fatal("Failed to initialize database:", err) | ||
| 29 | } | 32 | } | ||
D internal/cli/cmd/checkserver.go +0 -1
| @@ -1,1 +0,0 @@ | |||
| 1 | - | package cmd | |
| @@ -1,1 +0,0 @@ | |||||
| 1 | - | package cmd | |||
M internal/cli/cmd/push.go +12 -0
| @@ -1,3 +1,15 @@ | |||
| 1 | 1 | package cmd | |
| 2 | 2 | ||
| 3 | 3 | // the push command is used to push a container image to gordon's endpoint | |
| 4 | + | ||
| 5 | + | // Pseudo code of the steps for the push command | |
| 6 | + | ||
| 7 | + | // 1. Extract the container image as .tar and store it in a temporary directory | |
| 8 | + | ||
| 9 | + | // 2. Prepare a payload with the .tar as a byte array, the image name and tag (if any) | |
| 10 | + | ||
| 11 | + | // 3. append the payload to the request body with the type "push" and the token | |
| 12 | + | ||
| 13 | + | // 4. Send the request to the backend | |
| 14 | + | ||
| 15 | + | // 5. If the response is 200, print the success message | |
| @@ -1,3 +1,15 @@ | |||||
| 1 | package cmd | 1 | package cmd | ||
| 2 | 2 | ||||
| 3 | // the push command is used to push a container image to gordon's endpoint | 3 | // the push command is used to push a container image to gordon's endpoint | ||
| 4 | + | ||||
| 5 | + | // Pseudo code of the steps for the push command | |||
| 6 | + | ||||
| 7 | + | // 1. Extract the container image as .tar and store it in a temporary directory | |||
| 8 | + | ||||
| 9 | + | // 2. Prepare a payload with the .tar as a byte array, the image name and tag (if any) | |||
| 10 | + | ||||
| 11 | + | // 3. append the payload to the request body with the type "push" and the token | |||
| 12 | + | ||||
| 13 | + | // 4. Send the request to the backend | |||
| 14 | + | ||||
| 15 | + | // 5. If the response is 200, print the success message | |||
A internal/cli/flags/ports.go +0 -0
no textual change.
A internal/cli/flags/targetdomain.go +3 -0
| @@ -0,0 +1,3 @@ | |||
| 1 | + | package flags | |
| 2 | + | ||
| 3 | + | // Domain flags is -d, --domain | |
| @@ -0,0 +1,3 @@ | |||||
| 1 | + | package flags | |||
| 2 | + | ||||
| 3 | + | // Domain flags is -d, --domain | |||
M internal/cli/handler/startserver.go +48 -1
| @@ -1,9 +1,56 @@ | |||
| 1 | 1 | package handler | |
| 2 | 2 | ||
| 3 | - | import "github.com/bnema/gordon/internal/server" | |
| 3 | + | import ( | |
| 4 | + | "fmt" | |
| 5 | + | "log" | |
| 6 | + | "os" | |
| 7 | + | "os/signal" | |
| 8 | + | "syscall" | |
| 9 | + | ||
| 10 | + | "github.com/bnema/gordon/internal/httpserve" | |
| 11 | + | "github.com/bnema/gordon/internal/server" | |
| 12 | + | "github.com/bnema/gordon/pkg/docker" | |
| 13 | + | "github.com/labstack/echo/v4" | |
| 14 | + | ) | |
| 4 | 15 | ||
| 5 | 16 | // execute cmd/srv/main.go main function | |
| 6 | 17 | ||
| 7 | 18 | func StartServer(a *server.App) error { | |
| 19 | + | _, err := server.InitializeDB(a) | |
| 20 | + | if err != nil { | |
| 21 | + | log.Fatal("Failed to initialize database:", err) | |
| 22 | + | } | |
| 23 | + | ||
| 24 | + | _, err = server.HandleNewTokenInitialization(a) | |
| 25 | + | if err != nil { | |
| 26 | + | log.Print(err) | |
| 27 | + | } | |
| 28 | + | ||
| 29 | + | dockerClient := &docker.DockerClient{} | |
| 30 | + | err = dockerClient.InitializeClient(a.Config.NewDockerConfig()) | |
| 31 | + | if err != nil { | |
| 32 | + | log.Printf("Error: %s", err) | |
| 33 | + | } | |
| 34 | + | ||
| 35 | + | // Setup a channel to capture termination signals | |
| 36 | + | sigs := make(chan os.Signal, 1) | |
| 37 | + | signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) | |
| 38 | + | ||
| 39 | + | go func() { | |
| 40 | + | sig := <-sigs | |
| 41 | + | log.Println("Received signal:", sig) | |
| 42 | + | os.Exit(0) | |
| 43 | + | }() | |
| 44 | + | ||
| 45 | + | e := echo.New() | |
| 46 | + | e.HideBanner = true | |
| 47 | + | e.HidePort = true | |
| 48 | + | e = httpserve.RegisterRoutes(e, a) | |
| 49 | + | ||
| 50 | + | log.Println("Starting server on port", a.Config.Http.Port) | |
| 51 | + | if err := e.Start(fmt.Sprintf(":%d", a.Config.Http.Port)); err != nil { | |
| 52 | + | log.Fatal("Server error:", err) | |
| 53 | + | } | |
| 54 | + | ||
| 8 | 55 | return nil | |
| 9 | 56 | } | |
| @@ -1,9 +1,56 @@ | |||||
| 1 | package handler | 1 | package handler | ||
| 2 | 2 | ||||
| 3 | - | import "github.com/bnema/gordon/internal/server" | 3 | + | import ( |
| 4 | + | "fmt" | |||
| 5 | + | "log" | |||
| 6 | + | "os" | |||
| 7 | + | "os/signal" | |||
| 8 | + | "syscall" | |||
| 9 | + | ||||
| 10 | + | "github.com/bnema/gordon/internal/httpserve" | |||
| 11 | + | "github.com/bnema/gordon/internal/server" | |||
| 12 | + | "github.com/bnema/gordon/pkg/docker" | |||
| 13 | + | "github.com/labstack/echo/v4" | |||
| 14 | + | ) | |||
| 4 | 15 | ||||
| 5 | // execute cmd/srv/main.go main function | 16 | // execute cmd/srv/main.go main function | ||
| 6 | 17 | ||||
| 7 | func StartServer(a *server.App) error { | 18 | func StartServer(a *server.App) error { | ||
| 19 | + | _, err := server.InitializeDB(a) | |||
| 20 | + | if err != nil { | |||
| 21 | + | log.Fatal("Failed to initialize database:", err) | |||
| 22 | + | } | |||
| 23 | + | ||||
| 24 | + | _, err = server.HandleNewTokenInitialization(a) | |||
| 25 | + | if err != nil { | |||
| 26 | + | log.Print(err) | |||
| 27 | + | } | |||
| 28 | + | ||||
| 29 | + | dockerClient := &docker.DockerClient{} | |||
| 30 | + | err = dockerClient.InitializeClient(a.Config.NewDockerConfig()) | |||
| 31 | + | if err != nil { | |||
| 32 | + | log.Printf("Error: %s", err) | |||
| 33 | + | } | |||
| 34 | + | ||||
| 35 | + | // Setup a channel to capture termination signals | |||
| 36 | + | sigs := make(chan os.Signal, 1) | |||
| 37 | + | signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) | |||
| 38 | + | ||||
| 39 | + | go func() { | |||
| 40 | + | sig := <-sigs | |||
| 41 | + | log.Println("Received signal:", sig) | |||
| 42 | + | os.Exit(0) | |||
| 43 | + | }() | |||
| 44 | + | ||||
| 45 | + | e := echo.New() | |||
| 46 | + | e.HideBanner = true | |||
| 47 | + | e.HidePort = true | |||
| 48 | + | e = httpserve.RegisterRoutes(e, a) | |||
| 49 | + | ||||
| 50 | + | log.Println("Starting server on port", a.Config.Http.Port) | |||
| 51 | + | if err := e.Start(fmt.Sprintf(":%d", a.Config.Http.Port)); err != nil { | |||
| 52 | + | log.Fatal("Server error:", err) | |||
| 53 | + | } | |||
| 54 | + | ||||
| 8 | return nil | 55 | return nil | ||
| 9 | } | 56 | } | ||
M internal/server/newserver.go +2 -2
| @@ -10,7 +10,7 @@ import ( | |||
| 10 | 10 | "github.com/bnema/gordon/internal/webui" | |
| 11 | 11 | ) | |
| 12 | 12 | ||
| 13 | - | func NewServerApp() *App { | |
| 13 | + | func NewServerApp() (*App, error) { | |
| 14 | 14 | // Initialize AppConfig | |
| 15 | 15 | config := &Config{} | |
| 16 | 16 | _, err := LoadConfig(config) | |
| @@ -47,5 +47,5 @@ func NewServerApp() *App { | |||
| 47 | 47 | } | |
| 48 | 48 | ||
| 49 | 49 | OauthCallbackURL = config.GenerateOauthCallbackURL() | |
| 50 | - | return a | |
| 50 | + | return a, nil | |
| 51 | 51 | } | |
| @@ -10,7 +10,7 @@ import ( | |||||
| 10 | "github.com/bnema/gordon/internal/webui" | 10 | "github.com/bnema/gordon/internal/webui" | ||
| 11 | ) | 11 | ) | ||
| 12 | 12 | ||||
| 13 | - | func NewServerApp() *App { | 13 | + | func NewServerApp() (*App, error) { |
| 14 | // Initialize AppConfig | 14 | // Initialize AppConfig | ||
| 15 | config := &Config{} | 15 | config := &Config{} | ||
| 16 | _, err := LoadConfig(config) | 16 | _, err := LoadConfig(config) | ||
| @@ -47,5 +47,5 @@ func NewServerApp() *App { | |||||
| 47 | } | 47 | } | ||
| 48 | 48 | ||||
| 49 | OauthCallbackURL = config.GenerateOauthCallbackURL() | 49 | OauthCallbackURL = config.GenerateOauthCallbackURL() | ||
| 50 | - | return a | 50 | + | return a, nil |
| 51 | } | 51 | } | ||
M internal/server/sqliteclose.go +6 -0
| @@ -11,5 +11,11 @@ func CloseDB(a *App) error { | |||
| 11 | 11 | ||
| 12 | 12 | func saveDB(a *App) error { | |
| 13 | 13 | // TODO: Backup the database file if there is any modification. | |
| 14 | + | backupDBFile(a) | |
| 15 | + | return nil | |
| 16 | + | } | |
| 17 | + | ||
| 18 | + | // TODO | |
| 19 | + | func backupDBFile(a *App) error { | |
| 14 | 20 | return nil | |
| 15 | 21 | } | |
| @@ -11,5 +11,11 @@ func CloseDB(a *App) error { | |||||
| 11 | 11 | ||||
| 12 | func saveDB(a *App) error { | 12 | func saveDB(a *App) error { | ||
| 13 | // TODO: Backup the database file if there is any modification. | 13 | // TODO: Backup the database file if there is any modification. | ||
| 14 | + | backupDBFile(a) | |||
| 15 | + | return nil | |||
| 16 | + | } | |||
| 17 | + | ||||
| 18 | + | // TODO | |||
| 19 | + | func backupDBFile(a *App) error { | |||
| 14 | return nil | 20 | return nil | ||
| 15 | } | 21 | } | ||