-
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow hosting OpenBooks server at a subpath. (#34)
* feature: Add support for hosting at sub path. - The subpath is set via environment variable or cli argument. - Updated readme files to show it's used. * fix: minor wording change
- Loading branch information
Showing
12 changed files
with
111 additions
and
56 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 |
---|---|---|
@@ -1,4 +1,7 @@ | ||
**/node_modules/ | ||
**/build/ | ||
**/dist/ | ||
**/github/ | ||
openbooks.exe | ||
openbooks | ||
*.exe |
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
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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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,47 @@ | ||
package server | ||
|
||
import ( | ||
"embed" | ||
"fmt" | ||
"io/fs" | ||
"log" | ||
"net/http" | ||
"path" | ||
) | ||
|
||
//go:embed app/dist | ||
var reactClient embed.FS | ||
|
||
func registerRoutes(hub *Hub) { | ||
basePath := getBaseRoute() | ||
fmt.Printf("Base Path: %s\n", basePath) | ||
|
||
http.Handle(basePath, serveStaticFiles(basePath, "app/dist")) | ||
|
||
http.HandleFunc(path.Join(basePath, "ws"), func(w http.ResponseWriter, r *http.Request) { | ||
serveWs(hub, w, r) | ||
}) | ||
|
||
http.HandleFunc(path.Join(basePath, "connections"), func(w http.ResponseWriter, r *http.Request) { | ||
fmt.Fprintf(w, "There are currently %d active connections.", *numConnections) | ||
}) | ||
} | ||
|
||
func serveStaticFiles(basePath, assetPath string) http.Handler { | ||
// update the embedded file system's tree so that index.html is at the root | ||
app, err := fs.Sub(reactClient, assetPath) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// strip the predefined base path and serve the static file | ||
return http.StripPrefix(basePath, http.FileServer(http.FS(app))) | ||
} | ||
|
||
func getBaseRoute() string { | ||
cleaned := path.Clean(config.Basepath) | ||
if cleaned == "/" { | ||
return cleaned | ||
} | ||
return cleaned + "/" | ||
} |
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
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