| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- package main
- import (
- "github.com/kataras/iris"
- "github.com/kataras/go-template/html"
- "fmt"
- "git.mmnx.de/Moe/databaseutils"
- "git.mmnx.de/Moe/usermanager"
- "git.mmnx.de/Moe/configutils"
- )
- type pageParams struct{
- HasError string
- Error string
- ReqDir string
- IsAdmin string
- } // {Error: ""}
- type pageUserParams struct{
- HasError string
- Error string
- ReqDir string
- Username string
- Email string
- IsAdmin string
- } // {Error: ""}
- 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("<b> " + input + "</b>")
- }*/
- /** ROUTING **/
- iris.Static("/js", "./static/js", 1)
- iris.Static("/css", "./static/css", 1)
- iris.Static("/img", "./static/img", 1)
- iris.Post("/login", loginHandler) // login form handler
- iris.Get("/", usermanager.AuthHandler, func(ctx *iris.Context) {
- params := pageParams{"0", "", "", "0"}
- if err := ctx.Render("home_boxes.html", params); err != nil {
- println(err.Error())
- }
- })
- iris.Get("/account", usermanager.AuthHandler, func(ctx *iris.Context) {
- params := pageUserParams{"0", "", "account", "moe", "moritz@mmnx.de", "0"}
- if err := ctx.Render("account_box.html", params); err != nil {
- println(err.Error())
- }
- })
- iris.Get("/login", func(ctx *iris.Context) {
- params := pageParams{"0", "", "login", "0"}
- if err := ctx.Render("login.html", params); err != nil { // no error for normal login screen, struct{ Error string }{Error: ""}
- 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())
- }
- })
- iris.UseTemplate(html.New(html.Config{
- Layout: "layouts/main.html",
- }))
- /** 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", pageParams{"1", err.Error(), "login", "0"})
- } else {
- ctx.SetCookieKV("token", tokenString)
- ctx.Redirect("/")
- //ctx.Render("home.html", nil) // TODO: error-alternative success (main.html)
- }
- }
- func testHandler(ctx *iris.Context) {
- userID := ctx.Get("userID")
- ctx.Write("Test %d", userID);
- }
|