|
|
@@ -0,0 +1,79 @@
|
|
|
+package main
|
|
|
+
|
|
|
+import (
|
|
|
+ "github.com/kataras/iris"
|
|
|
+ "fmt"
|
|
|
+ "git.mmnx.de/Moe/databaseutils"
|
|
|
+ "git.mmnx.de/Moe/usermanager"
|
|
|
+ "git.mmnx.de/Moe/configutils"
|
|
|
+)
|
|
|
+
|
|
|
+func main() {
|
|
|
+
|
|
|
+ // fmt.Printf("%+v\n", user)
|
|
|
+
|
|
|
+ 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.Get("/login", func (ctx *iris.Context) {
|
|
|
+ ctx.MustRender("login.html", nil)
|
|
|
+ })
|
|
|
+
|
|
|
+ iris.Post("/login", loginHandler)
|
|
|
+
|
|
|
+ needAuth := iris.Party("/secret", usermanager.AuthHandler)
|
|
|
+ {
|
|
|
+ needAuth.Get("/", func(ctx *iris.Context) {
|
|
|
+ username := ctx.GetString("mycustomkey") // the Contextkey from the authConfig
|
|
|
+ ctx.Write("Hello authenticated user: %s from localhost:8080/secret ", username)
|
|
|
+ })
|
|
|
+
|
|
|
+ needAuth.Get("/profile", func(ctx *iris.Context) {
|
|
|
+ username := ctx.GetString("mycustomkey") // the Contextkey from the authConfig
|
|
|
+ ctx.Write("Hello authenticated user: %s from localhost:8080/secret/profile ", username)
|
|
|
+ })
|
|
|
+
|
|
|
+ /*needAuth.Get("/settings", func(ctx *iris.Context) {
|
|
|
+ username := authConfig.User(ctx) // same thing as ctx.GetString("mycustomkey")
|
|
|
+ ctx.Write("Hello authenticated user: %s from localhost:8080/secret/settings ", username)
|
|
|
+ })*/
|
|
|
+
|
|
|
+ needAuth.Get("/test", testHandler)
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ 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.Write(err.Error())
|
|
|
+ }
|
|
|
+
|
|
|
+ ctx.SetCookieKV("token", tokenString)
|
|
|
+}
|
|
|
+
|
|
|
+func testHandler(ctx *iris.Context) {
|
|
|
+ userID := ctx.Get("userID")
|
|
|
+ fmt.Printf("%#v\n", userID)
|
|
|
+ ctx.Write("Test %s", userID);
|
|
|
+}
|