Skip to content

Commit

Permalink
feat(server): prettiest watcher and best port flag binding
Browse files Browse the repository at this point in the history
  • Loading branch information
luisnquin committed Jul 1, 2022
1 parent f5cdb24 commit f795008
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,16 @@ func (a *Server) listenAndRefreshData() {
w := fswatch.NewFileWatcher(config.App.Paths.DataFile, 3)
w.Start()

color.New(color.FgHiCyan).Fprintln(os.Stdout, "馃憖 Watching "+config.App.Paths.DataFile+"\n")

var timesMod int

for w.IsRunning() {
select {
case <-w.Modified():
timesMod++
a.box.ModifyBox(data.JustLoadBox())
color.New(color.FgHiBlue).Fprintln(os.Stdout, "Data refreshed")
color.New(color.FgHiBlue).Fprintf(os.Stdout, "\rData refreshed(x%d)", timesMod)

case <-w.Moved():
color.New(color.FgHiRed).Fprintln(os.Stderr, "Error: Unable to find data file, apparently moved")
Expand All @@ -55,6 +60,7 @@ func (a *Server) mountHandlers() {
}

func (a *Server) JSONResponse(w http.ResponseWriter, statusCode int, v any) {

w.WriteHeader(statusCode)
w.Header().Set("Content-Type", "application/json")

Expand Down
8 changes: 6 additions & 2 deletions src/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@ func buildServer() serverComp {

c.cmd.RunE = c.Main()

c.cmd.Flags().StringVarP(&c.port, "port", "p", ":3000", "Port to listen (e.g.: \":XXXX\")")
c.cmd.Flags().StringVarP(&c.port, "port", "p", ":3000", "Port to listen (e.g.: \"XXXX\")")

return c
}

func (s *serverComp) Main() scriptor {
return func(cmd *cobra.Command, args []string) error {
color.New(color.FgHiGreen).Fprintln(os.Stdout, "Listen on http://localhost"+s.port+"\n")
if len(s.port) > 0 && string([]rune(s.port)[0]) != ":" {
s.port = ":" + s.port
}

color.New(color.FgHiGreen).Fprintln(os.Stdout, "Listening on http://localhost"+s.port+"\n")

return api.New().Start(s.port)
}
Expand Down
2 changes: 1 addition & 1 deletion src/constants/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package constants

const (
AppName string = "nao"
Version string = "v0.3.2"
Version string = "v0.4.0"
)

const (
Expand Down

0 comments on commit f795008

Please sign in to comment.