forked from Roverr/rtsp-stream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
66 lines (61 loc) · 1.85 KB
/
main.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"github.com/julienschmidt/httprouter"
"github.com/rs/cors"
"github.com/Roverr/rtsp-stream/core"
"github.com/Roverr/rtsp-stream/core/config"
"github.com/sirupsen/logrus"
)
func main() {
config := config.InitConfig()
core.SetupLogger(config)
fileServer := http.FileServer(http.Dir(config.StoreDir))
router := httprouter.New()
controllers := core.NewController(config, fileServer)
router.GET("/", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
w.WriteHeader(http.StatusOK)
})
if config.EndpointYML.Endpoints.List.Enabled {
router.GET("/list", controllers.ListStreamHandler)
logrus.Infoln("list endpoint enabled | MainProcess")
}
if config.EndpointYML.Endpoints.Start.Enabled {
router.POST("/start", controllers.StartStreamHandler)
logrus.Infoln("start endpoint enabled | MainProcess")
}
if config.EndpointYML.Endpoints.Static.Enabled {
router.GET("/stream/*filepath", controllers.StaticFileHandler)
logrus.Infoln("static endpoint enabled | MainProcess")
}
if config.EndpointYML.Endpoints.Stop.Enabled {
router.POST("/stop", controllers.StopStreamHandler)
logrus.Infoln("stop endpoint enabled | MainProcess")
}
done := controllers.ExitPreHook()
handler := cors.AllowAll().Handler(router)
if config.CORS.Enabled {
handler = cors.New(cors.Options{
AllowCredentials: config.CORS.AllowCredentials,
AllowedOrigins: config.CORS.AllowedOrigins,
MaxAge: config.CORS.MaxAge,
}).Handler(router)
}
srv := &http.Server{
Addr: fmt.Sprintf(":%d", config.Port),
Handler: handler,
}
go func() {
logrus.Infof("rtsp-stream transcoder started on %d | MainProcess", config.Port)
log.Fatal(srv.ListenAndServe())
}()
<-done
if err := srv.Shutdown(context.Background()); err != nil {
logrus.Errorf("HTTP server Shutdown: %v", err)
}
os.Exit(0)
}