diff --git a/cmd/warehost-host/pure-ftp-auth/main.go b/cmd/warehost-host/pure-ftp-auth/main.go index 69d6b92..525176c 100644 --- a/cmd/warehost-host/pure-ftp-auth/main.go +++ b/cmd/warehost-host/pure-ftp-auth/main.go @@ -8,9 +8,9 @@ import ( "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/postgres" - log "dev.sum7.de/sum7/warehost/lib/log" - libpassword "dev.sum7.de/sum7/warehost/lib/password" - system "dev.sum7.de/sum7/warehost/system" + liblog "dev.sum7.eu/sum7/warehost/lib/log" + libpassword "dev.sum7.eu/sum7/warehost/lib/password" + system "dev.sum7.eu/sum7/warehost/system" ) var ( @@ -27,12 +27,12 @@ func main() { } config = ReadConfigFile(configFile) - log.NewSilenceLogger(config.Log.Path) + liblog.NewSilenceLogger(config.Log.Path) // Main Databaseconnection dbconnection, err = gorm.Open("postgres", config.Database) if err != nil { - log.Log.Fatal("database connection: ", err) + liblog.Log.Fatal("database connection: ", err) } defer dbconnection.Close() dbconnection.SingularTable(true) @@ -40,7 +40,7 @@ func main() { username := os.Getenv("AUTHD_ACCOUNT") password := os.Getenv("AUTHD_PASSWORD") - logger := log.Log.WithField("user", username) + logger := liblog.Log.WithField("user", username) login := system.Login{Username: username} dbconnection.Where("mail = ?", username).First(&login) if login.ID <= 0 { diff --git a/cmd/warehost-web/handler.go b/cmd/warehost-web/handler.go index e9bbad4..9da1ce4 100644 --- a/cmd/warehost-web/handler.go +++ b/cmd/warehost-web/handler.go @@ -9,15 +9,15 @@ import ( "github.com/microcosm-cc/bluemonday" "github.com/russross/blackfriday" - log "dev.sum7.de/sum7/warehost/lib/log" - modul "dev.sum7.de/sum7/warehost/modul/web" + liblog "dev.sum7.eu/sum7/warehost/lib/log" + web "dev.sum7.eu/sum7/warehost/modul/web" ) //ProxyHost if this server is behind a proxy const ProxyHost = "X-Real-Host" -func getWebsite(host string) *modul.Website { - website := &modul.Website{} +func getWebsite(host string) *web.Website { + website := &web.Website{} dbconnection.Model(website).Joins("JOIN web_domain ON web_domain.website = web_website.id").Where("web_domain.name = ?", host).First(website) return website } @@ -59,13 +59,13 @@ func handlerfunc(w http.ResponseWriter, r *http.Request) { host = r.Host } url := r.URL.Path[1:] - logger := log.NewModulLog(host).GetLog(r, url) + logger := liblog.NewModulLog(host).GetLog(r, url) website := getWebsite(host) logger = logger.WithField("hID", website.ID) - var menus []*modul.Menu + var menus []*web.Menu dbconnection.Where("website = ?", website.ID).Preload("Menu").Order("position").Find(&menus) - var menu *modul.Menu + var menu *web.Menu for _, item := range menus { if item.Shorturl == "" && menu == nil { menu = item @@ -74,8 +74,8 @@ func handlerfunc(w http.ResponseWriter, r *http.Request) { menu = item } } - menus = modul.BuildMenuTree(menus) - page := &modul.Page{WebsiteID: website.ID, MenuID: menu.ID} + menus = web.BuildMenuTree(menus) + page := &web.Page{WebsiteID: website.ID, MenuID: menu.ID} dbconnection.Where(page).Find(page) unsafe := blackfriday.MarkdownCommon([]byte(page.Content)) diff --git a/cmd/warehost-web/main.go b/cmd/warehost-web/main.go index 2dc8f11..9d4f74a 100644 --- a/cmd/warehost-web/main.go +++ b/cmd/warehost-web/main.go @@ -9,9 +9,9 @@ import ( "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/postgres" - log "dev.sum7.de/sum7/warehost/lib/log" - modul "dev.sum7.de/sum7/warehost/modul/web" - system "dev.sum7.de/sum7/warehost/system" + liblog "dev.sum7.eu/sum7/warehost/lib/log" + web "dev.sum7.eu/sum7/warehost/modul/web" + system "dev.sum7.eu/sum7/warehost/system" ) var ( @@ -25,12 +25,12 @@ func main() { flag.StringVar(&configFile, "c", "config.yml", "path of configuration file") flag.Parse() config = ReadConfigFile(configFile) - log.NewLogger(config.Log.Path) + liblog.NewLogger(config.Log.Path) // Main Databaseconnection dbconnection, err = gorm.Open("postgres", config.Database) if err != nil { - log.Log.Fatal("database connection: ", err) + liblog.Log.Fatal("database connection: ", err) } defer dbconnection.Close() dbconnection.Callback().Create().Remove("gorm:update_time_stamp") @@ -40,7 +40,7 @@ func main() { //load system Models to database system.SyncModels(dbconnection) - modul.SyncModels(dbconnection) + web.SyncModels(dbconnection) // http handler http.Handle("/static/", gziphandler.GzipHandler(http.StripPrefix("/static/", http.HandlerFunc(handlerstatic)))) @@ -49,7 +49,7 @@ func main() { // Start server address := net.JoinHostPort(config.Address, config.Port) - log.Log.Info("starting warehost web on ", address) + liblog.Log.Info("starting warehost web on ", address) // TODO bad - log.Log.Fatal(http.ListenAndServe(address, nil)) + liblog.Log.Fatal(http.ListenAndServe(address, nil)) } diff --git a/cmd/warehost-web/model.go b/cmd/warehost-web/model.go index e7ccbe8..a065df1 100644 --- a/cmd/warehost-web/model.go +++ b/cmd/warehost-web/model.go @@ -1,6 +1,6 @@ package main -import modul "dev.sum7.de/sum7/warehost/modul/web" +import modul "dev.sum7.eu/sum7/warehost/modul/web" //TemplateInfo Information send to Template type TemplateInfo struct { diff --git a/cmd/warehost/main.go b/cmd/warehost/main.go index 992351c..e362622 100644 --- a/cmd/warehost/main.go +++ b/cmd/warehost/main.go @@ -6,111 +6,112 @@ import ( "net/http" "github.com/NYTimes/gziphandler" - "github.com/astaxie/session" - _ "github.com/astaxie/session/providers/memory" "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/postgres" - "github.com/julienschmidt/httprouter" "github.com/rs/cors" + "goji.io" + "goji.io/pat" + "golang.org/x/net/context" - 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" + config "dev.sum7.eu/sum7/warehost/config" + libapi "dev.sum7.eu/sum7/warehost/lib/api" + liblog "dev.sum7.eu/sum7/warehost/lib/log" + libsession "dev.sum7.eu/sum7/warehost/lib/session" + modulhost "dev.sum7.eu/sum7/warehost/modul/host" + modulweb "dev.sum7.eu/sum7/warehost/modul/web" + "dev.sum7.eu/sum7/warehost/system" ) var ( configFile string - config *libconfig.Config dbconnection *gorm.DB - sessions *session.Manager ) func main() { var err error flag.StringVar(&configFile, "c", "config.yml", "path of configuration file") flag.Parse() - config = libconfig.ReadConfigFile(configFile) - log.NewLogger(config.Log.Path) + config.ReadConfigFile(configFile) + liblog.NewLogger(config.Data.Log.Path) // Session mgmt - sessions, _ = session.NewManager("memory", "session", 3600) - go sessions.GC() + libsession.Init() + go libsession.Stop() // Main Databaseconnection - dbconnection, err = gorm.Open("postgres", config.Database) + dbconnection, err = gorm.Open("postgres", config.Data.Database) if err != nil { - log.Log.Fatal("database connection: ", err) + liblog.Log.Fatal("database connection: ", err) } defer dbconnection.Close() dbconnection.Callback().Create().Remove("gorm:update_time_stamp") dbconnection.Callback().Update().Remove("gorm:update_time_stamp") dbconnection.SingularTable(true) - dbconnection.LogMode(config.DatabaseDebug) + dbconnection.LogMode(config.Data.DatabaseDebug) //load system Models to database system.SyncModels(dbconnection) // API routes - router := httprouter.New() - system.NewAPI(config, sessions, dbconnection, router, "") + router := goji.NewMux() + system.BindAPI(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) + if modul := config.Data.Modules[modulname]; MODULWEB && modul != nil && modul.Enabled { + dbmodulconnection := modulconnection(modul, modulname) modulweb.SyncModels(dbmodulconnection) - modulweb.NewAPI(config, sessions, dbmodulconnection, router, "/web") + modulweb.BindAPI(dbmodulconnection, router, "/web") modules = append(modules, modulname) } modulname = "host" - if modul := config.Modules[modulname]; MODULHOST && modul != nil && modul.Enabled { - dbmodulconnection := modulconnection(dbconnection, modul, modulname) + if modul := config.Data.Modules[modulname]; MODULHOST && modul != nil && modul.Enabled { + dbmodulconnection := modulconnection(modul, modulname) modulhost.SyncModels(dbmodulconnection) - modulhost.NewAPI(config, sessions, dbmodulconnection, router, "/host") + modulhost.BindAPI(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) { + router.HandleFuncC(pat.Get("/modules"), libapi.SessionHandler( + func(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { returndata = modules return - }, - sessions, - )) - log.Log.Info("load modul: ", modules) + })) + liblog.Log.Info("load modul: ", modules) // Fallback filesystem - if config.Webroot != "" { - router.NotFound = gziphandler.GzipHandler(http.FileServer(http.Dir(config.Webroot))) + if config.Data.Webroot != "" { + router.Handle(pat.Get(""), gziphandler.GzipHandler(http.FileServer(http.Dir(config.Data.Webroot)))) } // Manage CORS (JSONOutput allow requested -> lib/api) c := cors.New(cors.Options{ - AllowedOrigins: []string{config.API.AllowedOrigins}, + AllowedOrigins: []string{config.Data.API.AllowedOrigins}, AllowCredentials: true, AllowedMethods: []string{"GET", "POST", "PUT", "DELETE"}, }) handler := c.Handler(router) // Start server - address := net.JoinHostPort(config.API.Address, config.API.Port) - log.Log.Info("starting api on ", address) + address := net.JoinHostPort(config.Data.API.Address, config.Data.API.Port) + liblog.Log.Info("starting api on ", address) // TODO bad - log.Log.Fatal(http.ListenAndServe(address, handler)) + liblog.Log.Fatal(http.ListenAndServe(address, handler)) } -func modulconnection(dbconnection *gorm.DB, modul *libconfig.ModulConfig, name string) *gorm.DB { +func modulconnection(modul *config.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) + liblog.Log.WithField("modul", name).Fatal("database modul connection: ", err) } defer dbmodulconnection.Close() + dbmodulconnection.Callback().Create().Remove("gorm:update_time_stamp") + dbmodulconnection.Callback().Update().Remove("gorm:update_time_stamp") + dbmodulconnection.SingularTable(true) + dbmodulconnection.LogMode(config.Data.DatabaseDebug) return dbmodulconnection } return dbconnection diff --git a/config/config.go b/config/config.go index d52d1a2..4556a20 100644 --- a/config/config.go +++ b/config/config.go @@ -6,6 +6,9 @@ import ( "log" ) +// Data of readed configuration +var Data *Config + // Config is the struct of the api type Config struct { API struct { @@ -29,12 +32,12 @@ type ModulConfig struct { } // ReadConfigFile reads a config models by path to a yml file -func ReadConfigFile(path string) *Config { +func ReadConfigFile(path string) { config := &Config{} file, _ := ioutil.ReadFile(path) err := yaml.Unmarshal(file, &config) if err != nil { log.Fatal(err) } - return config + Data = config } diff --git a/lib/api/main.go b/lib/api/main.go index 3512061..903344d 100644 --- a/lib/api/main.go +++ b/lib/api/main.go @@ -4,12 +4,14 @@ import ( "encoding/json" "net/http" - "github.com/astaxie/session" - "github.com/julienschmidt/httprouter" + "goji.io" + "golang.org/x/net/context" + + libsession "dev.sum7.eu/sum7/warehost/lib/session" ) -// Handle for session -type Handle func(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session) (interface{}, *ErrorResult) +// Handle with response +type Handle func(ctx context.Context, w http.ResponseWriter, r *http.Request) (interface{}, *ErrorResult) // ErrorResult struct for api error answer type ErrorResult struct { @@ -28,7 +30,8 @@ type JSONResult struct { } // JSONOutput generate default json answer -func JSONOutput(w http.ResponseWriter, r *http.Request, sess session.Session, data interface{}, errorresult *ErrorResult) { +func JSONOutput(ctx context.Context, w http.ResponseWriter, r *http.Request, data interface{}, errorresult *ErrorResult) { + sess := ctx.Value("session").(libsession.Session) result := JSONResult{Data: data, Error: errorresult} result.Session.Login = sess.Get("login") js, err := json.Marshal(result) @@ -48,10 +51,11 @@ func JSONOutput(w http.ResponseWriter, r *http.Request, sess session.Session, da } // SessionHandler Handler to manage session of api request -func SessionHandler(h Handle, sessions *session.Manager) httprouter.Handle { - return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - sess := sessions.SessionStart(w, r) - data, err := h(w, r, ps, sess) - JSONOutput(w, r, sess, data, err) +func SessionHandler(h Handle) goji.HandlerFunc { + return func(ctx context.Context, w http.ResponseWriter, r *http.Request) { + sess := libsession.SessionStart(w, r) + ctx = context.WithValue(ctx, "session", sess) + data, err := h(ctx, w, r) + JSONOutput(ctx, w, r, data, err) } } diff --git a/lib/session/main.go b/lib/session/main.go new file mode 100644 index 0000000..f6e61fd --- /dev/null +++ b/lib/session/main.go @@ -0,0 +1,36 @@ +package session + +import ( + "net/http" + + assession "github.com/astaxie/session" + _ "github.com/astaxie/session/providers/memory" +) + +// Session a Session object with all data +type Session struct { + assession.Session +} + +// Data of readed configuration +var data *assession.Manager + +// Init session manager +func Init() { + data, _ = assession.NewManager("memory", "session", 3600) +} + +// Stop cleanup session manager +func Stop() { + data.GC() +} + +// SessionStart init a session on a request +func SessionStart(w http.ResponseWriter, r *http.Request) assession.Session { + return data.SessionStart(w, r) +} + +// SessionDestroy destroy a session on a request +func SessionDestroy(w http.ResponseWriter, r *http.Request) assession.Session { + return SessionDestroy(w, r) +} diff --git a/modul/host/api.go b/modul/host/api.go index 57834bb..dac7aa0 100644 --- a/modul/host/api.go +++ b/modul/host/api.go @@ -3,41 +3,33 @@ package host import ( "net/http" - "github.com/astaxie/session" "github.com/jinzhu/gorm" - "github.com/julienschmidt/httprouter" + "goji.io" + "goji.io/pat" + "golang.org/x/net/context" - libconfig "dev.sum7.de/sum7/warehost/config" - libapi "dev.sum7.de/sum7/warehost/lib/api" - log "dev.sum7.de/sum7/warehost/lib/log" + libapi "dev.sum7.eu/sum7/warehost/lib/api" + liblog "dev.sum7.eu/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 -} +var dbconnection *gorm.DB +var log *liblog.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)) +// BindAPI sets the routes to the api functions +func BindAPI(db *gorm.DB, router *goji.Mux, prefix string) { + dbconnection = db + log = liblog.NewModulLog(MODULNAME) + + router.HandleFuncC(pat.Get(prefix+"/status"), libapi.SessionHandler(status)) } // 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) { +func status(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { returndata = true - logger := api.log.GetLog(r, "status") + logger := log.GetLog(r, "status") logger.Info("status") return } diff --git a/modul/web/api.go b/modul/web/api.go index 902bbf6..f22e28d 100644 --- a/modul/web/api.go +++ b/modul/web/api.go @@ -4,72 +4,67 @@ import ( "encoding/json" "net/http" - "github.com/astaxie/session" "github.com/jinzhu/gorm" - "github.com/julienschmidt/httprouter" + "goji.io" + "goji.io/pat" + "golang.org/x/net/context" - 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" + //libconfig "dev.sum7.eu/sum7/warehost/config" + libapi "dev.sum7.eu/sum7/warehost/lib/api" + liblog "dev.sum7.eu/sum7/warehost/lib/log" + libsystem "dev.sum7.eu/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 -} +var dbconnection *gorm.DB +var log *liblog.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+"/website", libsystem.LoginHandler(api.WebsiteAdd, sessions)) - router.PUT(prefix+"/website/:websiteid", InvolveWebsiteHandler(api.WebsiteEdit, sessions, dbconnection)) - router.DELETE(prefix+"/website/:websiteid", InvolveWebsiteHandler(api.WebsiteDelete, sessions, dbconnection)) - router.GET(prefix+"/website/:websiteid/permission", InvolveWebsiteHandler(api.PermissionList, sessions, dbconnection)) - router.POST(prefix+"/website/:websiteid/permission/:loginid", InvolveWebsiteHandler(api.PermissionAdd, sessions, dbconnection)) - router.DELETE(prefix+"/website/:websiteid/permission/:loginid", InvolveWebsiteHandler(api.PermissionDelete, sessions, dbconnection)) - router.GET(prefix+"/website/:websiteid/domain", InvolveWebsiteHandler(api.DomainList, sessions, dbconnection)) - router.POST(prefix+"/website/:websiteid/domain/:domain", InvolveWebsiteHandler(api.DomainAdd, sessions, dbconnection)) - router.DELETE(prefix+"/website/:websiteid/domain/:domain", InvolveWebsiteHandler(api.DomainDelete, sessions, dbconnection)) - router.GET(prefix+"/website/:websiteid/menu", InvolveWebsiteHandler(api.MenuTree, sessions, dbconnection)) - router.GET(prefix+"/website/:websiteid/menu/list", InvolveWebsiteHandler(api.MenuList, sessions, dbconnection)) - router.POST(prefix+"/website/:websiteid/menu", InvolveWebsiteHandler(api.MenuAdd, sessions, dbconnection)) - router.PUT(prefix+"/website/:websiteid/menu/:menuid", InvolveWebsiteHandler(api.MenuEdit, sessions, dbconnection)) - router.DELETE(prefix+"/website/:websiteid/menu/:menuid", InvolveWebsiteHandler(api.MenuDelete, sessions, dbconnection)) - router.GET(prefix+"/website/:websiteid/page", InvolveWebsiteHandler(api.PageList, sessions, dbconnection)) - router.POST(prefix+"/website/:websiteid/page", InvolveWebsiteHandler(api.PageAdd, sessions, dbconnection)) - router.PUT(prefix+"/website/:websiteid/page/:pageid", InvolveWebsiteHandler(api.PageEdit, sessions, dbconnection)) - router.DELETE(prefix+"/website/:websiteid/page/:pageid", InvolveWebsiteHandler(api.PageDelete, sessions, dbconnection)) +// BindAPI sets the routes to the api functions +func BindAPI(db *gorm.DB, router *goji.Mux, prefix string) { + dbconnection = db + log = liblog.NewModulLog(MODULNAME) + + router.HandleFuncC(pat.Get(prefix+"/involve"), libapi.SessionHandler(libsystem.LoginHandler(involve))) + router.HandleFuncC(pat.Post(prefix+"/website"), libapi.SessionHandler(libsystem.LoginHandler(websiteAdd))) + router.HandleFuncC(pat.Put(prefix+"/website/:websiteid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(websiteEdit)))) + router.HandleFuncC(pat.Delete(prefix+"/website/:websiteid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(websiteDelete)))) + router.HandleFuncC(pat.Get(prefix+"/website/:websiteid/permission"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(permissionList)))) + router.HandleFuncC(pat.Post(prefix+"/website/:websiteid/permission/:loginid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(permissionAdd)))) + router.HandleFuncC(pat.Delete(prefix+"/website/:websiteid/permission/:loginid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(permissionDelete)))) + router.HandleFuncC(pat.Get(prefix+"/website/:websiteid/domain"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(domainList)))) + router.HandleFuncC(pat.Post(prefix+"/website/:websiteid/domain/:domain"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(domainAdd)))) + router.HandleFuncC(pat.Delete(prefix+"/website/:websiteid/domain/:domain"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(domainDelete)))) + router.HandleFuncC(pat.Get(prefix+"/website/:websiteid/menu"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(menuTree)))) + router.HandleFuncC(pat.Get(prefix+"/website/:websiteid/menu/list"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(menuList)))) + router.HandleFuncC(pat.Post(prefix+"/website/:websiteid/menu"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(menuAdd)))) + router.HandleFuncC(pat.Put(prefix+"/website/:websiteid/menu/:menuid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(menuEdit)))) + router.HandleFuncC(pat.Delete(prefix+"/website/:websiteid/menu/:menuid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(menuDelete)))) + router.HandleFuncC(pat.Get(prefix+"/website/:websiteid/page"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(pageList)))) + router.HandleFuncC(pat.Post(prefix+"/website/:websiteid/page"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(pageAdd)))) + router.HandleFuncC(pat.Put(prefix+"/website/:websiteid/page/:pageid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(pageEdit)))) + router.HandleFuncC(pat.Delete(prefix+"/website/:websiteid/page/:pageid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(pageDelete)))) } // 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) { +func involve(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { + login := ctx.Value("login").(*libsystem.Login) returndata = false - logger := api.log.GetLog(r, "involve") + logger := log.GetLog(r, "involve") var involved []*Manager - api.dbconnection.Where("login = ?", login.ID).Preload("Website").Find(&involved) + dbconnection.Where("login = ?", login.ID).Preload("Website").Find(&involved) logger.Info("done") returndata = involved return } // WebsiteAdd to add a new website -func (api *API) WebsiteAdd(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session, login *libsystem.Login) (returndata interface{}, returnerr *libapi.ErrorResult) { +func websiteAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { + login := ctx.Value("login").(*libsystem.Login) returndata = false - logger := api.log.GetLog(r, "websiteadd") - tx := api.dbconnection.Begin() + logger := log.GetLog(r, "websiteadd") + tx := dbconnection.Begin() var websiteRequest Website err := json.NewDecoder(r.Body).Decode(&websiteRequest) if err != nil { @@ -101,9 +96,9 @@ func (api *API) WebsiteAdd(w http.ResponseWriter, r *http.Request, _ httprouter. } // WebsiteEdit to edit website -func (api *API) WebsiteEdit(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) { +func websiteEdit(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { returndata = false - logger := api.log.GetLog(r, "websiteedit") + logger := log.GetLog(r, "websiteedit") var websiteRequest Website err := json.NewDecoder(r.Body).Decode(&websiteRequest) if err != nil { @@ -112,8 +107,8 @@ func (api *API) WebsiteEdit(w http.ResponseWriter, r *http.Request, _ httprouter returnerr = &libapi.ErrorResult{Message: "Internal Request Error"} return } - websiteRequest.ID = websiteid - if err := api.dbconnection.Save(websiteRequest).Error; err != nil { + websiteRequest.ID = ctx.Value("websiteid").(int64) + if err := dbconnection.Save(websiteRequest).Error; err != nil { logger.Error("Database: during edit Website") returnerr = &libapi.ErrorResult{Message: "Internal Database Error"} return @@ -124,13 +119,13 @@ func (api *API) WebsiteEdit(w http.ResponseWriter, r *http.Request, _ httprouter } // WebsiteDelete to delete website -func (api *API) WebsiteDelete(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) { +func websiteDelete(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { returndata = false - logger := api.log.GetLog(r, "websitedelete") + logger := log.GetLog(r, "websitedelete") website := &Website{ - ID: websiteid, + ID: ctx.Value("websiteid").(int64), } - if err := api.dbconnection.Unscoped().Delete(website).Error; err != nil { + if err := dbconnection.Unscoped().Delete(website).Error; err != nil { logger.Error("database: during delete website") returnerr = &libapi.ErrorResult{Message: "Internal Database Error"} return diff --git a/modul/web/apidomain.go b/modul/web/apidomain.go index dd3e7e4..bd1ba83 100644 --- a/modul/web/apidomain.go +++ b/modul/web/apidomain.go @@ -3,33 +3,32 @@ package web import ( "net/http" - "github.com/astaxie/session" - "github.com/julienschmidt/httprouter" + "goji.io/pat" + "golang.org/x/net/context" - libapi "dev.sum7.de/sum7/warehost/lib/api" - libsystem "dev.sum7.de/sum7/warehost/system" + libapi "dev.sum7.eu/sum7/warehost/lib/api" ) // DomainList to list domains -func (api *API) DomainList(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) { +func domainList(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { returndata = false - logger := api.log.GetLog(r, "domainlist") + logger := log.GetLog(r, "domainlist") var domain []*Domain - api.dbconnection.Where("website = ?", websiteid).Preload("Domains").Find(&domain) + dbconnection.Where("website = ?", ctx.Value("websiteid").(int64)).Preload("Domains").Find(&domain) logger.Info("done") returndata = domain return } // DomainAdd to add domain -func (api *API) DomainAdd(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) { +func domainAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { returndata = false - logger := api.log.GetLog(r, "domainadd") + logger := log.GetLog(r, "domainadd") domain := &Domain{ - WebsiteID: websiteid, - Name: ps.ByName("domain"), + WebsiteID: ctx.Value("websiteid").(int64), + Name: pat.Param(ctx, "domain"), } - if err := api.dbconnection.Create(domain).Error; err != nil { + if err := dbconnection.Create(domain).Error; err != nil { logger.Error("database: during create website domain: ", err) returnerr = &libapi.ErrorResult{Message: "Internal Database Error"} return @@ -40,14 +39,14 @@ func (api *API) DomainAdd(w http.ResponseWriter, r *http.Request, ps httprouter. } // DomainDelete to delete domain -func (api *API) DomainDelete(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) { +func domainDelete(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { returndata = false - logger := api.log.GetLog(r, "domaindelete") + logger := log.GetLog(r, "domaindelete") domain := &Domain{ - WebsiteID: websiteid, - Name: ps.ByName("domain"), + WebsiteID: ctx.Value("websiteid").(int64), + Name: pat.Param(ctx, "domain"), } - if err := api.dbconnection.Unscoped().Delete(domain).Error; err != nil { + if err := dbconnection.Unscoped().Delete(domain).Error; err != nil { logger.Error("database: during delete website Domain") returnerr = &libapi.ErrorResult{Message: "Internal Database Error"} return diff --git a/modul/web/apimenu.go b/modul/web/apimenu.go index e5fa469..1c0dd35 100644 --- a/modul/web/apimenu.go +++ b/modul/web/apimenu.go @@ -5,39 +5,38 @@ import ( "net/http" "strconv" - "github.com/astaxie/session" - "github.com/julienschmidt/httprouter" + "goji.io/pat" + "golang.org/x/net/context" - libapi "dev.sum7.de/sum7/warehost/lib/api" - libsystem "dev.sum7.de/sum7/warehost/system" + libapi "dev.sum7.eu/sum7/warehost/lib/api" ) // MenuTree to give the tree of a menu back -func (api *API) MenuTree(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) { +func menuTree(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { returndata = false - logger := api.log.GetLog(r, "menutree") + logger := log.GetLog(r, "menutree") var menus []*Menu - api.dbconnection.Where("website = ?", websiteid).Order("position").Find(&menus) + dbconnection.Where("website = ?", ctx.Value("websiteid").(int64)).Order("position").Find(&menus) returndata = BuildMenuTree(menus) logger.Info("done") return } // MenuList give all menu entries of a website -func (api *API) MenuList(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) { +func menuList(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { returndata = false - logger := api.log.GetLog(r, "menulist") + logger := log.GetLog(r, "menulist") var menus []*Menu - api.dbconnection.Where("website = ?", websiteid).Order("position").Find(&menus) + dbconnection.Where("website = ?", ctx.Value("websiteid").(int64)).Order("position").Find(&menus) returndata = menus logger.Info("done") return } // MenuAdd to add a new menu entry -func (api *API) MenuAdd(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) { +func menuAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { returndata = false - logger := api.log.GetLog(r, "menuadd") + logger := log.GetLog(r, "menuadd") var menuEntry Menu err := json.NewDecoder(r.Body).Decode(&menuEntry) if err != nil { @@ -49,9 +48,9 @@ func (api *API) MenuAdd(w http.ResponseWriter, r *http.Request, ps httprouter.Pa return } - menuEntry.WebsiteID = websiteid + menuEntry.WebsiteID = ctx.Value("websiteid").(int64) - if err := api.dbconnection.Create(&menuEntry).Error; err != nil { + if err := dbconnection.Create(&menuEntry).Error; err != nil { logger.Error("database: during create menu") returnerr = &libapi.ErrorResult{Message: "Internal Database Error"} return @@ -62,11 +61,11 @@ func (api *API) MenuAdd(w http.ResponseWriter, r *http.Request, ps httprouter.Pa } // MenuEdit to edit menu -func (api *API) MenuEdit(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) { +func menuEdit(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { returndata = false - logger := api.log.GetLog(r, "menuedit") + logger := log.GetLog(r, "menuedit") var menuEntry Menu - menuid, err := strconv.ParseInt(ps.ByName("menuid"), 10, 64) + menuid, err := strconv.ParseInt(pat.Param(ctx, "menuid"), 10, 64) if err != nil { returnerr = &libapi.ErrorResult{Fields: []string{"menuid"}, Message: "Not a valid menuid"} logger.Warn("invalid loginid, no integer") @@ -82,10 +81,10 @@ func (api *API) MenuEdit(w http.ResponseWriter, r *http.Request, ps httprouter.P } return } - menuEntry.WebsiteID = websiteid + menuEntry.WebsiteID = ctx.Value("websiteid").(int64) menuEntry.ID = menuid - if err := api.dbconnection.Save(menuEntry).Error; err != nil { + if err := dbconnection.Save(menuEntry).Error; err != nil { logger.Error("database: during delete website menu entry") returnerr = &libapi.ErrorResult{Message: "Internal Database Error"} return @@ -96,10 +95,10 @@ func (api *API) MenuEdit(w http.ResponseWriter, r *http.Request, ps httprouter.P } // MenuDelete to delete menu entry -func (api *API) MenuDelete(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) { +func menuDelete(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { returndata = false - logger := api.log.GetLog(r, "menudelete") - menuid, err := strconv.ParseInt(ps.ByName("menuid"), 10, 64) + logger := log.GetLog(r, "menudelete") + menuid, err := strconv.ParseInt(pat.Param(ctx, "menuid"), 10, 64) if err != nil { returnerr = &libapi.ErrorResult{Fields: []string{"menuid"}, Message: "Not a valid menuid"} logger.Warn("invalid menuid, no integer") @@ -107,10 +106,10 @@ func (api *API) MenuDelete(w http.ResponseWriter, r *http.Request, ps httprouter } logger = logger.WithField("id", menuid) menu := &Menu{ - WebsiteID: websiteid, + WebsiteID: ctx.Value("websiteid").(int64), ID: menuid, } - if err := api.dbconnection.Unscoped().Delete(menu).Error; err != nil { + if err := dbconnection.Unscoped().Delete(menu).Error; err != nil { logger.Error("database: during delete website menu entry") returnerr = &libapi.ErrorResult{Message: "Internal Database Error"} return diff --git a/modul/web/apipage.go b/modul/web/apipage.go index 1ba92ff..f7e7b6b 100644 --- a/modul/web/apipage.go +++ b/modul/web/apipage.go @@ -5,28 +5,27 @@ import ( "net/http" "strconv" - "github.com/astaxie/session" - "github.com/julienschmidt/httprouter" + "goji.io/pat" + "golang.org/x/net/context" - libapi "dev.sum7.de/sum7/warehost/lib/api" - libsystem "dev.sum7.de/sum7/warehost/system" + libapi "dev.sum7.eu/sum7/warehost/lib/api" ) // PageList give all pages of a website -func (api *API) PageList(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) { +func pageList(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { returndata = false - logger := api.log.GetLog(r, "pagelist") + logger := log.GetLog(r, "pagelist") var pages []*Page - api.dbconnection.Where("website = ?", websiteid).Preload("Menu").Find(&pages) + dbconnection.Where("website = ?", ctx.Value("websiteid").(int64)).Preload("Menu").Find(&pages) returndata = pages logger.Info("done") return } // PageAdd to add a new page -func (api *API) PageAdd(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) { +func pageAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { returndata = false - logger := api.log.GetLog(r, "pageadd") + logger := log.GetLog(r, "pageadd") var page Page err := json.NewDecoder(r.Body).Decode(&page) if err != nil { @@ -38,13 +37,13 @@ func (api *API) PageAdd(w http.ResponseWriter, r *http.Request, ps httprouter.Pa return } - page.WebsiteID = websiteid + page.WebsiteID = ctx.Value("websiteid").(int64) if menu := page.Menu; menu != nil { page.MenuID = page.Menu.ID page.Menu = nil } - if err := api.dbconnection.Create(&page).Error; err != nil { + if err := dbconnection.Create(&page).Error; err != nil { logger.Error("database: during create page") returnerr = &libapi.ErrorResult{Message: "Internal Database Error"} return @@ -55,11 +54,11 @@ func (api *API) PageAdd(w http.ResponseWriter, r *http.Request, ps httprouter.Pa } // PageEdit to edit page -func (api *API) PageEdit(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) { +func pageEdit(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { returndata = false - logger := api.log.GetLog(r, "pageedit") + logger := log.GetLog(r, "pageedit") var page Page - pageid, err := strconv.ParseInt(ps.ByName("pageid"), 10, 64) + pageid, err := strconv.ParseInt(pat.Param(ctx, "pageid"), 10, 64) if err != nil { returnerr = &libapi.ErrorResult{Fields: []string{"pageid"}, Message: "Not a valid pageid"} logger.Warn("invalid pageid, no integer") @@ -75,14 +74,14 @@ func (api *API) PageEdit(w http.ResponseWriter, r *http.Request, ps httprouter.P } return } - page.WebsiteID = websiteid + page.WebsiteID = ctx.Value("websiteid").(int64) page.ID = pageid if menu := page.Menu; menu != nil { page.MenuID = page.Menu.ID page.Menu = nil } - if err := api.dbconnection.Save(page).Error; err != nil { + if err := dbconnection.Save(page).Error; err != nil { logger.Error("database: during delete website page") returnerr = &libapi.ErrorResult{Message: "Internal Database Error"} return @@ -93,10 +92,10 @@ func (api *API) PageEdit(w http.ResponseWriter, r *http.Request, ps httprouter.P } // PageDelete to delete page -func (api *API) PageDelete(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) { +func pageDelete(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { returndata = false - logger := api.log.GetLog(r, "pagedelete") - pageid, err := strconv.ParseInt(ps.ByName("pageid"), 10, 64) + logger := log.GetLog(r, "pagedelete") + pageid, err := strconv.ParseInt(pat.Param(ctx, "pageid"), 10, 64) if err != nil { returnerr = &libapi.ErrorResult{Fields: []string{"pageid"}, Message: "Not a valid pageid"} logger.Warn("invalid pageid, no integer") @@ -104,10 +103,10 @@ func (api *API) PageDelete(w http.ResponseWriter, r *http.Request, ps httprouter } logger = logger.WithField("id", pageid) page := &Page{ - WebsiteID: websiteid, + WebsiteID: ctx.Value("websiteid").(int64), ID: pageid, } - if err := api.dbconnection.Unscoped().Delete(page).Error; err != nil { + if err := dbconnection.Unscoped().Delete(page).Error; err != nil { logger.Error("database: during delete website page") returnerr = &libapi.ErrorResult{Message: "Internal Database Error"} return diff --git a/modul/web/apipermission.go b/modul/web/apipermission.go index 23bd879..f5b2f11 100644 --- a/modul/web/apipermission.go +++ b/modul/web/apipermission.go @@ -4,39 +4,38 @@ import ( "net/http" "strconv" - "github.com/astaxie/session" - "github.com/julienschmidt/httprouter" + "goji.io/pat" + "golang.org/x/net/context" - libapi "dev.sum7.de/sum7/warehost/lib/api" - libsystem "dev.sum7.de/sum7/warehost/system" + libapi "dev.sum7.eu/sum7/warehost/lib/api" ) // PermissionList to add permissions -func (api *API) PermissionList(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) { +func permissionList(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { returndata = false - logger := api.log.GetLog(r, "permissionlist") + logger := log.GetLog(r, "permissionlist") var involved []*Manager - api.dbconnection.Where("website = ?", websiteid).Preload("Login").Find(&involved) + dbconnection.Where("website = ?", ctx.Value("websiteid").(int64)).Preload("Login").Find(&involved) logger.Info("done") returndata = involved return } // PermissionAdd to add permissions -func (api *API) PermissionAdd(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) { +func permissionAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { returndata = false - logger := api.log.GetLog(r, "permissionadd") - loginid, err := strconv.ParseInt(ps.ByName("loginid"), 10, 64) + logger := log.GetLog(r, "permissionadd") + loginid, err := strconv.ParseInt(pat.Param(ctx, "loginid"), 10, 64) if err != nil { returnerr = &libapi.ErrorResult{Fields: []string{"loginid"}, Message: "Not a valid loginid"} logger.Warn("invalid loginid, no integer") return } manager := &Manager{ - WebsiteID: websiteid, + WebsiteID: ctx.Value("websiteid").(int64), LoginID: loginid, } - if err := api.dbconnection.Create(manager).Error; err != nil { + if err := dbconnection.Create(manager).Error; err != nil { logger.Error("database: during create website permission") returnerr = &libapi.ErrorResult{Message: "Internal Database Error"} return @@ -47,20 +46,20 @@ func (api *API) PermissionAdd(w http.ResponseWriter, r *http.Request, ps httprou } // PermissionDelete to delete permissions -func (api *API) PermissionDelete(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *libsystem.Login, websiteid int64) (returndata interface{}, returnerr *libapi.ErrorResult) { +func permissionDelete(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { returndata = false - logger := api.log.GetLog(r, "permissiondelete") - loginid, err := strconv.ParseInt(ps.ByName("loginid"), 10, 64) + logger := log.GetLog(r, "permissiondelete") + loginid, err := strconv.ParseInt(pat.Param(ctx, "loginid"), 10, 64) if err != nil { returnerr = &libapi.ErrorResult{Fields: []string{"loginid"}, Message: "Not a valid loginid"} logger.Warn("invalid loginid, no integer") return } manager := &Manager{ - WebsiteID: websiteid, + WebsiteID: ctx.Value("websiteid").(int64), LoginID: loginid, } - if err := api.dbconnection.Unscoped().Delete(manager).Error; err != nil { + if err := dbconnection.Unscoped().Delete(manager).Error; err != nil { logger.Error("database: during delete website permission") returnerr = &libapi.ErrorResult{Message: "Internal Database Error"} return diff --git a/modul/web/lib.go b/modul/web/lib.go index b489a05..1df0898 100644 --- a/modul/web/lib.go +++ b/modul/web/lib.go @@ -5,46 +5,43 @@ import ( "strconv" "github.com/astaxie/session" - "github.com/jinzhu/gorm" - "github.com/julienschmidt/httprouter" + "goji.io/pat" + "golang.org/x/net/context" - libapi "dev.sum7.de/sum7/warehost/lib/api" - log "dev.sum7.de/sum7/warehost/lib/log" - libsystem "dev.sum7.de/sum7/warehost/system" + libapi "dev.sum7.eu/sum7/warehost/lib/api" + liblog "dev.sum7.eu/sum7/warehost/lib/log" + libsystem "dev.sum7.eu/sum7/warehost/system" ) -// Handle to handle request with session and current logged in user -type Handle func(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *libsystem.Login, id int64) (interface{}, *libapi.ErrorResult) - -//InvolveWebsiteHandler for api function to Verifie User ist loggedin -func InvolveWebsiteHandler(h Handle, sessions *session.Manager, dbconnection *gorm.DB) httprouter.Handle { - return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - sess := sessions.SessionStart(w, r) - err := &libapi.ErrorResult{Fields: []string{"session"}, Message: "Not logged in"} - var data interface{} - data = false +//InvolveWebsiteHandler for api function to Verifie User ist libloggedin +func InvolveWebsiteHandler(h libapi.Handle) libapi.Handle { + return func(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { + sess := ctx.Value("session").(session.Session) + returnerr = &libapi.ErrorResult{Fields: []string{"session"}, Message: "Not liblogged in"} + returndata = false if login := sess.Get("login"); login != nil { if loginObj := login.(libsystem.Login); loginObj.Active { - id, errI := strconv.ParseInt(ps.ByName("websiteid"), 10, 64) - if errI != nil { - err = &libapi.ErrorResult{Fields: []string{"websiteid"}, Message: "Not a valid websiteid"} - log.Log.Warn("invalid websiteid, no integer") - } else { + id, err := strconv.ParseInt(pat.Param(ctx, "websiteid"), 10, 64) + if err == nil { res := dbconnection.Where(map[string]int64{"website": id, "login": loginObj.ID}).Find(&Manager{}) if !res.RecordNotFound() { - data, err = h(w, r, ps, sess, &loginObj, id) - } else { - err = &libapi.ErrorResult{Fields: []string{"permission"}, Message: "No permission"} - log.Log.Info("no Permissions") + ctx = context.WithValue(ctx, "websiteid", id) + returndata, returnerr = h(ctx, w, r) + return } + returnerr = &libapi.ErrorResult{Fields: []string{"permission"}, Message: "No permission"} + liblog.Log.Info("no Permissions") + return } - } else { - log.Log.Warn("user not active") + returnerr = &libapi.ErrorResult{Fields: []string{"websiteid"}, Message: "Not a valid websiteid"} + liblog.Log.Warn("invalid websiteid, no integer") + return } - } else { - log.Log.Warn("not loggedin") + liblog.Log.Warn("user not active") + return } - libapi.JSONOutput(w, r, sess, data, err) + liblog.Log.Warn("not libloggedin") + return } } diff --git a/modul/web/models.go b/modul/web/models.go index 3d899f1..7d1fcf7 100644 --- a/modul/web/models.go +++ b/modul/web/models.go @@ -4,7 +4,7 @@ import ( "database/sql" "github.com/jinzhu/gorm" - system "dev.sum7.de/sum7/warehost/system" + system "dev.sum7.eu/sum7/warehost/system" ) // Website struct diff --git a/system/api.go b/system/api.go index d74f4fc..14ba287 100644 --- a/system/api.go +++ b/system/api.go @@ -7,56 +7,49 @@ import ( "strings" "time" - "github.com/astaxie/session" "github.com/jinzhu/gorm" - "github.com/julienschmidt/httprouter" + "goji.io" + "goji.io/pat" + "golang.org/x/net/context" - libconfig "dev.sum7.de/sum7/warehost/config" - libapi "dev.sum7.de/sum7/warehost/lib/api" - log "dev.sum7.de/sum7/warehost/lib/log" - libpassword "dev.sum7.de/sum7/warehost/lib/password" + libapi "dev.sum7.eu/sum7/warehost/lib/api" + liblog "dev.sum7.eu/sum7/warehost/lib/log" + libpassword "dev.sum7.eu/sum7/warehost/lib/password" + libsession "dev.sum7.eu/sum7/warehost/lib/session" ) //MODULNAME to get global name for the modul const MODULNAME = "system" -//API keep data in module global -type API struct { - config *libconfig.Config - sessions *session.Manager - dbconnection *gorm.DB - log *log.ModulLog -} +var dbconnection *gorm.DB +var log *liblog.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)) - router.POST(prefix+"/login", libapi.SessionHandler(api.Login, sessions)) - router.GET(prefix+"/logout", LoginHandler(api.Logout, sessions)) - router.POST(prefix+"/password", LoginHandler(api.Password, sessions)) - router.GET(prefix+"/delete", LoginHandler(api.Delete, sessions)) - router.GET(prefix+"/invite", LoginHandler(api.InviteList, sessions)) - router.POST(prefix+"/invite", LoginHandler(api.InviteAdd, sessions)) - router.GET(prefix+"/user", LoginHandler(api.LoginList, sessions)) - router.POST(prefix+"/user", LoginHandler(api.LoginAdd, sessions)) - router.PUT(prefix+"/user/:id", LoginHandler(api.LoginEdit, sessions)) - router.DELETE(prefix+"/user/:id", LoginHandler(api.LoginDelete, sessions)) - router.GET(prefix+"/invitor", LoginHandler(api.Invitor, sessions)) - router.PUT(prefix+"/invitor", LoginHandler(api.InvitorAdminToggle, sessions)) +// BindAPI sets the routes to the api functions +func BindAPI(db *gorm.DB, router *goji.Mux, prefix string) { + dbconnection = db + log = liblog.NewModulLog(MODULNAME) + + router.HandleFuncC(pat.Get(prefix+"/status"), libapi.SessionHandler(status)) + router.HandleFuncC(pat.Post(prefix+"/login"), libapi.SessionHandler(login)) + router.HandleFuncC(pat.Get(prefix+"/logout"), libapi.SessionHandler(LoginHandler(logout))) + router.HandleFuncC(pat.Post(prefix+"/password"), libapi.SessionHandler(LoginHandler(password))) + router.HandleFuncC(pat.Get(prefix+"/delete"), libapi.SessionHandler(LoginHandler(delete))) + router.HandleFuncC(pat.Get(prefix+"/invite"), libapi.SessionHandler(LoginHandler(inviteList))) + router.HandleFuncC(pat.Post(prefix+"/invite"), libapi.SessionHandler(LoginHandler(inviteAdd))) + router.HandleFuncC(pat.Get(prefix+"/user"), libapi.SessionHandler(LoginHandler(loginList))) + router.HandleFuncC(pat.Post(prefix+"/user"), libapi.SessionHandler(LoginHandler(loginAdd))) + router.HandleFuncC(pat.Put(prefix+"/user/:id"), libapi.SessionHandler(LoginHandler(loginEdit))) + router.HandleFuncC(pat.Delete(prefix+"/user/:id"), libapi.SessionHandler(LoginHandler(loginDelete))) + router.HandleFuncC(pat.Get(prefix+"/invitor"), libapi.SessionHandler(LoginHandler(invitor))) + router.HandleFuncC(pat.Put(prefix+"/invitor"), libapi.SessionHandler(LoginHandler(invitorAdminToggle))) } // 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) { +func status(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { returndata = false - logger := api.log.GetLog(r, "status") + logger := log.GetLog(r, "status") var result int64 - api.dbconnection.Model(&Login{}).Count(&result) + dbconnection.Model(&Login{}).Count(&result) if result > 0 { returndata = true } @@ -65,9 +58,10 @@ func (api *API) Status(w http.ResponseWriter, r *http.Request, _ httprouter.Para } // Logout current user -func (api *API) Logout(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session, _ *Login) (returndata interface{}, returnerr *libapi.ErrorResult) { - api.sessions.SessionDestroy(w, r) - logger := api.log.GetLog(r, "logout") +func logout(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { + sess := ctx.Value("session").(libsession.Session) + libsession.SessionDestroy(w, r) + logger := log.GetLog(r, "logout") if login := sess.Get("login"); login != nil { logger = logger.WithField("user", login.(Login).Username) } @@ -80,9 +74,10 @@ func (api *API) Logout(w http.ResponseWriter, r *http.Request, _ httprouter.Para } // Login of system -func (api *API) Login(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session) (returndata interface{}, returnerr *libapi.ErrorResult) { +func login(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { + sess := ctx.Value("session").(libsession.Session) returndata = false - logger := api.log.GetLog(r, "login") + logger := log.GetLog(r, "login") var requestlogin RequestLogin err := json.NewDecoder(r.Body).Decode(&requestlogin) if err != nil { @@ -92,7 +87,7 @@ func (api *API) Login(w http.ResponseWriter, r *http.Request, _ httprouter.Param } logger = logger.WithField("user", requestlogin.Username) var login = Login{Username: requestlogin.Username} - api.dbconnection.Where("mail = ?", requestlogin.Username).First(&login) + dbconnection.Where("mail = ?", requestlogin.Username).First(&login) if login.ID <= 0 { logger.Warn("user not found") returnerr = &libapi.ErrorResult{Fields: []string{"username"}, Message: "User not Found"} @@ -102,7 +97,7 @@ func (api *API) Login(w http.ResponseWriter, r *http.Request, _ httprouter.Param output, _ := libpassword.Validate(login.Password, requestlogin.Password) if output { returndata = true - api.dbconnection.Model(&login).Update("LastLoginAt", time.Now()) + dbconnection.Model(&login).Update("LastLoginAt", time.Now()) sess.Set("login", login) logger.Info("done") } else { @@ -117,9 +112,11 @@ func (api *API) Login(w http.ResponseWriter, r *http.Request, _ httprouter.Param } //Password to change the password -func (api *API) Password(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session, login *Login) (returndata interface{}, returnerr *libapi.ErrorResult) { +func password(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { + sess := ctx.Value("session").(libsession.Session) + login := ctx.Value("login").(*Login) returndata = false - logger := api.log.GetLog(r, "password") + logger := log.GetLog(r, "password") var changePasswordRequest ChangePasswordRequest err := json.NewDecoder(r.Body).Decode(&changePasswordRequest) @@ -142,7 +139,7 @@ func (api *API) Password(w http.ResponseWriter, r *http.Request, _ httprouter.Pa return } login.Password = libpassword.NewHash(changePasswordRequest.NewPassword) - if err := api.dbconnection.Save(login).Error; err != nil { + if err := dbconnection.Save(login).Error; err != nil { logger.Warn("error save new password to database") returnerr = &libapi.ErrorResult{Message: "Error save new password"} return @@ -154,11 +151,13 @@ func (api *API) Password(w http.ResponseWriter, r *http.Request, _ httprouter.Pa } //Delete of login on warehost -func (api *API) Delete(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session, login *Login) (returndata interface{}, returnerr *libapi.ErrorResult) { +func delete(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { + sess := ctx.Value("session").(libsession.Session) + login := ctx.Value("login").(*Login) returndata = false - logger := api.log.GetLog(r, "delete") + logger := log.GetLog(r, "delete") sess.Delete("login") - if err := api.dbconnection.Unscoped().Delete(login).Error; err != nil { + if err := dbconnection.Unscoped().Delete(login).Error; err != nil { logger.Warn("error detete login") returnerr = &libapi.ErrorResult{Message: "Error delete login"} return @@ -169,10 +168,11 @@ func (api *API) Delete(w http.ResponseWriter, r *http.Request, _ httprouter.Para } // InviteList list all of your invites -func (api *API) InviteList(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session, login *Login) (returndata interface{}, returnerr *libapi.ErrorResult) { +func inviteList(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { + login := ctx.Value("login").(*Login) returndata = false - logger := api.log.GetLog(r, "invitelist") - if err := api.dbconnection.Model(login).Preload("Invites.Invited").First(login).Error; err != nil { + logger := log.GetLog(r, "invitelist") + if err := dbconnection.Model(login).Preload("Invites.Invited").First(login).Error; err != nil { logger.Warn("error load own invites") returnerr = &libapi.ErrorResult{Message: "Could not load invites!"} return @@ -183,9 +183,10 @@ func (api *API) InviteList(w http.ResponseWriter, r *http.Request, _ httprouter. } // InviteAdd invite a new user to warehost -func (api *API) InviteAdd(w http.ResponseWriter, r *http.Request, _ httprouter.Params, sess session.Session, login *Login) (returndata interface{}, returnerr *libapi.ErrorResult) { +func inviteAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { + login := ctx.Value("login").(*Login) returndata = false - logger := api.log.GetLog(r, "inviteadd") + logger := log.GetLog(r, "inviteadd") var newLogin RequestLogin err := json.NewDecoder(r.Body).Decode(&newLogin) if err != nil { @@ -202,7 +203,7 @@ func (api *API) InviteAdd(w http.ResponseWriter, r *http.Request, _ httprouter.P Active: true, }, } - if err := api.dbconnection.Create(invite).Error; err != nil { + if err := dbconnection.Create(invite).Error; err != nil { logger.Warn("error create invite") returnerr = &libapi.ErrorResult{Message: "Username exists already"} return @@ -213,15 +214,16 @@ func (api *API) InviteAdd(w http.ResponseWriter, r *http.Request, _ httprouter.P } // LoginList list all users in system -func (api *API) LoginList(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *Login) (returndata interface{}, returnerr *libapi.ErrorResult) { +func loginList(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { + login := ctx.Value("login").(*Login) returndata = false - logger := api.log.GetLog(r, "loginlist") + logger := log.GetLog(r, "loginlist") var logins []Login selectfield := "ID, mail" if login.Superadmin { selectfield = "ID, mail, superadmin" } - if err := api.dbconnection.Select(selectfield).Find(&logins).Error; err != nil { + if err := dbconnection.Select(selectfield).Find(&logins).Error; err != nil { logger.Warn("sql edit login") returnerr = &libapi.ErrorResult{Message: "Error during edit login"} return @@ -232,9 +234,10 @@ func (api *API) LoginList(w http.ResponseWriter, r *http.Request, ps httprouter. } // LoginAdd add a new Login -func (api *API) LoginAdd(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *Login) (returndata interface{}, returnerr *libapi.ErrorResult) { +func loginAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { + login := ctx.Value("login").(*Login) returndata = false - logger := api.log.GetLog(r, "loginadd") + logger := log.GetLog(r, "loginadd") if !login.Superadmin { logger.Error("no superadmin") returnerr = &libapi.ErrorResult{Message: "Error no permission to edit this invite"} @@ -253,7 +256,7 @@ func (api *API) LoginAdd(w http.ResponseWriter, r *http.Request, ps httprouter.P Password: libpassword.NewHash(newLogin.Password), Active: true, } - if err := api.dbconnection.Create(loginObj).Error; err != nil { + if err := dbconnection.Create(loginObj).Error; err != nil { logger.Warn("error create login") returnerr = &libapi.ErrorResult{Message: "Username exists already"} return @@ -264,10 +267,11 @@ func (api *API) LoginAdd(w http.ResponseWriter, r *http.Request, ps httprouter.P } // LoginEdit edit a login by invite or superadmin -func (api *API) LoginEdit(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *Login) (returndata interface{}, returnerr *libapi.ErrorResult) { +func loginEdit(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { + login := ctx.Value("login").(*Login) returndata = false - logger := api.log.GetLog(r, "loginedit") - id, err := strconv.ParseInt(ps.ByName("id"), 10, 64) + logger := log.GetLog(r, "loginedit") + id, err := strconv.ParseInt(pat.Param(ctx, "id"), 10, 64) if err != nil { returnerr = &libapi.ErrorResult{Message: "Error invalid input"} logger.Warn("invalid userinput, no integer") @@ -284,8 +288,8 @@ func (api *API) LoginEdit(w http.ResponseWriter, r *http.Request, ps httprouter. return } - api.dbconnection.Where("id = ?", invitedLogin.ID).First(&invitedLogin) - invite := invitedLogin.GetInvitedby(api.dbconnection) + dbconnection.Where("id = ?", invitedLogin.ID).First(&invitedLogin) + invite := invitedLogin.GetInvitedby(dbconnection) if !login.Superadmin && !invite.Admin && invitedLogin.CreateAt.Before(invitedLogin.LastLoginAt) { logger.Warn("no permission") returnerr = &libapi.ErrorResult{Message: "Error no permission to edit this login"} @@ -298,7 +302,7 @@ func (api *API) LoginEdit(w http.ResponseWriter, r *http.Request, ps httprouter. invitedLogin.Username = changeLogin.Username invitedLogin.Superadmin = changeLogin.Superadmin } - if err := api.dbconnection.Save(invitedLogin).Error; err != nil { + if err := dbconnection.Save(invitedLogin).Error; err != nil { logger.Warn("sql edit login") returnerr = &libapi.ErrorResult{Message: "Error during edit login"} return @@ -309,10 +313,11 @@ func (api *API) LoginEdit(w http.ResponseWriter, r *http.Request, ps httprouter. } // LoginDelete delete a login by invite or superadmin -func (api *API) LoginDelete(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *Login) (returndata interface{}, returnerr *libapi.ErrorResult) { +func loginDelete(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { + login := ctx.Value("login").(*Login) returndata = false - logger := api.log.GetLog(r, "logindelete") - id, err := strconv.ParseInt(ps.ByName("id"), 10, 64) + logger := log.GetLog(r, "logindelete") + id, err := strconv.ParseInt(pat.Param(ctx, "id"), 10, 64) if err != nil { returnerr = &libapi.ErrorResult{Message: "Error invalid input"} logger.Warn("invalid userinput, no integer") @@ -320,14 +325,14 @@ func (api *API) LoginDelete(w http.ResponseWriter, r *http.Request, ps httproute } logger = logger.WithField("id", id) var invitedLogin = Login{ID: id} - api.dbconnection.Where("id = ?", invitedLogin.ID).First(&invitedLogin) - invite := invitedLogin.GetInvitedby(api.dbconnection) + dbconnection.Where("id = ?", invitedLogin.ID).First(&invitedLogin) + invite := invitedLogin.GetInvitedby(dbconnection) if !login.Superadmin && !invite.Admin && invitedLogin.CreateAt.Before(invitedLogin.LastLoginAt) { logger.Warn("no permission") returnerr = &libapi.ErrorResult{Message: "Error no permission to delete this login"} return } - if err := api.dbconnection.Unscoped().Delete(invitedLogin).Error; err != nil { + if err := dbconnection.Unscoped().Delete(invitedLogin).Error; err != nil { logger.Warn("sql detete login") returnerr = &libapi.ErrorResult{Message: "Error during delete login"} return @@ -338,22 +343,24 @@ func (api *API) LoginDelete(w http.ResponseWriter, r *http.Request, ps httproute } // Invitor get Invite of current login -func (api *API) Invitor(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *Login) (returndata interface{}, returnerr *libapi.ErrorResult) { +func invitor(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { + login := ctx.Value("login").(*Login) returndata = false - logger := api.log.GetLog(r, "invitor") - invite := login.GetInvitedby(api.dbconnection) + logger := log.GetLog(r, "invitor") + invite := login.GetInvitedby(dbconnection) logger.Info("done") returndata = invite return } // InvitorAdminToggle toggle admin of current login -func (api *API) InvitorAdminToggle(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *Login) (returndata interface{}, returnerr *libapi.ErrorResult) { +func invitorAdminToggle(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { + login := ctx.Value("login").(*Login) returndata = false - logger := api.log.GetLog(r, "invitoradmintoggle") - invite := login.GetInvitedby(api.dbconnection) + logger := log.GetLog(r, "invitoradmintoggle") + invite := login.GetInvitedby(dbconnection) invite.Admin = !invite.Admin - api.dbconnection.Model(invite).Save(&invite) + dbconnection.Model(invite).Save(&invite) logger.Info("done") returndata = true return diff --git a/system/api_test.go b/system/api_test.go index 9ac7e99..192e375 100644 --- a/system/api_test.go +++ b/system/api_test.go @@ -4,7 +4,7 @@ import ( "net/http" "testing" - "dev.sum7.de/sum7/warehost/test" + "dev.sum7.eu/sum7/warehost/test" ) func TestAPI(t *testing.T) { @@ -16,7 +16,7 @@ func TestAPI(t *testing.T) { SyncModels(db) // Bind API - NewAPI(config, sessions, db, router, "") + BindAPI(db, router, "") session := test.NewSession(router) // Test status diff --git a/system/lib.go b/system/lib.go index cc1a547..73b4e68 100644 --- a/system/lib.go +++ b/system/lib.go @@ -4,32 +4,29 @@ import ( "net/http" "github.com/astaxie/session" - "github.com/julienschmidt/httprouter" + "golang.org/x/net/context" - libapi "dev.sum7.de/sum7/warehost/lib/api" - log "dev.sum7.de/sum7/warehost/lib/log" + libapi "dev.sum7.eu/sum7/warehost/lib/api" + liblog "dev.sum7.eu/sum7/warehost/lib/log" ) -// Handle to handle request with session and current logged in user -type Handle func(w http.ResponseWriter, r *http.Request, ps httprouter.Params, sess session.Session, login *Login) (interface{}, *libapi.ErrorResult) - //LoginHandler for api function to Verifie User ist loggedin -func LoginHandler(h Handle, sessions *session.Manager) httprouter.Handle { - return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - sess := sessions.SessionStart(w, r) - err := &libapi.ErrorResult{Fields: []string{"session"}, Message: "Not logged in"} - var data interface{} - data = false +func LoginHandler(h libapi.Handle) libapi.Handle { + return func(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { + sess := ctx.Value("session").(session.Session) + returndata = false if login := sess.Get("login"); login != nil { if loginObj := login.(Login); loginObj.Active { - data, err = h(w, r, ps, sess, &loginObj) - } else { - log.Log.Warn("user not active") + ctx = context.WithValue(ctx, "login", &loginObj) + returndata, returnerr = h(ctx, w, r) } - } else { - log.Log.Warn("not loggedin") + returnerr = &libapi.ErrorResult{Fields: []string{"session"}, Message: "Not active user"} + liblog.Log.Warn("user not active") + return } - libapi.JSONOutput(w, r, sess, data, err) + returnerr = &libapi.ErrorResult{Fields: []string{"session"}, Message: "Not logged in"} + liblog.Log.Warn("not loggedin") + return } } diff --git a/system/models.go b/system/models.go index 1892911..9ebf4df 100644 --- a/system/models.go +++ b/system/models.go @@ -3,10 +3,10 @@ package system import ( "time" - log "dev.sum7.de/sum7/warehost/lib/log" - libpassword "dev.sum7.de/sum7/warehost/lib/password" - "github.com/jinzhu/gorm" + + liblog "dev.sum7.eu/sum7/warehost/lib/log" + libpassword "dev.sum7.eu/sum7/warehost/lib/password" ) // MINPASSWORDLENTH to validate password @@ -77,8 +77,8 @@ func SyncModels(dbconnection *gorm.DB) { } dbconnection.Create(login) - log.Log.Error("have to create \"login\"") + liblog.Log.Error("have to create \"login\"") } else { - log.Log.Info("Connection to \"login\" works") + liblog.Log.Info("Connection to \"login\" works") } } diff --git a/test/main.go b/test/main.go index 9015fc3..7d9ae14 100644 --- a/test/main.go +++ b/test/main.go @@ -11,16 +11,16 @@ import ( _ "github.com/astaxie/session/providers/memory" "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/postgres" - "github.com/julienschmidt/httprouter" "github.com/stretchr/testify/assert" + "goji.io" - libconfig "dev.sum7.de/sum7/warehost/config" - libapi "dev.sum7.de/sum7/warehost/lib/api" - log "dev.sum7.de/sum7/warehost/lib/log" + libconfig "dev.sum7.eu/sum7/warehost/config" + libapi "dev.sum7.eu/sum7/warehost/lib/api" + log "dev.sum7.eu/sum7/warehost/lib/log" ) //Init to initialisieren a API -func Init(t *testing.T) (assertion *assert.Assertions, config *libconfig.Config, sessions *session.Manager, dbconnection *gorm.DB, router *httprouter.Router) { +func Init(t *testing.T) (assertion *assert.Assertions, config *libconfig.Config, sessions *session.Manager, dbconnection *gorm.DB, router *goji.Mux) { assertion = assert.New(t) config = libconfig.ReadConfigFile("../cmd/warehost/config.yml.example") log.NewLogger(config.Log.Path) @@ -39,7 +39,7 @@ func Init(t *testing.T) (assertion *assert.Assertions, config *libconfig.Config, dbconnection.SingularTable(true) dbconnection.LogMode(config.DatabaseDebug) - router = httprouter.New() + router = goji.NewMux() return } @@ -47,11 +47,11 @@ func Init(t *testing.T) (assertion *assert.Assertions, config *libconfig.Config, type Request struct { req *http.Request cookies []*http.Cookie - router *httprouter.Router + router *goji.Mux } // NewSession to get a new easy manager -func NewSession(router *httprouter.Router) *Request { +func NewSession(router *goji.Mux) *Request { return &Request{router: router} }