Refactor API for plugins.

burst
Icedream 2018-04-10 17:08:40 +02:00
parent 0a5818132c
commit cefdc1b3f9
Signed by: icedream
GPG Key ID: C1D30A06E6490C14
8 changed files with 133 additions and 58 deletions

View File

@ -10,6 +10,12 @@ type ChannelManager struct {
channelsLock sync.RWMutex
}
func NewChannelManager() *ChannelManager {
return &ChannelManager{
channels: map[string]*Channel{},
}
}
func (manager *ChannelManager) Channel(uuid string) *Channel {
manager.channelsLock.RLock()
defer manager.channelsLock.RUnlock()

View File

@ -1,11 +1,43 @@
package app
import (
"log"
"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 {
*gin.Engine
*channels.ChannelManager
type App struct {
Server *httpserver.Server
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()
}

View File

@ -1,60 +1,28 @@
package httpserver
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"
"github.com/gin-gonic/gin"
)
type Server struct {
Authenticator authentication.Authenticator
ChannelManager *channels.ChannelManager
Http *http.Server
Router *gin.Engine
}
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
func NewServer() *Server {
server := &Server{
Http: new(http.Server),
Router: gin.New(),
}
})
server.Http.Handler = server.Router
server.Http.Addr = ":8000"
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()
return server
}
func (server *Server) Run() error {
return server.Http.ListenAndServe()
}

View File

@ -1,5 +1,10 @@
package plugins
type Plugin struct {
Descriptor PluginDescriptor
Run PluginRunner
}
type PluginDescriptor struct {
Name string
Version string

View File

@ -1,11 +1,50 @@
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 {
server *app.Server
}
func (instance *pluginInstance) SetServer(server *app.Server) {
func (instance *pluginInstance) SetServer(server *httpserver.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
}
})
}

View File

@ -1,14 +1,18 @@
package main
import "C"
import (
"git.icedream.tech/icedream/uplink/plugins"
)
var Descriptor = plugins.PluginDescriptor{
var Plugin = &plugins.Plugin{
Descriptor: plugins.PluginDescriptor{
Name: "Icecast Input",
Description: "Allows for Icecast clients to stream to the server.",
}
},
func Run() *pluginInstance {
Run: func() plugins.PluginInstance {
return &pluginInstance{}
},
}

20
plugins/interfaces.go Normal file
View File

@ -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)
}

1
plugins/registration.go Normal file
View File

@ -0,0 +1 @@
package plugins