Skip to content

Commit

Permalink
FIX: merge
Browse files Browse the repository at this point in the history
  • Loading branch information
silvareal committed Nov 24, 2022
2 parents c03f12c + f20417a commit 0a49dc7
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 121 deletions.
2 changes: 2 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ builds:
- linux
- windows
- darwin
flags:
- -tags=prod
archives:
- replacements:
darwin: Darwin
Expand Down
11 changes: 7 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@ endif
build-frontend:
cd web && export BUILD_PATH=../cmd/build && CI=false yarn build && cd ..

.PHONY: serve-backend
serve-backend:
air --build.cmd "go build -o ./tmp/$(bin) ." --build.bin "$(build_bin)" --build.exclude_dir "assets,docs,tmp,web,scripts,ssh-key,.github,.git" --build.include_ext "go,yaml,html,js" --build.exclude_file "config.example.yaml" --build.args_bin "dashboard,--config,config-test.yaml" --build.stop_on_error true --misc.clean_on_exit true --log.time true
.PHONY: dev-backend
dev-backend:
air --build.cmd "go build -o ./tmp/$(bin) ." --build.bin "$(build_bin)" --build.exclude_dir "assets,docs,tmp,web,scripts,ssh-key,.github,.git" --build.include_ext "go,yaml,html,js" --build.exclude_file "config.example.yaml" --build.args_bin "--config,config-test.yaml,--verbose" --build.stop_on_error true --misc.clean_on_exit true --log.time true

app: build-frontend serve-backend
prod-monolith:
go build -tags prod -o ./tmp/$(bin) . && $(build_bin) --config config-test.yaml -b

app: build-frontend prod-monolith
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ For personal usage, install latest from [Github Releases](https://github.com/bis

```bash
# binary is downloaded and named as saido
saido dashboard --config config.yaml --port 3000
saido --config config.yaml --port 3000 --verbose
```


Expand Down
104 changes: 0 additions & 104 deletions cmd/api.go

This file was deleted.

53 changes: 53 additions & 0 deletions cmd/frontend.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//go:build prod
// +build prod

package cmd

import (
"embed"
"fmt"
"io/fs"
"net/http"
"os"
"path"

log "github.com/sirupsen/logrus"
)

var (

//go:embed build/*
build embed.FS
)

type fsFunc func(name string) (fs.File, error)

func (f fsFunc) Open(name string) (fs.File, error) {
return f(name)
}

func EmbedHandler(prefix, root string) http.Handler {
handler := fsFunc(func(name string) (fs.File, error) {
defaultPath := fmt.Sprintf("%s/index.html", root)
assetPath := path.Join(root, name)
// If we can't find the asset, return the default index.html
// build
f, err := build.Open(assetPath)
log.Info(assetPath, err)
if os.IsNotExist(err) {
return build.Open(defaultPath)
}

// Otherwise assume this is a legitimate request routed
// correctly
return f, err
})

return http.StripPrefix(prefix, http.FileServer(http.FS(handler)))
}

func init() {
frontendHandler := EmbedHandler("/", "build")
server.Handle("/", frontendHandler)
server.Handle("/*filepath", frontendHandler)
}
56 changes: 44 additions & 12 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,31 @@ package cmd

import (
"fmt"
"net/http"
"os"
"strconv"

"github.com/bisohns/saido/client"
"github.com/bisohns/saido/config"
"github.com/gorilla/handlers"
"github.com/pkg/browser"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/bisohns/saido/config"
)

const appName = "saido"

var (
port string
server = http.NewServeMux()
// Verbose : Should display verbose logs
verbose bool
dashboardInfo *config.DashboardInfo
)
verbose bool
// open browser
browserFlag bool

const appName = "saido"
cfgFile string
cfg *config.Config
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Expand All @@ -40,17 +50,35 @@ var rootCmd = &cobra.Command{
Long: ``,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
// Log only errors except in Verbose mode
if verbose {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
fmt.Println("\n\nSaido - Bisohns (2020) (https://github.com/bisohns/saido)")
},
Run: func(cmd *cobra.Command, args []string) {
log.Info("Run dashboard with the [dashboard] subcommand. Feel free to drop a star at https://github.com/bisohns/saido")
if verbose {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
cfg = config.LoadConfig(cfgFile)
hosts := client.NewHostsController(cfg)

server.Handle("/metrics", hosts)
log.Info("listening on :", port)
_, err := strconv.Atoi(port)
if err != nil {
log.Fatal(err)
}
go hosts.Run()
loggedRouters := handlers.LoggingHandler(os.Stdout, server)
// Trigger browser open
url := fmt.Sprintf("http://localhost:%s", port)
if browserFlag {
browser.OpenURL(url)
}
if err := http.ListenAndServe(":"+port, loggedRouters); err != nil {
log.Fatal(err)
}
},
}

Expand All @@ -64,5 +92,9 @@ func Execute() {
}

func init() {
rootCmd.Flags().StringVarP(&port, "port", "p", "3000", "Port to run application server on")
rootCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "Run saido in verbose mode")
rootCmd.Flags().BoolVarP(&browserFlag, "open-browser", "b", false, "Prompt open browser")
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "Path to config file")
cobra.MarkFlagRequired(rootCmd.PersistentFlags(), "config")
}

0 comments on commit 0a49dc7

Please sign in to comment.