main.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package main
  2. import (
  3. "github.com/kataras/iris"
  4. "fmt"
  5. "git.mmnx.de/Moe/databaseutils"
  6. "git.mmnx.de/Moe/usermanager"
  7. "git.mmnx.de/Moe/configutils"
  8. )
  9. func main() {
  10. // fmt.Printf("%+v\n", user)
  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. iris.Get("/login", func (ctx *iris.Context) {
  20. ctx.MustRender("login.html", nil)
  21. })
  22. iris.Post("/login", loginHandler)
  23. needAuth := iris.Party("/secret", usermanager.AuthHandler)
  24. {
  25. needAuth.Get("/", func(ctx *iris.Context) {
  26. username := ctx.GetString("mycustomkey") // the Contextkey from the authConfig
  27. ctx.Write("Hello authenticated user: %s from localhost:8080/secret ", username)
  28. })
  29. needAuth.Get("/profile", func(ctx *iris.Context) {
  30. username := ctx.GetString("mycustomkey") // the Contextkey from the authConfig
  31. ctx.Write("Hello authenticated user: %s from localhost:8080/secret/profile ", username)
  32. })
  33. /*needAuth.Get("/settings", func(ctx *iris.Context) {
  34. username := authConfig.User(ctx) // same thing as ctx.GetString("mycustomkey")
  35. ctx.Write("Hello authenticated user: %s from localhost:8080/secret/settings ", username)
  36. })*/
  37. needAuth.Get("/test", testHandler)
  38. }
  39. iris.Listen(":8080")
  40. }
  41. func loginHandler(ctx *iris.Context) {
  42. username := ctx.FormValueString("username") // POST values
  43. password := ctx.FormValueString("password")
  44. user := usermanager.User{} // new user
  45. tokenString, err := user.Login(username, password) // try to login
  46. if err != nil { // TODO: template compatible error handling
  47. ctx.Write(err.Error())
  48. }
  49. ctx.SetCookieKV("token", tokenString)
  50. }
  51. func testHandler(ctx *iris.Context) {
  52. userID := ctx.Get("userID")
  53. fmt.Printf("%#v\n", userID)
  54. ctx.Write("Test %s", userID);
  55. }