package main import ( "github.com/kataras/iris" "github.com/kataras/go-template/html" "fmt" "errors" "git.mmnx.de/Moe/usermanager" "git.mmnx.de/Moe/databaseutils" "git.mmnx.de/Moe/configutils" "git.mmnx.de/Moe/errorhelpers" "git.mmnx.de/Moe/templatehelpers" ) func main() { conf := configutils.ReadConfig("config.json") // read config configutils.Conf = &conf // store conf globally accessible databaseutils.DBUtil = &databaseutils.DBUtils{configutils.Conf.DBUser, configutils.Conf.DBPass, configutils.Conf.DBHost, configutils.Conf.DBName, nil} // init dbutils databaseutils.DBUtil.Connect() // connect to db users := make([]usermanager.User, 0) // users list usermanager.Users = &users // store globally accessible fmt.Print("") // for not needing to remove fmt ... iris.Config.IsDevelopment = true //iris.Config.Render.Template.Gzip = true /** HELPER FUNCTION EXAMPLE **/ /*config := html.DefaultConfig() config.Layout = "layouts/main.html" config.Helpers["boldme"] = func(input string) raymond.SafeString { return raymond.SafeString(" " + input + "") }*/ /** ROUTING **/ iris.UseTemplate(html.New(html.Config{ // main layout for all pages (like a wrapper for boxes we register downwards) Layout: "layouts/main.html", })) iris.UseFunc(templatehelpers.InitPageParams) // dynamic page params, initialization iris.Static("/js", "./static/js", 1) // make js files in static/js available via /js iris.Static("/css", "./static/css", 1) iris.Static("/img", "./static/img", 1) iris.Static("/static", "./static/static", 1) iris.Post("/login", loginHandler, usermanager.AuthHandler) // login form handler iris.Post("/register", usermanager.CanBeAuthedHandler, usermanager.RegisterHandler, usermanager.LogoutHandler) // handles registration, logs user out iris.Post("/account", usermanager.AuthHandler, accountUpdateHandler, usermanager.LogoutHandler) // account management iris.Post("/admin", usermanager.AuthHandler, usermanager.AdminHandler, adminPostHandler) // admin panel iris.Get("/login", templateHandler) // TODO not when logged in iris.Get("/logout", usermanager.AuthHandler, usermanager.LogoutHandler) iris.Get("/register", templateHandler) iris.Get("/", usermanager.AuthHandler, templateHandler) iris.Get("/account", usermanager.AuthHandler, templateHandler) iris.Get("/help", usermanager.AuthHandler, templateHandler) iris.Get("/admin", usermanager.AuthHandler, usermanager.AdminHandler, templateHandler) /** OTHER **/ iris.Listen(":8080") } func loginHandler(ctx *iris.Context) { // TODO outsource? username := ctx.FormValueString("username") // POST values from login form password := ctx.FormValueString("password") user := usermanager.User{} // new user tokenString, err := user.Login(username, password) // try to login ctx.SetCookieKV("token", tokenString) if err != nil { errorhelpers.HandleError(err, ctx) } else { templatehelpers.UpdatePageParam(ctx, "notification", errorhelpers.SUCCESS_LOGIN) // TODO this for TODO down ? ctx.Next() } } func accountUpdateHandler(ctx *iris.Context) { username := ctx.FormValueString("username") // POST values password := ctx.FormValueString("password") userID := ctx.GetString("userID") err := usermanager.UserUpdateProcessor(username, password, userID) if err != nil { // TODO handle err nil stuff somewhere errorhelpers.HandleError(err, ctx) } else { err = errors.New(errorhelpers.SUCCESS_UPDATE) errorhelpers.HandleError(err, ctx) } } func adminPostHandler(ctx *iris.Context) { _, err := usermanager.GenerateTokens(5) // generate tokens and store in db, we don't need them now, TODO error handling? if err != nil { errorhelpers.HandleError(err, ctx) } else { err = errors.New(errorhelpers.SUCCESS_TOKENS_GENERATED) errorhelpers.HandleError(err, ctx) } } func templateHandler(ctx *iris.Context) { params := ctx.Get("params").(map[string]string) switch params["reqDir"] { case "admin": tokens := usermanager.GetTokensAsString(false) params["tokens"] = tokens ctx.Set("params", params) } ctx.MustRender(params["reqDir"] + "_box.html", params); }