Skip to content

Commit

Permalink
Add static route for web client
Browse files Browse the repository at this point in the history
  • Loading branch information
EnricoSchw committed May 29, 2024
1 parent 332c968 commit 6ed1ebf
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
7 changes: 3 additions & 4 deletions internal/media/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func NewRouter(
router.Use(cors)
// Auth
router.Use(logging.LoggingMiddleware)

router.HandleFunc("/authenticate", getAuthenticationHandler(accountService)).Methods("POST")
// Space and LiveStream Resource Endpoints
router.HandleFunc("/space/{space}/streams", auth.HttpMiddleware(securityConfig, getStreamList(streamService))).Methods("GET")
Expand All @@ -53,10 +54,8 @@ func NewRouter(
router.HandleFunc("/fed/space/{space}/stream/{id}/whep", auth.HttpMiddleware(securityConfig, fedWhep(streamService, liveLobbyService))).Methods("POST")
router.HandleFunc("/fed/space/{space}/stream/{id}/whip", auth.HttpMiddleware(securityConfig, fedWhip(streamService, liveLobbyService))).Methods("POST")
router.HandleFunc("/fed/space/{space}/stream/{id}/res", auth.HttpMiddleware(securityConfig, fedResource(streamService, liveLobbyService))).Methods("DELETE")

// router.HandleFunc("/space/{space}/stream/{id}/static/whep", auth.HttpMiddleware(securityConfig, whepStaticAnswer(streamService, liveLobbyService))).Methods("POST")
fs := http.FileServer(http.Dir("./web/"))
router.PathPrefix("/").Handler(http.StripPrefix("/", fs))
router.NotFoundHandler = indexHTMLWhenNotFound(http.Dir("./web")) // Fallthrough for HTML5 routing
http.Handle("/", router)
return router
}

Expand Down
21 changes: 21 additions & 0 deletions internal/media/static_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package media

import (
"errors"
"net/http"
"os"
"path"
)

func indexHTMLWhenNotFound(fs http.FileSystem) http.Handler {
fileServer := http.FileServer(fs)

return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
_, err := fs.Open(path.Clean(req.URL.Path)) // Do not allow path traversals.
if errors.Is(err, os.ErrNotExist) {
http.ServeFile(resp, req, "./web/index.html")
return
}
fileServer.ServeHTTP(resp, req)
})
}
2 changes: 1 addition & 1 deletion internal/stream/live_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
type LiveStream struct {
VideoId string `json:"-" gorm:"not null;"`
Video *models.Video `json:"-" gorm:"foreignKey:VideoId;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
UUID uuid.UUID `json:"uuid"`
UUID uuid.UUID `json:"uuid" gorm:"not null;index;unique;"`
Title string `json:"title" gorm:"-"`
LobbyId uint `json:"-" gorm:"not null;"`
Lobby *lobby.LobbyEntity `json:"-" gorm:"foreignKey:LobbyId;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
Expand Down

0 comments on commit 6ed1ebf

Please sign in to comment.