Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

services/serve: split dev handler #90

Merged
merged 1 commit into from
Jul 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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