add first moduls
This commit is contained in:
parent
0a2d43102a
commit
78ea10dc19
|
@ -29,6 +29,21 @@ func handlerstatic(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
website := getWebsite(host)
|
website := getWebsite(host)
|
||||||
|
|
||||||
|
path := fmt.Sprintf("%s/%d/%s/%s", config.Webroot, website.ID, "static", r.URL.Path)
|
||||||
|
if f, err := os.Stat(path); err == nil && !f.IsDir() {
|
||||||
|
http.ServeFile(w, r, path)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
http.NotFound(w, r)
|
||||||
|
}
|
||||||
|
func handlerfiles(w http.ResponseWriter, r *http.Request) {
|
||||||
|
host := r.Header.Get(ProxyHost)
|
||||||
|
if len(host) <= 1 {
|
||||||
|
host = r.Host
|
||||||
|
}
|
||||||
|
website := getWebsite(host)
|
||||||
|
|
||||||
path := fmt.Sprintf("%s/%d/%s/%s", config.Webroot, website.ID, "files", r.URL.Path)
|
path := fmt.Sprintf("%s/%d/%s/%s", config.Webroot, website.ID, "files", r.URL.Path)
|
||||||
if f, err := os.Stat(path); err == nil && !f.IsDir() {
|
if f, err := os.Stat(path); err == nil && !f.IsDir() {
|
||||||
http.ServeFile(w, r, path)
|
http.ServeFile(w, r, path)
|
||||||
|
@ -37,6 +52,7 @@ func handlerstatic(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
func handlerfunc(w http.ResponseWriter, r *http.Request) {
|
func handlerfunc(w http.ResponseWriter, r *http.Request) {
|
||||||
host := r.Header.Get(ProxyHost)
|
host := r.Header.Get(ProxyHost)
|
||||||
if len(host) <= 1 {
|
if len(host) <= 1 {
|
||||||
|
|
|
@ -44,6 +44,7 @@ func main() {
|
||||||
|
|
||||||
// http handler
|
// http handler
|
||||||
http.Handle("/static/", gziphandler.GzipHandler(http.StripPrefix("/static/", http.HandlerFunc(handlerstatic))))
|
http.Handle("/static/", gziphandler.GzipHandler(http.StripPrefix("/static/", http.HandlerFunc(handlerstatic))))
|
||||||
|
http.Handle("/files/", gziphandler.GzipHandler(http.StripPrefix("/files/", http.HandlerFunc(handlerfiles))))
|
||||||
http.Handle("/", gziphandler.GzipHandler(http.HandlerFunc(handlerfunc)))
|
http.Handle("/", gziphandler.GzipHandler(http.HandlerFunc(handlerfunc)))
|
||||||
|
|
||||||
// Start server
|
// Start server
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
// +build all
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
// MODUL variables
|
||||||
|
const (
|
||||||
|
MODULWEB = true
|
||||||
|
MODULHOST = true
|
||||||
|
)
|
|
@ -0,0 +1,9 @@
|
||||||
|
// +build !all
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
// MODUL variables
|
||||||
|
const (
|
||||||
|
MODULWEB = false
|
||||||
|
MODULHOST = false
|
||||||
|
)
|
|
@ -8,3 +8,8 @@ log:
|
||||||
webroot: ./webroot/build
|
webroot: ./webroot/build
|
||||||
database: "host=localhost user=warehost dbname=warehost password=hallo sslmode=disable"
|
database: "host=localhost user=warehost dbname=warehost password=hallo sslmode=disable"
|
||||||
databasedebug: false
|
databasedebug: false
|
||||||
|
modules:
|
||||||
|
web:
|
||||||
|
enabled: true
|
||||||
|
host:
|
||||||
|
enabled: true
|
||||||
|
|
|
@ -14,7 +14,9 @@ import (
|
||||||
"github.com/rs/cors"
|
"github.com/rs/cors"
|
||||||
|
|
||||||
libconfig "dev.sum7.de/sum7/warehost/config"
|
libconfig "dev.sum7.de/sum7/warehost/config"
|
||||||
|
libapi "dev.sum7.de/sum7/warehost/lib/api"
|
||||||
log "dev.sum7.de/sum7/warehost/lib/log"
|
log "dev.sum7.de/sum7/warehost/lib/log"
|
||||||
|
modulhost "dev.sum7.de/sum7/warehost/modul/host"
|
||||||
modulweb "dev.sum7.de/sum7/warehost/modul/web"
|
modulweb "dev.sum7.de/sum7/warehost/modul/web"
|
||||||
"dev.sum7.de/sum7/warehost/system"
|
"dev.sum7.de/sum7/warehost/system"
|
||||||
)
|
)
|
||||||
|
@ -49,12 +51,40 @@ func main() {
|
||||||
dbconnection.LogMode(config.DatabaseDebug)
|
dbconnection.LogMode(config.DatabaseDebug)
|
||||||
//load system Models to database
|
//load system Models to database
|
||||||
system.SyncModels(dbconnection)
|
system.SyncModels(dbconnection)
|
||||||
modulweb.SyncModels(dbconnection)
|
|
||||||
|
|
||||||
// API routes
|
// API routes
|
||||||
router := httprouter.New()
|
router := httprouter.New()
|
||||||
system.NewAPI(config, sessions, dbconnection, router, "")
|
system.NewAPI(config, sessions, dbconnection, router, "")
|
||||||
|
|
||||||
|
// START Module deklations
|
||||||
|
var modules []string
|
||||||
|
// load modul web (first)
|
||||||
|
modulname := "web"
|
||||||
|
if modul := config.Modules[modulname]; MODULWEB && modul != nil && modul.Enabled {
|
||||||
|
dbmodulconnection := modulconnection(dbconnection, modul, modulname)
|
||||||
|
modulweb.SyncModels(dbmodulconnection)
|
||||||
|
modulweb.NewAPI(config, sessions, dbmodulconnection, router, "/web")
|
||||||
|
modules = append(modules, modulname)
|
||||||
|
}
|
||||||
|
modulname = "host"
|
||||||
|
if modul := config.Modules[modulname]; MODULWEB && modul != nil && modul.Enabled {
|
||||||
|
dbmodulconnection := modulconnection(dbconnection, modul, modulname)
|
||||||
|
modulhost.SyncModels(dbmodulconnection)
|
||||||
|
modulhost.NewAPI(config, sessions, dbmodulconnection, router, "/host")
|
||||||
|
modules = append(modules, modulname)
|
||||||
|
}
|
||||||
|
// END Module deklations
|
||||||
|
|
||||||
|
// Make Modules debugable
|
||||||
|
router.GET("/modules", libapi.SessionHandler(
|
||||||
|
func(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
|
returndata = modules
|
||||||
|
return
|
||||||
|
},
|
||||||
|
sessions,
|
||||||
|
))
|
||||||
|
log.Log.Info("load modul: ", modules)
|
||||||
|
|
||||||
// Fallback filesystem
|
// Fallback filesystem
|
||||||
if config.Webroot != "" {
|
if config.Webroot != "" {
|
||||||
router.NotFound = gziphandler.GzipHandler(http.FileServer(http.Dir(config.Webroot)))
|
router.NotFound = gziphandler.GzipHandler(http.FileServer(http.Dir(config.Webroot)))
|
||||||
|
@ -73,3 +103,15 @@ func main() {
|
||||||
// TODO bad
|
// TODO bad
|
||||||
log.Log.Fatal(http.ListenAndServe(address, handler))
|
log.Log.Fatal(http.ListenAndServe(address, handler))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func modulconnection(dbconnection *gorm.DB, modul *libconfig.ModulConfig, name string) *gorm.DB {
|
||||||
|
if len(modul.Database) > 0 {
|
||||||
|
dbmodulconnection, err := gorm.Open("postgres", modul.Database)
|
||||||
|
if err != nil {
|
||||||
|
log.Log.WithField("modul", name).Fatal("database modul connection: ", err)
|
||||||
|
}
|
||||||
|
defer dbmodulconnection.Close()
|
||||||
|
return dbmodulconnection
|
||||||
|
}
|
||||||
|
return dbconnection
|
||||||
|
}
|
||||||
|
|
|
@ -17,13 +17,16 @@ type Config struct {
|
||||||
Log struct {
|
Log struct {
|
||||||
Path string `yaml:"path"`
|
Path string `yaml:"path"`
|
||||||
} `yaml:"log"`
|
} `yaml:"log"`
|
||||||
Webroot string `yaml:"webroot"`
|
Webroot string `yaml:"webroot"`
|
||||||
Database string `yaml:"database"`
|
Database string `yaml:"database"`
|
||||||
DatabaseDebug bool `yaml:"databasedebug"`
|
DatabaseDebug bool `yaml:"databasedebug"`
|
||||||
Modules []struct {
|
Modules map[string]*ModulConfig `yaml:"modules"`
|
||||||
Name string `yaml:"name"`
|
}
|
||||||
Database string `yaml:"database"`
|
|
||||||
} `yaml:"modules"`
|
// ModulConfig a spezific configuration for a modul
|
||||||
|
type ModulConfig struct {
|
||||||
|
Enabled bool `yaml:"enabled"`
|
||||||
|
Database string `yaml:"database,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadConfigFile reads a config models by path to a yml file
|
// ReadConfigFile reads a config models by path to a yml file
|
||||||
|
|
|
@ -0,0 +1,43 @@
|
||||||
|
package host
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/astaxie/session"
|
||||||
|
"github.com/jinzhu/gorm"
|
||||||
|
"github.com/julienschmidt/httprouter"
|
||||||
|
|
||||||
|
libconfig "dev.sum7.de/sum7/warehost/config"
|
||||||
|
libapi "dev.sum7.de/sum7/warehost/lib/api"
|
||||||
|
log "dev.sum7.de/sum7/warehost/lib/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
//MODULNAME to get global name for the modul
|
||||||
|
const MODULNAME = "host"
|
||||||
|
|
||||||
|
//API keep data in module global
|
||||||
|
type API struct {
|
||||||
|
config *libconfig.Config
|
||||||
|
sessions *session.Manager
|
||||||
|
dbconnection *gorm.DB
|
||||||
|
log *log.ModulLog
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewAPI sets the routes to the api functions
|
||||||
|
func NewAPI(config *libconfig.Config, sessions *session.Manager, dbconnection *gorm.DB, router *httprouter.Router, prefix string) {
|
||||||
|
api := &API{
|
||||||
|
config: config,
|
||||||
|
sessions: sessions,
|
||||||
|
dbconnection: dbconnection,
|
||||||
|
log: log.NewModulLog(MODULNAME),
|
||||||
|
}
|
||||||
|
router.GET(prefix+"/status", libapi.SessionHandler(api.Status, sessions))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Status to get Login and Server status
|
||||||
|
func (api *API) Status(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
|
returndata = true
|
||||||
|
logger := api.log.GetLog(r, "status")
|
||||||
|
logger.Info("status")
|
||||||
|
return
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
package host
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/jinzhu/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
// SyncModels to verify the database schema
|
||||||
|
func SyncModels(dbconnection *gorm.DB) {
|
||||||
|
dbconnection.AutoMigrate()
|
||||||
|
}
|
|
@ -0,0 +1,56 @@
|
||||||
|
package web
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/astaxie/session"
|
||||||
|
"github.com/jinzhu/gorm"
|
||||||
|
"github.com/julienschmidt/httprouter"
|
||||||
|
|
||||||
|
libconfig "dev.sum7.de/sum7/warehost/config"
|
||||||
|
libapi "dev.sum7.de/sum7/warehost/lib/api"
|
||||||
|
log "dev.sum7.de/sum7/warehost/lib/log"
|
||||||
|
libsystem "dev.sum7.de/sum7/warehost/system"
|
||||||
|
)
|
||||||
|
|
||||||
|
//MODULNAME to get global name for the modul
|
||||||
|
const MODULNAME = "web"
|
||||||
|
|
||||||
|
//API keep data in module global
|
||||||
|
type API struct {
|
||||||
|
config *libconfig.Config
|
||||||
|
sessions *session.Manager
|
||||||
|
dbconnection *gorm.DB
|
||||||
|
log *log.ModulLog
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewAPI sets the routes to the api functions
|
||||||
|
func NewAPI(config *libconfig.Config, sessions *session.Manager, dbconnection *gorm.DB, router *httprouter.Router, prefix string) {
|
||||||
|
api := &API{
|
||||||
|
config: config,
|
||||||
|
sessions: sessions,
|
||||||
|
dbconnection: dbconnection,
|
||||||
|
log: log.NewModulLog(MODULNAME),
|
||||||
|
}
|
||||||
|
router.GET(prefix+"/involve", libsystem.LoginHandler(api.Involve, sessions))
|
||||||
|
router.POST(prefix+"/web", libsystem.LoginHandler(api.WebsiteAdd, sessions))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Involve to get Website where loggend in user has privilegs
|
||||||
|
func (api *API) Involve(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session, login *libsystem.Login) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
|
returndata = false
|
||||||
|
logger := api.log.GetLog(r, "involve")
|
||||||
|
var involved []*Manager
|
||||||
|
api.dbconnection.Where("login = ?", login.ID).Preload("Website").Find(&involved)
|
||||||
|
logger.Info("okay")
|
||||||
|
returndata = involved
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add Website
|
||||||
|
func (api *API) WebsiteAdd(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session, login *libsystem.Login) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
|
returndata = false
|
||||||
|
logger := api.log.GetLog(r, "websiteadd")
|
||||||
|
logger.Warn("not implemented")
|
||||||
|
return
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
package web
|
|
@ -3,8 +3,7 @@ package web
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
|
//system "dev.sum7.de/sum7/warehost/system"
|
||||||
system "dev.sum7.de/sum7/warehost/system"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Website struct
|
// Website struct
|
||||||
|
@ -28,9 +27,9 @@ func (Domain) TableName() string { return "web_domain" }
|
||||||
|
|
||||||
// Manager struct
|
// Manager struct
|
||||||
type Manager struct {
|
type Manager struct {
|
||||||
Login system.Login `gorm:"column:login" json:"login"`
|
LoginID int64 `sql:"type:bigint REFERENCES login(id) ON UPDATE CASCADE ON DELETE CASCADE;column:login;primary_key" json:"login"`
|
||||||
LoginID int64 `sql:"type:bigint REFERENCES login(id) ON UPDATE CASCADE ON DELETE CASCADE;column:login;primary_key"`
|
WebsiteID int64 `sql:"type:bigint REFERENCES web_website(id) ON UPDATE CASCADE ON DELETE CASCADE;column:website;primary_key" json:"-"`
|
||||||
WebsiteID int64 `sql:"type:bigint REFERENCES web_website(id) ON UPDATE CASCADE ON DELETE CASCADE;column:website;primary_key"`
|
Website Website `gorm:"foreignkey:Website" json:"website"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// TableName of struct
|
// TableName of struct
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Subproject commit 908f09a014e1ad086771b11b0931929e844ffe25
|
Reference in New Issue