Moritz Schmidt 9 anni fa
parent
commit
832fa52f98
4 ha cambiato i file con 108 aggiunte e 0 eliminazioni
  1. 7 0
      config.json.dist
  2. 79 0
      main.go
  3. 8 0
      templates/hi.html
  4. 14 0
      templates/login.html

+ 7 - 0
config.json.dist

@@ -0,0 +1,7 @@
+{
+    "dbHost": "127.0.0.1:3306",
+    "dbUser": "user",
+    "dbPass": "pass",
+    "dbName": "vpn",
+    "cryptoKey": "s3cr3t"
+}

+ 79 - 0
main.go

@@ -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);
+}

+ 8 - 0
templates/hi.html

@@ -0,0 +1,8 @@
+<html>
+<head>
+<title>Hi Iris</title>
+</head>
+<body>
+    <h1>Hi {{.Name}}</h1>
+</body>
+</html>

+ 14 - 0
templates/login.html

@@ -0,0 +1,14 @@
+<html>
+  <head>
+    <title>Login</title>
+  </head>
+  <body>
+    <form action="/login" method="post">
+      Username:<br>
+      <input type="text" name="username" placeholder="Username"><br>
+      Password:<br>
+      <input type="password" name="password" placeholder="Password"><br><br>
+      <input type="submit" value="Login">
+    </form>
+  </body>
+</html>