mirror of
https://github.com/hanzoai/core-networkmanagement.git
synced 2026-09-30 20:56:28 +00:00
seperated out boilerplate plugin code
This commit is contained in:
+1
-1
@@ -19,8 +19,8 @@ func initDB() {
|
||||
dbcreds := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable",
|
||||
dbUser, dbPassword, dbName)
|
||||
db, dberr := sql.Open("postgres", dbcreds)
|
||||
fmt.Println("DB status:", db.Ping())
|
||||
if dberr != nil {
|
||||
log.Fatal(dberr)
|
||||
}
|
||||
defer db.Close()
|
||||
}
|
||||
@@ -6,24 +6,13 @@ import (
|
||||
"strings"
|
||||
"core-networkmanager/frontend/plugins"
|
||||
)
|
||||
//Page struct to hold per-page data
|
||||
type page struct {
|
||||
Location string
|
||||
Name string
|
||||
}
|
||||
//Plugin implements plugin interface
|
||||
type Plugin struct {}
|
||||
|
||||
func init() {
|
||||
plugins.RegisterPlugin("assets", &Plugin{})
|
||||
}
|
||||
|
||||
//Activate sets up endpoints and such
|
||||
func (p Plugin ) Activate () {
|
||||
func setup() bool {
|
||||
fileserver := http.FileServer(FileSystem{http.Dir("./assets")})
|
||||
plugins.RegisterEndpoint("/assets/", http.StripPrefix("/assets", fileserver).ServeHTTP)
|
||||
return true
|
||||
}
|
||||
//FileSystem implements http.Filesystem, with Open() that doesnt allow Dirs
|
||||
//FileSystem implements http.Filesystem, with Open() that doesnt allow Dirs
|
||||
type FileSystem struct {
|
||||
fs http.FileSystem
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package assets
|
||||
import (
|
||||
"sync"
|
||||
"core-networkmanager/frontend/plugins"
|
||||
)
|
||||
|
||||
var (
|
||||
mux sync.Mutex
|
||||
active bool
|
||||
)
|
||||
|
||||
//init registers the plugin as available with Plugin manager
|
||||
func init() {
|
||||
plugins.RegisterPlugin("assets", &plugin{})
|
||||
}
|
||||
|
||||
//plugin struct that for the plugin to impliment required and optional interfaces as defined in Plugin package
|
||||
type plugin struct {}
|
||||
|
||||
//Activate sets up endpoints and databases, and any other initialization
|
||||
func (p plugin ) Activate () {
|
||||
if(!active) {
|
||||
mux.Lock()
|
||||
active = setup()
|
||||
mux.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
//Active returns true if the plugin has already been activated
|
||||
func (p plugin ) Active () bool {
|
||||
return active
|
||||
}
|
||||
@@ -4,8 +4,10 @@ import (
|
||||
"log"
|
||||
"sync"
|
||||
"net/http"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
//Global variables
|
||||
var (
|
||||
pluginMu sync.RWMutex
|
||||
plugins = make(map[string]Plugin)
|
||||
@@ -13,21 +15,13 @@ var (
|
||||
universalPlugins [] UniversalHandlerPlugin
|
||||
endpointMappings = make(map[string]func(http.ResponseWriter, *http.Request))
|
||||
mux *http.ServeMux
|
||||
db *sql.DB
|
||||
)
|
||||
|
||||
//Plugin is an interface defining all plugin exported functions
|
||||
type Plugin interface {
|
||||
Activate() //should register endpoints and add database tables etc
|
||||
}
|
||||
|
||||
//UniversalHandlerPlugin is an interface that extends the Plugin interface, for if a plugin needs to be called on all endpoints
|
||||
type UniversalHandlerPlugin interface {
|
||||
Plugin
|
||||
UniversalHandler(http.HandlerFunc) http.HandlerFunc
|
||||
}
|
||||
|
||||
//SetupPlugins activates plugins and loads endpoints
|
||||
func SetupPlugins(m *http.ServeMux, names[] string) {
|
||||
func SetupPlugins(m *http.ServeMux, d *sql.DB, names[] string) {
|
||||
mux = m
|
||||
db = d
|
||||
for _, n := range names {
|
||||
if _, exists := plugins[n]; !exists {
|
||||
log.Fatalf("Plugin %s does not exist", n)
|
||||
@@ -37,7 +31,6 @@ func SetupPlugins(m *http.ServeMux, names[] string) {
|
||||
universalPlugins = append(universalPlugins, p)
|
||||
}
|
||||
}
|
||||
mux = m
|
||||
for key, element := range endpointMappings {
|
||||
mux.HandleFunc(key, chainUniversalHandlers(element))
|
||||
}
|
||||
|
||||
@@ -8,22 +8,17 @@ import (
|
||||
"core-networkmanager/frontend/plugins"
|
||||
)
|
||||
|
||||
func setup() bool {
|
||||
plugins.RegisterEndpoint("/", homePage)
|
||||
return true
|
||||
}
|
||||
|
||||
//Page struct to hold per-page data
|
||||
type page struct {
|
||||
Location string
|
||||
Name string
|
||||
}
|
||||
|
||||
type Plugin struct {}
|
||||
|
||||
func init() {
|
||||
plugins.RegisterPlugin("root", &Plugin{})
|
||||
}
|
||||
|
||||
//Activate sets up endpoints and such
|
||||
func (p Plugin ) Activate () {
|
||||
plugins.RegisterEndpoint("/", homePage)
|
||||
}
|
||||
func homePage(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == "GET" {
|
||||
t, err := template.ParseFiles("index.gohtml")
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package root
|
||||
import (
|
||||
"sync"
|
||||
"core-networkmanager/frontend/plugins"
|
||||
)
|
||||
|
||||
var (
|
||||
mux sync.Mutex
|
||||
active bool
|
||||
)
|
||||
|
||||
//init registers the plugin as available with Plugin manager
|
||||
func init() {
|
||||
plugins.RegisterPlugin("root", &plugin{})
|
||||
}
|
||||
|
||||
//plugin struct that for the plugin to impliment required and optional interfaces as defined in Plugin package
|
||||
type plugin struct {}
|
||||
|
||||
//Activate sets up endpoints and databases, and any other initialization
|
||||
func (p plugin ) Activate () {
|
||||
if(!active) {
|
||||
mux.Lock()
|
||||
active = setup()
|
||||
mux.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
//Active returns true if the plugin has already been activated
|
||||
func (p plugin ) Active () bool {
|
||||
return active
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package plugins
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
//Plugin is an interface defining all plugin exported functions
|
||||
type Plugin interface {
|
||||
Activate() //should register endpoints and add database tables etc
|
||||
Active() bool //should check if plugin has already been activated
|
||||
}
|
||||
|
||||
//UniversalHandlerPlugin is an interface that extends the Plugin interface, for if a plugin needs to be called on all endpoints
|
||||
type UniversalHandlerPlugin interface {
|
||||
Plugin
|
||||
UniversalHandler(http.HandlerFunc) http.HandlerFunc
|
||||
}
|
||||
+3
-2
@@ -4,7 +4,6 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
_ "github.com/lib/pq"
|
||||
"core-networkmanager/frontend/plugins"
|
||||
_ "core-networkmanager/frontend/plugins/root"
|
||||
_ "core-networkmanager/frontend/plugins/assets"
|
||||
@@ -12,12 +11,14 @@ import (
|
||||
|
||||
func main() {
|
||||
initDB()
|
||||
defer db.Close()
|
||||
var port = 8080
|
||||
mux := http.NewServeMux()
|
||||
activePlugins := []string{"root", "assets"}
|
||||
plugins.SetupPlugins(mux, activePlugins)
|
||||
plugins.SetupPlugins(mux, nil, activePlugins)
|
||||
err := http.ListenAndServe(":" + strconv.Itoa(port), mux)
|
||||
if(err != nil) {
|
||||
log.Fatal("ListenAndServe failed with error: ", err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user