61 lines
1.3 KiB
Go
61 lines
1.3 KiB
Go
|
package httpserver
|
||
|
|
||
|
import (
|
||
|
_ "git.icedream.tech/icedream/uplink/internal"
|
||
|
"git.icedream.tech/icedream/uplink/internal/authentication"
|
||
|
channels "git.icedream.tech/icedream/uplink/internal/channels"
|
||
|
_ "git.icedream.tech/icedream/uplink/internal/transcoders"
|
||
|
|
||
|
"net/http"
|
||
|
|
||
|
"github.com/gin-gonic/gin"
|
||
|
)
|
||
|
|
||
|
type Server struct {
|
||
|
Authenticator authentication.Authenticator
|
||
|
ChannelManager *channels.ChannelManager
|
||
|
}
|
||
|
|
||
|
func (server *Server) Run() {
|
||
|
httpServer := new(http.Server)
|
||
|
|
||
|
router := gin.New()
|
||
|
router.POST("/:channel", func(ctx *gin.Context) {
|
||
|
channel := server.ChannelManager.Channel(ctx.Param("channel"))
|
||
|
if channel == nil {
|
||
|
ctx.Status(404)
|
||
|
return
|
||
|
}
|
||
|
if user, password, ok := ctx.Request.BasicAuth(); ok {
|
||
|
if !server.Authenticator.VerifyUsernameAndPassword(channel, user, password) {
|
||
|
ctx.Status(401)
|
||
|
return
|
||
|
}
|
||
|
} else {
|
||
|
ctx.Status(401)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
})
|
||
|
|
||
|
router.GET("/:channel", func(ctx *gin.Context) {
|
||
|
channel := server.ChannelManager.Channel(ctx.Param("channel"))
|
||
|
if channel == nil {
|
||
|
ctx.Status(404)
|
||
|
return
|
||
|
}
|
||
|
})
|
||
|
|
||
|
router.GET("/:channel/:stream", func(ctx *gin.Context) {
|
||
|
channel := server.ChannelManager.Channel(ctx.Param("channel"))
|
||
|
if channel == nil {
|
||
|
ctx.Status(404)
|
||
|
return
|
||
|
}
|
||
|
})
|
||
|
|
||
|
httpServer.Handler = router
|
||
|
httpServer.Addr = ":8000"
|
||
|
httpServer.ListenAndServe()
|
||
|
}
|