Skip to content

Commit aee79a1

Browse files
committed
add logic to serve react bundle from the current executable directory
1 parent 1297e55 commit aee79a1

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ go 1.15
44

55
require (
66
github.com/99designs/gqlgen v0.13.0
7-
github.com/GeertJohan/go.rice v1.0.0 // indirect
87
github.com/TwinProduction/go-color v0.0.2
9-
github.com/daaku/go.zipexe v1.0.1 // indirect
108
github.com/go-git/go-git/v5 v5.2.0
119
github.com/google/uuid v1.1.2
1210
github.com/gorilla/mux v1.8.0

server.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"github.com/rs/cors"
1414
"log"
1515
"net/http"
16+
"os"
17+
"path/filepath"
1618
"strconv"
1719
)
1820

@@ -61,18 +63,32 @@ func main() {
6163
router.Handle("/query", srv)
6264
router.Handle("/gitconvexapi", srv)
6365

66+
execName, _ := os.Executable()
67+
var (
68+
staticPath string
69+
buildPath string
70+
)
71+
if execName != "" {
72+
currentDir := filepath.Dir(execName)
73+
buildPath = fmt.Sprintf("%s/build", currentDir)
74+
staticPath = fmt.Sprintf("%s/static", buildPath)
75+
}
76+
6477
// Static file supplier for hosting the react static assets and scripts
65-
router.PathPrefix("/static").Handler(http.StripPrefix("/static", http.FileServer(http.Dir("./build/static/"))))
78+
logger.Log(fmt.Sprintf("Serving static files from -> %s", staticPath), global.StatusInfo)
79+
router.PathPrefix("/static").Handler(http.StripPrefix("/static", http.FileServer(http.Dir(staticPath))))
6680

6781
// Route for serving the webpage logo from the reach build bundle
6882
router.PathPrefix("/gitconvex.png").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
69-
http.ServeFile(w, r, "./build/gitconvex.png")
83+
logger.Log(fmt.Sprintf("Serving logo from directory -> %s", buildPath), global.StatusInfo)
84+
http.ServeFile(w, r, buildPath+"/gitconvex.png")
7085
})
7186

7287
// A default fallback route for handling all routes with '/' prefix.
7388
// For making it compatible with react router
7489
router.PathPrefix("/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
75-
http.ServeFile(w, r, "./build/index.html")
90+
logger.Log(fmt.Sprintf("Serving UI from directory -> %s", buildPath), global.StatusInfo)
91+
http.ServeFile(w, r, buildPath+"/index.html")
7692
})
7793

7894
// Checking and Assigning port received from the command line ( --port args )

0 commit comments

Comments
 (0)