main.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package main
  2. import (
  3. "github.com/kataras/iris"
  4. "github.com/kataras/go-template/html"
  5. "fmt"
  6. "git.mmnx.de/Moe/databaseutils"
  7. "git.mmnx.de/Moe/usermanager"
  8. "git.mmnx.de/Moe/configutils"
  9. )
  10. type pageParams struct{
  11. HasError string
  12. Error string
  13. ReqDir string
  14. } // {Error: ""}
  15. type pageUserParams struct{
  16. HasError string
  17. Error string
  18. ReqDir string
  19. Username string
  20. Email string
  21. } // {Error: ""}
  22. func main() {
  23. conf := configutils.ReadConfig("config.json") // read config
  24. configutils.Conf = &conf // store conf globally accessible
  25. databaseutils.DBUtil = &databaseutils.DBUtils{configutils.Conf.DBUser, configutils.Conf.DBPass, configutils.Conf.DBHost, configutils.Conf.DBName, nil} // init dbutils
  26. databaseutils.DBUtil.Connect() // connect to db
  27. users := make([]usermanager.User, 0) // users list
  28. usermanager.Users = &users // store globally accessible
  29. fmt.Print("") // for not needing to remove fmt ...
  30. iris.Config.IsDevelopment = true
  31. //iris.Config.Render.Template.Gzip = true
  32. /** HELPER FUNCTION EXAMPLE **/
  33. /*config := html.DefaultConfig()
  34. config.Layout = "layouts/main.html"
  35. config.Helpers["boldme"] = func(input string) raymond.SafeString {
  36. return raymond.SafeString("<b> " + input + "</b>")
  37. }*/
  38. /** ROUTING **/
  39. iris.Static("/js", "./static/js", 1)
  40. iris.Static("/css", "./static/css", 1)
  41. iris.Static("/img", "./static/img", 1)
  42. iris.Post("/login", loginHandler) // login form handler
  43. iris.Get("/", usermanager.AuthHandler, func(ctx *iris.Context) {
  44. params := pageParams{"0", "", ""}
  45. if err := ctx.Render("home_boxes.html", params); err != nil {
  46. println(err.Error())
  47. }
  48. })
  49. iris.Get("/account", usermanager.AuthHandler, func(ctx *iris.Context) {
  50. params := pageUserParams{"0", "", "account", "moe", "moritz@mmnx.de"}
  51. if err := ctx.Render("account_box.html", params); err != nil {
  52. println(err.Error())
  53. }
  54. })
  55. iris.Get("/login", func(ctx *iris.Context) {
  56. params := pageParams{"0", "", "login"}
  57. if err := ctx.Render("login.html", params); err != nil { // no error for normal login screen, struct{ Error string }{Error: ""}
  58. println(err.Error())
  59. }
  60. })
  61. iris.Get("/test", usermanager.AuthHandler, testHandler)
  62. // remove the layout for a specific route
  63. iris.Get("/nolayout", func(ctx *iris.Context) {
  64. if err := ctx.Render("page1.html", nil, iris.RenderOptions{"layout": iris.NoLayout}); err != nil {
  65. println(err.Error())
  66. }
  67. })
  68. iris.UseTemplate(html.New(html.Config{
  69. Layout: "layouts/main.html",
  70. }))
  71. /** OTHER **/
  72. iris.Listen(":8080")
  73. }
  74. func loginHandler(ctx *iris.Context) {
  75. username := ctx.FormValueString("username") // POST values
  76. password := ctx.FormValueString("password")
  77. user := usermanager.User{} // new user
  78. tokenString, err := user.Login(username, password) // try to login
  79. if err != nil { // TODO: template compatible error handling
  80. ctx.Render("login.html", pageParams{"1", err.Error(), "login"})
  81. } else {
  82. ctx.SetCookieKV("token", tokenString)
  83. ctx.Redirect("/")
  84. //ctx.Render("home.html", nil) // TODO: error-alternative success (main.html)
  85. }
  86. }
  87. func testHandler(ctx *iris.Context) {
  88. userID := ctx.Get("userID")
  89. ctx.Write("Test %d", userID);
  90. }