-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
include spinup UI in setup script, improve CLI with cobra
- Loading branch information
Showing
12 changed files
with
480 additions
and
121 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var rootCmd = &cobra.Command{ | ||
Use: "spinup", | ||
Short: "Spinup CLI", | ||
TraverseChildren: true, | ||
} | ||
|
||
func Execute(ctx context.Context, apiVersion string) error { | ||
rootCmd.AddCommand(versionCmd(apiVersion)) | ||
rootCmd.AddCommand(startCmd()) | ||
|
||
return rootCmd.ExecuteContext(ctx) | ||
} |
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,160 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"math/rand" | ||
"net" | ||
"net/http" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/spinup-host/api" | ||
"github.com/spinup-host/config" | ||
"github.com/spinup-host/metrics" | ||
|
||
"github.com/golang-jwt/jwt" | ||
"github.com/rs/cors" | ||
"github.com/spf13/cobra" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
var ( | ||
cfgFile string | ||
uiPath string | ||
|
||
apiPort = ":4434" | ||
uiPort = ":3000" | ||
) | ||
|
||
func apiHandler() http.Handler { | ||
rand.Seed(time.Now().UnixNano()) | ||
mux := http.NewServeMux() | ||
mux.HandleFunc("/hello", api.Hello) | ||
mux.HandleFunc("/createservice", api.CreateService) | ||
mux.HandleFunc("/githubAuth", api.GithubAuth) | ||
mux.HandleFunc("/logs", api.Logs) | ||
mux.HandleFunc("/jwt", api.JWT) | ||
mux.HandleFunc("/jwtdecode", api.JWTDecode) | ||
mux.HandleFunc("/streamlogs", api.StreamLogs) | ||
mux.HandleFunc("/listcluster", api.ListCluster) | ||
mux.HandleFunc("/cluster", api.GetCluster) | ||
mux.HandleFunc("/metrics", metrics.HandleMetrics) | ||
mux.HandleFunc("/createbackup", api.CreateBackup) | ||
c := cors.New(cors.Options{ | ||
AllowedOrigins: []string{"https://app.spinup.host", "http://localhost:3000"}, | ||
AllowedHeaders: []string{"authorization", "content-type"}, | ||
}) | ||
|
||
return c.Handler(mux) | ||
} | ||
|
||
func uiHandler() http.Handler { | ||
fs := http.FileServer(http.Dir(uiPath)) | ||
http.Handle("/", fs) | ||
|
||
return http.DefaultServeMux | ||
} | ||
|
||
func startCmd() *cobra.Command { | ||
sc := &cobra.Command{ | ||
Use: "start", | ||
Short: "start the spinup API and frontend servers", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
home, err := os.UserHomeDir() | ||
if err != nil { | ||
log.Fatalf("FATAL: obtaining home directory: %v", err) | ||
} | ||
cmd.PersistentFlags().StringVar(&cfgFile, "config", | ||
fmt.Sprintf("%s/.local/spinup/config.yaml", home), "Path to spinup configuration") | ||
cmd.PersistentFlags().StringVar(&uiPath, "ui-path", | ||
fmt.Sprintf("%s/.local/spinup/spinup-dash", home), "Path to spinup frontend") | ||
|
||
log.Println(fmt.Sprintf("INFO: using config file: %s", cfgFile)) | ||
if err = validateConfig(cfgFile); err != nil { | ||
log.Fatalf("FATAL: validating config: %v", err) | ||
} | ||
log.Println("INFO: initial validations successful") | ||
|
||
apiListener, err := net.Listen("tcp", apiPort) | ||
if err != nil { | ||
log.Fatalf("FATAL: starting API server %v", err) | ||
} | ||
uiListener, err := net.Listen("tcp", uiPort) | ||
if err != nil { | ||
log.Fatalf("FATAL: starting UI server %v", err) | ||
} | ||
apiServer := &http.Server{ | ||
Handler: apiHandler(), | ||
} | ||
uiServer := &http.Server{ | ||
Handler: uiHandler(), | ||
} | ||
|
||
stopCh := make(chan os.Signal, 1) | ||
go func() { | ||
log.Println(fmt.Sprintf("INFO: starting Spinup API on port %s", apiPort)) | ||
apiServer.Serve(apiListener) | ||
}() | ||
|
||
go func() { | ||
log.Println(fmt.Sprintf("INFO: starting Spinup UI on port %s", uiPort)) | ||
uiServer.Serve(uiListener) | ||
}() | ||
|
||
defer stop(apiServer) | ||
defer stop(uiServer) | ||
|
||
signal.Notify(stopCh, syscall.SIGINT, syscall.SIGTERM) | ||
log.Println(fmt.Sprint(<-stopCh)) | ||
log.Println("stopping spinup apiServer") | ||
}, | ||
} | ||
|
||
return sc | ||
} | ||
|
||
func validateConfig(path string) error { | ||
file, err := os.Open(path) | ||
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
|
||
d := yaml.NewDecoder(file) | ||
if err = d.Decode(&config.Cfg); err != nil { | ||
return err | ||
} | ||
|
||
signBytes, err := ioutil.ReadFile(config.Cfg.Common.ProjectDir + "/app.rsa") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if config.Cfg.SignKey, err = jwt.ParseRSAPrivateKeyFromPEM(signBytes); err != nil { | ||
return err | ||
} | ||
|
||
verifyBytes, err := ioutil.ReadFile(config.Cfg.Common.ProjectDir + "/app.rsa.pub") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if config.Cfg.VerifyKey, err = jwt.ParseRSAPublicKeyFromPEM(verifyBytes); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func stop(server *http.Server) { | ||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) //nolint | ||
defer cancel() | ||
if err := server.Shutdown(ctx); err != nil { | ||
log.Printf("Can't stop Spinup API correctly: %v", err) | ||
} | ||
} |
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,16 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func versionCmd(version string) *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "version", | ||
Short: "Print the SpinUp version", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
fmt.Println(fmt.Sprintf("spinup version: %s", version)) | ||
}, | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
internal/dockerservice.go → internal/dockerservice/dockerservice.go
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,4 +1,4 @@ | ||
package internal | ||
package dockerservice | ||
|
||
import ( | ||
"context" | ||
|
Oops, something went wrong.