-
-
Notifications
You must be signed in to change notification settings - Fork 163
/
routing.go
44 lines (38 loc) · 1.07 KB
/
routing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package core
import (
"net/http"
"strings"
"time"
"github.com/Roverr/rtsp-stream/core/config"
"github.com/julienschmidt/httprouter"
)
// determinesHost is for parsing out the host from the storage path
func determineHost(path string) string {
parts := strings.Split(path, "/")
if len(parts) >= 1 {
return parts[1]
}
return ""
}
// GetRouter returns the return for the application
func GetRouter(config *config.Specification) (*httprouter.Router, *Controller) {
fileServer := http.FileServer(http.Dir(config.StoreDir))
router := httprouter.New()
controllers := NewController(config, fileServer)
if config.ListEndpoint {
router.GET("/list", controllers.ListStreamHandler)
}
router.GET("/", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
w.WriteHeader(http.StatusOK)
})
router.POST("/start", controllers.StartStreamHandler)
router.GET("/stream/*filepath", controllers.FileHandler)
// Start cleaning process in the background
go func() {
for {
<-time.After(config.CleanupTime)
controllers.cleanUnused()
}
}()
return router, controllers
}