diff --git a/cmd/warehost-web/handler.go b/cmd/warehost-web/handler.go index 087a1aa..f32fcf5 100644 --- a/cmd/warehost-web/handler.go +++ b/cmd/warehost-web/handler.go @@ -29,6 +29,21 @@ func handlerstatic(w http.ResponseWriter, r *http.Request) { } 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) if f, err := os.Stat(path); err == nil && !f.IsDir() { http.ServeFile(w, r, path) @@ -37,6 +52,7 @@ func handlerstatic(w http.ResponseWriter, r *http.Request) { http.NotFound(w, r) } + func handlerfunc(w http.ResponseWriter, r *http.Request) { host := r.Header.Get(ProxyHost) if len(host) <= 1 { diff --git a/cmd/warehost-web/main.go b/cmd/warehost-web/main.go index 37b770f..2dc8f11 100644 --- a/cmd/warehost-web/main.go +++ b/cmd/warehost-web/main.go @@ -44,6 +44,7 @@ func main() { // http handler 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))) // Start server diff --git a/cmd/warehost/build.all.go b/cmd/warehost/build.all.go new file mode 100644 index 0000000..2ba22f6 --- /dev/null +++ b/cmd/warehost/build.all.go @@ -0,0 +1,9 @@ +// +build all + +package main + +// MODUL variables +const ( + MODULWEB = true + MODULHOST = true +) diff --git a/cmd/warehost/build.default.go b/cmd/warehost/build.default.go new file mode 100644 index 0000000..aae892c --- /dev/null +++ b/cmd/warehost/build.default.go @@ -0,0 +1,9 @@ +// +build !all + +package main + +// MODUL variables +const ( + MODULWEB = false + MODULHOST = false +) diff --git a/cmd/warehost/config.yml.example b/cmd/warehost/config.yml.example index 995caa9..32f39c2 100644 --- a/cmd/warehost/config.yml.example +++ b/cmd/warehost/config.yml.example @@ -8,3 +8,8 @@ log: webroot: ./webroot/build database: "host=localhost user=warehost dbname=warehost password=hallo sslmode=disable" databasedebug: false +modules: + web: + enabled: true + host: + enabled: true diff --git a/cmd/warehost/main.go b/cmd/warehost/main.go index 6d5ccf1..6158754 100644 --- a/cmd/warehost/main.go +++ b/cmd/warehost/main.go @@ -14,7 +14,9 @@ import ( "github.com/rs/cors" libconfig "dev.sum7.de/sum7/warehost/config" + libapi "dev.sum7.de/sum7/warehost/lib/api" log "dev.sum7.de/sum7/warehost/lib/log" + modulhost "dev.sum7.de/sum7/warehost/modul/host" modulweb "dev.sum7.de/sum7/warehost/modul/web" "dev.sum7.de/sum7/warehost/system" ) @@ -49,12 +51,40 @@ func main() { dbconnection.LogMode(config.DatabaseDebug) //load system Models to database system.SyncModels(dbconnection) - modulweb.SyncModels(dbconnection) // API routes router := httprouter.New() 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 if config.Webroot != "" { router.NotFound = gziphandler.GzipHandler(http.FileServer(http.Dir(config.Webroot))) @@ -73,3 +103,15 @@ func main() { // TODO bad 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 +} diff --git a/config/config.go b/config/config.go index b1ba4fa..af60a25 100644 --- a/config/config.go +++ b/config/config.go @@ -17,13 +17,16 @@ type Config struct { Log struct { Path string `yaml:"path"` } `yaml:"log"` - Webroot string `yaml:"webroot"` - Database string `yaml:"database"` - DatabaseDebug bool `yaml:"databasedebug"` - Modules []struct { - Name string `yaml:"name"` - Database string `yaml:"database"` - } `yaml:"modules"` + Webroot string `yaml:"webroot"` + Database string `yaml:"database"` + DatabaseDebug bool `yaml:"databasedebug"` + Modules map[string]*ModulConfig `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 diff --git a/modul/host/api.go b/modul/host/api.go new file mode 100644 index 0000000..57834bb --- /dev/null +++ b/modul/host/api.go @@ -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 +} diff --git a/modul/host/models.go b/modul/host/models.go new file mode 100644 index 0000000..5f39119 --- /dev/null +++ b/modul/host/models.go @@ -0,0 +1,10 @@ +package host + +import ( + "github.com/jinzhu/gorm" +) + +// SyncModels to verify the database schema +func SyncModels(dbconnection *gorm.DB) { + dbconnection.AutoMigrate() +} diff --git a/modul/web/api.go b/modul/web/api.go new file mode 100644 index 0000000..fc49716 --- /dev/null +++ b/modul/web/api.go @@ -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 +} diff --git a/modul/web/lib.go b/modul/web/lib.go new file mode 100644 index 0000000..efb3895 --- /dev/null +++ b/modul/web/lib.go @@ -0,0 +1 @@ +package web diff --git a/modul/web/models.go b/modul/web/models.go index 0ab1da9..cb84de4 100644 --- a/modul/web/models.go +++ b/modul/web/models.go @@ -3,8 +3,7 @@ package web import ( "database/sql" "github.com/jinzhu/gorm" - - system "dev.sum7.de/sum7/warehost/system" + //system "dev.sum7.de/sum7/warehost/system" ) // Website struct @@ -28,9 +27,9 @@ func (Domain) TableName() string { return "web_domain" } // 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"` - WebsiteID int64 `sql:"type:bigint REFERENCES web_website(id) ON UPDATE CASCADE ON DELETE CASCADE;column:website;primary_key"` + LoginID int64 `sql:"type:bigint REFERENCES login(id) ON UPDATE CASCADE ON DELETE CASCADE;column:login;primary_key" json:"login"` + WebsiteID int64 `sql:"type:bigint REFERENCES web_website(id) ON UPDATE CASCADE ON DELETE CASCADE;column:website;primary_key" json:"-"` + Website Website `gorm:"foreignkey:Website" json:"website"` } // TableName of struct diff --git a/webroot b/webroot new file mode 160000 index 0000000..908f09a --- /dev/null +++ b/webroot @@ -0,0 +1 @@ +Subproject commit 908f09a014e1ad086771b11b0931929e844ffe25