main.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package main
  2. import (
  3. "github.com/kataras/iris"
  4. "github.com/kataras/go-template/html"
  5. "fmt"
  6. "errors"
  7. "git.mmnx.de/Moe/usermanager"
  8. "git.mmnx.de/Moe/databaseutils"
  9. "git.mmnx.de/Moe/configutils"
  10. "git.mmnx.de/Moe/errorhelpers"
  11. "git.mmnx.de/Moe/templatehelpers"
  12. )
  13. func main() {
  14. conf := configutils.ReadConfig("config.json") // read config
  15. configutils.Conf = &conf // store conf globally accessible
  16. databaseutils.DBUtil = &databaseutils.DBUtils{configutils.Conf.DBUser, configutils.Conf.DBPass, configutils.Conf.DBHost, configutils.Conf.DBName, nil} // init dbutils
  17. databaseutils.DBUtil.Connect() // connect to db
  18. users := make([]usermanager.User, 0) // users list
  19. usermanager.Users = &users // store globally accessible
  20. fmt.Print("") // for not needing to remove fmt ...
  21. iris.Config.IsDevelopment = true
  22. //iris.Config.Render.Template.Gzip = true
  23. /** HELPER FUNCTION EXAMPLE **/
  24. /*config := html.DefaultConfig()
  25. config.Layout = "layouts/main.html"
  26. config.Helpers["boldme"] = func(input string) raymond.SafeString {
  27. return raymond.SafeString("<b> " + input + "</b>")
  28. }*/
  29. /** ROUTING **/
  30. iris.UseTemplate(html.New(html.Config{ // main layout for all pages (like a wrapper for boxes we register downwards)
  31. Layout: "layouts/main.html",
  32. }))
  33. iris.UseFunc(templatehelpers.InitPageParams) // dynamic page params, initialization
  34. iris.Static("/js", "./static/js", 1) // make js files in static/js available via /js
  35. iris.Static("/css", "./static/css", 1)
  36. iris.Static("/img", "./static/img", 1)
  37. iris.Static("/static", "./static/static", 1)
  38. iris.Post("/login", loginHandler, usermanager.AuthHandler) // login form handler
  39. iris.Post("/register", usermanager.CanBeAuthedHandler, usermanager.RegisterHandler, usermanager.LogoutHandler) // handles registration, logs user out
  40. iris.Post("/account", usermanager.AuthHandler, accountUpdateHandler, usermanager.LogoutHandler) // account management
  41. iris.Post("/admin", usermanager.AuthHandler, usermanager.AdminHandler, adminPostHandler) // admin panel
  42. iris.Get("/login", templateHandler) // TODO not when logged in
  43. iris.Get("/logout", usermanager.AuthHandler, usermanager.LogoutHandler)
  44. iris.Get("/register", templateHandler)
  45. iris.Get("/", usermanager.AuthHandler, templateHandler)
  46. iris.Get("/account", usermanager.AuthHandler, templateHandler)
  47. iris.Get("/help", usermanager.AuthHandler, templateHandler)
  48. iris.Get("/admin", usermanager.AuthHandler, usermanager.AdminHandler, templateHandler)
  49. /** OTHER **/
  50. iris.Listen(":8080")
  51. }
  52. func loginHandler(ctx *iris.Context) { // TODO outsource?
  53. username := ctx.FormValueString("username") // POST values from login form
  54. password := ctx.FormValueString("password")
  55. user := usermanager.User{} // new user
  56. tokenString, err := user.Login(username, password) // try to login
  57. ctx.SetCookieKV("token", tokenString)
  58. if err != nil {
  59. errorhelpers.HandleError(err, ctx)
  60. } else {
  61. templatehelpers.UpdatePageParam(ctx, "notification", errorhelpers.SUCCESS_LOGIN) // TODO this for TODO down ?
  62. ctx.Next()
  63. }
  64. }
  65. func accountUpdateHandler(ctx *iris.Context) {
  66. username := ctx.FormValueString("username") // POST values
  67. password := ctx.FormValueString("password")
  68. userID := ctx.GetString("userID")
  69. err := usermanager.UserUpdateProcessor(username, password, userID)
  70. if err != nil { // TODO handle err nil stuff somewhere
  71. errorhelpers.HandleError(err, ctx)
  72. } else {
  73. err = errors.New(errorhelpers.SUCCESS_UPDATE)
  74. errorhelpers.HandleError(err, ctx)
  75. }
  76. }
  77. func adminPostHandler(ctx *iris.Context) {
  78. _, err := usermanager.GenerateTokens(5) // generate tokens and store in db, we don't need them now, TODO error handling?
  79. if err != nil {
  80. errorhelpers.HandleError(err, ctx)
  81. } else {
  82. err = errors.New(errorhelpers.SUCCESS_TOKENS_GENERATED)
  83. errorhelpers.HandleError(err, ctx)
  84. }
  85. }
  86. func templateHandler(ctx *iris.Context) {
  87. params := ctx.Get("params").(map[string]string)
  88. switch params["reqDir"] {
  89. case "admin":
  90. tokens := usermanager.GetTokensAsString(false)
  91. params["tokens"] = tokens
  92. ctx.Set("params", params)
  93. }
  94. ctx.MustRender(params["reqDir"] + "_box.html", params);
  95. }