forked from ignite/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
services/serve: split dev handler (ignite#90)
as a starting point to refactor development server.
- Loading branch information
Showing
2 changed files
with
60 additions
and
48 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
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 | ||
} |
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