Refactor API for plugins.
parent
0a5818132c
commit
cefdc1b3f9
|
@ -10,6 +10,12 @@ type ChannelManager struct {
|
||||||
channelsLock sync.RWMutex
|
channelsLock sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewChannelManager() *ChannelManager {
|
||||||
|
return &ChannelManager{
|
||||||
|
channels: map[string]*Channel{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (manager *ChannelManager) Channel(uuid string) *Channel {
|
func (manager *ChannelManager) Channel(uuid string) *Channel {
|
||||||
manager.channelsLock.RLock()
|
manager.channelsLock.RLock()
|
||||||
defer manager.channelsLock.RUnlock()
|
defer manager.channelsLock.RUnlock()
|
||||||
|
|
|
@ -1,11 +1,43 @@
|
||||||
package app
|
package app
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
"git.icedream.tech/icedream/uplink/app/channels"
|
"git.icedream.tech/icedream/uplink/app/channels"
|
||||||
"github.com/gin-gonic/gin"
|
"git.icedream.tech/icedream/uplink/app/servers/http"
|
||||||
|
"git.icedream.tech/icedream/uplink/plugins"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Server struct {
|
type App struct {
|
||||||
*gin.Engine
|
Server *httpserver.Server
|
||||||
*channels.ChannelManager
|
ChannelManager *channels.ChannelManager
|
||||||
|
|
||||||
|
plugins []plugins.PluginInstance
|
||||||
|
}
|
||||||
|
|
||||||
|
func New() *App {
|
||||||
|
return &App{
|
||||||
|
Server: httpserver.NewServer(),
|
||||||
|
ChannelManager: channels.NewChannelManager(),
|
||||||
|
|
||||||
|
plugins: []plugins.PluginInstance{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (app *App) UsePlugin(plugin *plugins.Plugin) {
|
||||||
|
instance := plugin.Run()
|
||||||
|
app.plugins = append(app.plugins, instance)
|
||||||
|
log.Println("Plugin loaded:", plugin.Descriptor.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (app *App) Init() {
|
||||||
|
for _, plugin := range app.plugins {
|
||||||
|
if p, ok := plugin.(plugins.ServerPlugin); ok {
|
||||||
|
p.SetServer(app.Server)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (app *App) Run() error {
|
||||||
|
return app.Server.Run()
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,60 +1,28 @@
|
||||||
package httpserver
|
package httpserver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
_ "git.icedream.tech/icedream/uplink/app"
|
|
||||||
"git.icedream.tech/icedream/uplink/app/authentication"
|
|
||||||
channels "git.icedream.tech/icedream/uplink/app/channels"
|
|
||||||
_ "git.icedream.tech/icedream/uplink/app/transcoders"
|
|
||||||
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
Authenticator authentication.Authenticator
|
Http *http.Server
|
||||||
ChannelManager *channels.ChannelManager
|
Router *gin.Engine
|
||||||
}
|
}
|
||||||
|
|
||||||
func (server *Server) Run() {
|
func NewServer() *Server {
|
||||||
httpServer := new(http.Server)
|
server := &Server{
|
||||||
|
Http: new(http.Server),
|
||||||
router := gin.New()
|
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
})
|
server.Http.Handler = server.Router
|
||||||
|
server.Http.Addr = ":8000"
|
||||||
|
|
||||||
router.GET("/:channel", func(ctx *gin.Context) {
|
return server
|
||||||
channel := server.ChannelManager.Channel(ctx.Param("channel"))
|
}
|
||||||
if channel == nil {
|
|
||||||
ctx.Status(404)
|
func (server *Server) Run() error {
|
||||||
return
|
return server.Http.ListenAndServe()
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
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()
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,10 @@
|
||||||
package plugins
|
package plugins
|
||||||
|
|
||||||
|
type Plugin struct {
|
||||||
|
Descriptor PluginDescriptor
|
||||||
|
Run PluginRunner
|
||||||
|
}
|
||||||
|
|
||||||
type PluginDescriptor struct {
|
type PluginDescriptor struct {
|
||||||
Name string
|
Name string
|
||||||
Version string
|
Version string
|
||||||
|
|
|
@ -1,11 +1,50 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "git.icedream.tech/icedream/uplink/app"
|
import (
|
||||||
|
"git.icedream.tech/icedream/uplink/app"
|
||||||
|
"git.icedream.tech/icedream/uplink/app/servers/http"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
type pluginInstance struct {
|
type pluginInstance struct {
|
||||||
server *app.Server
|
server *app.Server
|
||||||
}
|
}
|
||||||
|
|
||||||
func (instance *pluginInstance) SetServer(server *app.Server) {
|
func (instance *pluginInstance) SetServer(server *httpserver.Server) {
|
||||||
instance.server = server
|
instance.server = server
|
||||||
|
|
||||||
|
router := instance.server.Router
|
||||||
|
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
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,18 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
|
import "C"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.icedream.tech/icedream/uplink/plugins"
|
"git.icedream.tech/icedream/uplink/plugins"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Descriptor = plugins.PluginDescriptor{
|
var Plugin = &plugins.Plugin{
|
||||||
|
Descriptor: plugins.PluginDescriptor{
|
||||||
Name: "Icecast Input",
|
Name: "Icecast Input",
|
||||||
Description: "Allows for Icecast clients to stream to the server.",
|
Description: "Allows for Icecast clients to stream to the server.",
|
||||||
}
|
},
|
||||||
|
|
||||||
func Run() *pluginInstance {
|
Run: func() plugins.PluginInstance {
|
||||||
return &pluginInstance{}
|
return &pluginInstance{}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
package plugins
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.icedream.tech/icedream/uplink/app/servers/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type PluginRunner func() PluginInstance
|
||||||
|
|
||||||
|
type PluginInstance interface {
|
||||||
|
}
|
||||||
|
|
||||||
|
type ServerPlugin interface {
|
||||||
|
PluginInstance
|
||||||
|
SetServer(*httpserver.Server)
|
||||||
|
}
|
||||||
|
|
||||||
|
type ChannelPlugin interface {
|
||||||
|
PluginInstance
|
||||||
|
SetChannel(id string)
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
package plugins
|
Loading…
Reference in New Issue