detached 32009ff

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
32009ff0b8e9ace2598fec006ac199c635fded8f
author
bnema · · 2023-10-24 20:06 UTC
committer
bnema · · 2023-10-24 17:15 UTC
parent
f2763b0
signature
unsigned
9 files · +87 -11
M cmd/cli/main.go +11 -5
@@ -5,18 +5,19 @@ import (
55
66 "github.com/bnema/gordon/internal/cli"
77 "github.com/bnema/gordon/internal/cli/cmd"
8+ "github.com/bnema/gordon/internal/server"
89 "github.com/spf13/cobra"
910 )
1011
1112 var rootCmd = &cobra.Command{Use: "gordon"}
1213
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))
1516 rootCmd.AddCommand(cmd.NewPingCommand(a))
1617 }
1718
18-func Execute(a *cli.App) {
19- InitializeCommands(a)
19+func Execute(a *cli.App, s *server.App) {
20+ InitializeCommands(a, s)
2021 cobra.CheckErr(rootCmd.Execute())
2122 }
2223
@@ -25,5 +26,10 @@ func main() {
2526 if err != nil {
2627 fmt.Println("Error initializing app:", err)
2728 }
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)
2935 }
@@ -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) {
2121 }
2222
2323 func main() {
24- a := server.NewServerApp()
24+ a, err := server.NewServerApp()
25+ if err != nil {
26+ log.Fatal("Failed to initialize app:", err)
27+ }
2528
26- _, err := server.InitializeDB(a)
29+ _, err = server.InitializeDB(a)
2730 if err != nil {
2831 log.Fatal("Failed to initialize database:", err)
2932 }
@@ -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 @@
11 package cmd
22
33 // 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 cmd1 package cmd
2 2
3 // the push command is used to push a container image to gordon's endpoint3 // 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 @@
11 package handler
22
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+)
415
516 // execute cmd/srv/main.go main function
617
718 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+
855 return nil
956 }
@@ -1,9 +1,56 @@
1 package handler1 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 function16 // 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 nil55 return nil
9 }56 }
M internal/server/newserver.go +2 -2
@@ -10,7 +10,7 @@ import (
1010 "github.com/bnema/gordon/internal/webui"
1111 )
1212
13-func NewServerApp() *App {
13+func NewServerApp() (*App, error) {
1414 // Initialize AppConfig
1515 config := &Config{}
1616 _, err := LoadConfig(config)
@@ -47,5 +47,5 @@ func NewServerApp() *App {
4747 }
4848
4949 OauthCallbackURL = config.GenerateOauthCallbackURL()
50- return a
50+ return a, nil
5151 }
@@ -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 AppConfig14 // 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 a50+ return a, nil
51 }51 }
M internal/server/sqliteclose.go +6 -0
@@ -11,5 +11,11 @@ func CloseDB(a *App) error {
1111
1212 func saveDB(a *App) error {
1313 // 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 {
1420 return nil
1521 }
@@ -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 nil20 return nil
15 }21 }

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 7

Tags 106

↑↓ moveenter openesc close