Skip to content

Commit

Permalink
services/serve: split dev handler (ignite#90)
Browse files Browse the repository at this point in the history
as a starting point to refactor development server.
  • Loading branch information
ilgooz authored Jul 30, 2020
1 parent 60315a9 commit 2be6a49
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 48 deletions.
57 changes: 57 additions & 0 deletions starport/services/serve/dev.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package starportserve

import (
"encoding/json"
"net/http"

"github.com/gobuffalo/packr/v2"
"github.com/gorilla/mux"
)

// newDevHandler creates a new development server handler.
func newDevHandler(app App) http.Handler {
router := mux.NewRouter()
devUI := packr.New("ui/dist", "../../ui/dist")
router.HandleFunc("/env", func(w http.ResponseWriter, r *http.Request) {
env := Env{app.Name, isCommandAvailable("node")}
js, err := json.Marshal(env)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
})
router.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) {
res, err := http.Get("http://localhost:1317/node_info")
if err != nil || res.StatusCode != 200 {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("500 error"))
} else if res.StatusCode == 200 {
w.WriteHeader(http.StatusOK)
w.Write([]byte("200 ok"))
}
})
router.HandleFunc("/rpc", func(w http.ResponseWriter, r *http.Request) {
res, err := http.Get("http://localhost:26657")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
} else if res.StatusCode == 200 {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusInternalServerError)
}
})
router.HandleFunc("/frontend", func(w http.ResponseWriter, r *http.Request) {
res, err := http.Get("http://localhost:8080")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
} else if res.StatusCode == 200 {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusInternalServerError)
}
})
router.PathPrefix("/").Handler(http.FileServer(devUI))
return router
}
51 changes: 3 additions & 48 deletions starport/services/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"path/filepath"
"time"

"github.com/gobuffalo/packr/v2"
"github.com/gorilla/mux"
"github.com/pkg/errors"
"github.com/radovskyb/watcher"
"github.com/tendermint/starport/starport/pkg/cmdrunner"
Expand All @@ -32,7 +30,7 @@ func Serve(ctx context.Context, app App, verbose bool) error {
cmdNpm.Start()
serveCtx, cancel := context.WithCancel(ctx)
startServe(serveCtx, app, verbose) // TODO handle error
go runDevServer(app.Name, verbose)
go runDevServer(app, verbose)
w := watcher.New()
w.SetMaxEvents(1)
go func() {
Expand Down Expand Up @@ -187,56 +185,13 @@ func startServe(ctx context.Context, app App, verbose bool) error {
return nil
}

func runDevServer(appName string, verbose bool) error {
func runDevServer(app App, verbose bool) error {
if verbose {
fmt.Printf("🔧 Running dev interface at http://localhost:12345\n\n")
} else {
fmt.Printf("\n🚀 Get started: http://localhost:12345/\n\n")
}
router := mux.NewRouter()
devUI := packr.New("ui/dist", "../../ui/dist")
router.HandleFunc("/env", func(w http.ResponseWriter, r *http.Request) {
env := Env{appName, isCommandAvailable("node")}
js, err := json.Marshal(env)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
})
router.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) {
res, err := http.Get("http://localhost:1317/node_info")
if err != nil || res.StatusCode != 200 {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("500 error"))
} else if res.StatusCode == 200 {
w.WriteHeader(http.StatusOK)
w.Write([]byte("200 ok"))
}
})
router.HandleFunc("/rpc", func(w http.ResponseWriter, r *http.Request) {
res, err := http.Get("http://localhost:26657")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
} else if res.StatusCode == 200 {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusInternalServerError)
}
})
router.HandleFunc("/frontend", func(w http.ResponseWriter, r *http.Request) {
res, err := http.Get("http://localhost:8080")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
} else if res.StatusCode == 200 {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusInternalServerError)
}
})
router.PathPrefix("/").Handler(http.FileServer(devUI))
return http.ListenAndServe(":12345", router)
return http.ListenAndServe(":12345", newDevHandler(app))
}

func isCommandAvailable(name string) bool {
Expand Down

0 comments on commit 2be6a49

Please sign in to comment.