2018-07-16 23:25:50 +03:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-07-19 15:15:50 +03:00
|
|
|
"github.com/MichaelMure/git-bug/graphql"
|
2018-07-16 23:25:50 +03:00
|
|
|
"github.com/MichaelMure/git-bug/webui"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/phayes/freeport"
|
|
|
|
"github.com/skratchdot/open-golang/open"
|
2018-07-19 13:30:25 +03:00
|
|
|
"github.com/spf13/cobra"
|
2018-07-16 23:25:50 +03:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2018-07-19 13:30:25 +03:00
|
|
|
func runWebUI(cmd *cobra.Command, args []string) error {
|
2018-07-16 23:25:50 +03:00
|
|
|
port, err := freeport.GetFreePort()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d", port)
|
2018-07-19 15:15:50 +03:00
|
|
|
webUiAddr := fmt.Sprintf("http://%s", addr)
|
|
|
|
|
|
|
|
fmt.Printf("Web UI available at %s\n", webUiAddr)
|
|
|
|
|
2018-07-20 01:25:30 +03:00
|
|
|
graphqlHandler, err := graphql.NewHandler(repo)
|
2018-07-19 15:15:50 +03:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-07-16 23:25:50 +03:00
|
|
|
|
|
|
|
router := mux.NewRouter()
|
2018-07-19 15:15:50 +03:00
|
|
|
|
|
|
|
// Routes
|
|
|
|
router.Path("/graphql").Handler(graphqlHandler)
|
2018-07-16 23:25:50 +03:00
|
|
|
router.PathPrefix("/").Handler(http.FileServer(webui.WebUIAssets))
|
|
|
|
|
2018-07-19 15:15:50 +03:00
|
|
|
open.Run(webUiAddr)
|
2018-07-16 23:25:50 +03:00
|
|
|
|
|
|
|
log.Fatal(http.ListenAndServe(addr, router))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-19 13:30:25 +03:00
|
|
|
var webUICmd = &cobra.Command{
|
|
|
|
Use: "webui",
|
|
|
|
Short: "Launch the web UI",
|
|
|
|
RunE: runWebUI,
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(webUICmd)
|
2018-07-16 23:25:50 +03:00
|
|
|
}
|