git-bug/commands/webui.go

60 lines
1.1 KiB
Go
Raw Normal View History

package commands
import (
"fmt"
2018-07-19 15:15:50 +03:00
"github.com/MichaelMure/git-bug/graphql"
"github.com/MichaelMure/git-bug/webui"
"github.com/gorilla/mux"
"github.com/phayes/freeport"
"github.com/skratchdot/open-golang/open"
"github.com/spf13/cobra"
"log"
"net/http"
)
var port int
func runWebUI(cmd *cobra.Command, args []string) error {
if port == 0 {
var err error
port, err = freeport.GetFreePort()
if err != nil {
return 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)
graphqlHandler, err := graphql.NewHandler(repo)
2018-07-19 15:15:50 +03:00
if err != nil {
return err
}
router := mux.NewRouter()
2018-07-19 15:15:50 +03:00
// Routes
router.Path("/graphql").Handler(graphqlHandler)
router.PathPrefix("/").Handler(http.FileServer(webui.WebUIAssets))
2018-07-19 15:15:50 +03:00
open.Run(webUiAddr)
log.Fatal(http.ListenAndServe(addr, router))
return nil
}
var webUICmd = &cobra.Command{
Use: "webui",
Short: "Launch the web UI",
RunE: runWebUI,
}
func init() {
2018-07-20 16:46:14 +03:00
RootCmd.AddCommand(webUICmd)
webUICmd.Flags().IntVarP(&port, "port", "p", 0, "Port to listen to")
}