package main import ( "github.com/kataras/iris" "git.mmnx.de/Moe/go-template/html" "fmt" "git.mmnx.de/Moe/databaseutils" "git.mmnx.de/Moe/usermanager" "git.mmnx.de/Moe/configutils" ) 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 /** ROUTING **/ iris.Static("/js", "./static/js", 1) iris.Static("/css", "./static/css", 1) iris.Static("/img", "./static/img", 1) //iris.Get("/test", usermanager.AuthHandler, testHandler) iris.Post("/login", loginHandler) // login form handler iris.UseTemplate(html.New(html.Config{ Layout: "layouts/main.html", })) /*config := html.DefaultConfig() config.Layout = "layouts/main.html" config.Helpers["boldme"] = func(input string) raymond.SafeString { return raymond.SafeString(" " + input + "") } // set the template engine iris.UseTemplate(handlebars.New(config)).Directory("./templates", ".html")*/ //iris.Config.Render.Template.Gzip = true iris.Get("/", usermanager.AuthHandler, func(ctx *iris.Context) { if err := ctx.Render("notification_box.html", nil); err != nil { println(err.Error()) } if err := ctx.Render("home_boxes.html", nil); err != nil { println(err.Error()) } }) iris.Get("/account", usermanager.AuthHandler, func(ctx *iris.Context) { if err := ctx.Render("notification_box.html", nil); err != nil { println(err.Error()) } if err := ctx.Render("account_box.html", nil); err != nil { println(err.Error()) } }) iris.Get("/login", func(ctx *iris.Context) { if err := ctx.Render("notification_box.html", struct{ Error string }{Error: ""}); err != nil { // TODO: do this only when there is a notification println(err.Error()) } if err := ctx.Render("login.html", struct{ Error string }{Error: ""}); err != nil { // no error for normal login screen println(err.Error()) } }) iris.Get("/test", usermanager.AuthHandler, testHandler) // remove the layout for a specific route iris.Get("/nolayout", func(ctx *iris.Context) { if err := ctx.Render("page1.html", nil, iris.RenderOptions{"layout": iris.NoLayout}); err != nil { println(err.Error()) } }) /** OTHER **/ iris.Listen(":8080") } func loginHandler(ctx *iris.Context) { username := ctx.FormValueString("username") // POST values password := ctx.FormValueString("password") user := usermanager.User{} // new user tokenString, err := user.Login(username, password) // try to login if err != nil { // TODO: template compatible error handling ctx.Render("login.html", struct{ Error string }{Error: err.Error()}) } else { ctx.SetCookieKV("token", tokenString) ctx.Redirect("/") //ctx.Render("home.html", nil) } } func testHandler(ctx *iris.Context) { userID := ctx.Get("userID") ctx.Write("Test %d", userID); }