main.go 3.2 KB

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