summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNikolaus Gotsche <n@softwarefools.com>2017-09-30 01:36:55 +0200
committerNikolaus Gotsche <n@softwarefools.com>2017-09-30 01:36:55 +0200
commite043d7a4772e08ce158645199c7dc14908589e51 (patch)
tree6b030ec10ccdea60119246bb0cb8894a57031d58
parent3fd5c25226ecaed13687ab59d12b1dbb2a2f1344 (diff)
redis functions
renaming
-rw-r--r--hexfool.go (renamed from signup.go)33
-rw-r--r--log.go2
-rw-r--r--redigo.go51
3 files changed, 73 insertions, 13 deletions
diff --git a/signup.go b/hexfool.go
index 1285c41..2da7de7 100644
--- a/signup.go
+++ b/hexfool.go
@@ -20,7 +20,7 @@ func signupPage(res http.ResponseWriter, req *http.Request) {
username := req.FormValue("username")
password := req.FormValue("password")
-
+ email := req.FormValue("email")
var user string
err := db.QueryRow("SELECT username FROM players WHERE username=?",username).Scan(&user)
@@ -30,20 +30,23 @@ func signupPage(res http.ResponseWriter, req *http.Request) {
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
http.Error(res, "Server Error creating Password. Unable to create account! You Fool...", 500)
- Log("ERROR 500")
+ Log("ERROR 500 - Hash generation Failed")
+ panic(err)
return
}
newuuid, uiderr := exec.Command("uuidgen").Output()
if uiderr != nil{
http.Error(res, "Server Error creating UUID. Unable to create account! You Fool...", 500)
- Log("ERROR 500")
+ Log("ERROR 500 - Creating UUID failed")
+ panic(uiderr)
return
}
- _, err = db.Exec("INSERT INTO players(username, password, uuid) VALUES(?, ?, ?)", username, hashedPassword, newuuid)
+ _, err = db.Exec("INSERT INTO players(username, password, email) VALUES(?, ?, ?)", username, hashedPassword, email)
if err != nil {
http.Error(res, "Server Error Inserting User. Unable to create account! You Fool...", 500)
Log("ERROR adding user"+username)
+ panic(err)
//fmt.Println("Error adding User",username)
return
}
@@ -54,6 +57,7 @@ func signupPage(res http.ResponseWriter, req *http.Request) {
case err != nil:
http.Error(res, "Server Error! Something fucked up", 500)
Log("ERROR Something fucked up")
+ panic(err)
return
default:
http.Redirect(res, req, "/", 301)
@@ -71,9 +75,9 @@ func loginPage(res http.ResponseWriter, req *http.Request) {
var databaseUsername string
var databasePassword string
- var databaseUUID string
+// var databaseUUID string
- err := db.QueryRow("SELECT username, password, uuid FROM players WHERE username=?", username).Scan(&databaseUsername, &databasePassword, &databaseUUID)
+ err := db.QueryRow("SELECT username, password FROM players WHERE username=?", username).Scan(&databaseUsername, &databasePassword)
if err != nil {
http.Redirect(res, req, "/login", 301)
@@ -86,15 +90,28 @@ func loginPage(res http.ResponseWriter, req *http.Request) {
if err != nil {
http.Redirect(res, req, "/login", 301)
Log(username+" used wrong Password "+password)
+ panic(err)
//fmt.Println(username," used wrong Password ",password)
return
}
-
- res.Write([]byte("Hello "+ databaseUsername + "! Your UUID is "+ databaseUUID))
+ tok, uiderr := getToken()
+ if uiderr != nil{
+ http.Error(res, "Server Error creating Token. You Fool...", 500)
+ Log("ERROR 500 - Creating UUID failed")
+ panic(uiderr)
+ return
+ }
+
+ res.Write([]byte("Hello "+ databaseUsername + "! Your token is "+ string(tok)))
Log("Successful Login by "+username)
//fmt.Println("Successful Login by",username)
}
+func getToken() ([]byte, error) {
+ newuuid, uiderr := exec.Command("uuidgen").Output()
+ return newuuid, uiderr
+}
+
func homePage(res http.ResponseWriter, req *http.Request) {
http.ServeFile(res, req, "html/index.html")
}
diff --git a/log.go b/log.go
index 365989b..976ce91 100644
--- a/log.go
+++ b/log.go
@@ -21,7 +21,7 @@ func Log(text string) {
panic(err)
}
if loud {
- fmt.Println(full)
+ fmt.Println("Log",full)
}
}
diff --git a/redigo.go b/redigo.go
index ff1bf3f..f83b174 100644
--- a/redigo.go
+++ b/redigo.go
@@ -3,8 +3,47 @@ package main
import (
"github.com/go-redis/redis"
"fmt"
+ "time"
)
+var client *redis.Client
+
+func init() {
+ client = redis.NewClient(&redis.Options{
+ Addr: "192.168.0.16:6379",
+ Password: "", // no password set
+ DB: 0, // use default DB
+
+ // DialTimeout: 10 * time.Second,
+ // ReadTimeout: 30 * time.Second,
+ // WriteTimeout: 30 * time.Second,
+ // PoolSize: 10,
+ // PoolTimeout: 30 * time.Second,
+ })
+ //client.FlushDB()
+}
+
+func getRed(key string) string {
+ val, err := client.Get(key).Result()
+ if err == redis.Nil {
+ return "nil"
+ } else if err != nil {
+ panic(err)
+ return err.Error()
+ } else {
+ return val
+ }
+}
+
+func setRed(key string , val string, dur int) bool {
+ err := client.Set(key, val,time.Duration(dur)*time.Second).Err()
+ if err != nil {
+ panic(err)
+ return false
+ }
+ return true
+}
+
func ExampleNewClient() {
client := redis.NewClient(&redis.Options{
Addr: "192.168.0.16:6379",
@@ -17,13 +56,14 @@ func ExampleNewClient() {
// Output: PONG <nil>
}
+
func ExampleClient() {
client := redis.NewClient(&redis.Options{
Addr: "192.168.0.16:6379",
Password: "", // no password set
DB: 0, // use default DB
})
- err := client.Set("key", "value", 0).Err()
+ err := client.Set("zack", "kack", 0).Err()
if err != nil {
panic(err)
}
@@ -46,6 +86,9 @@ func ExampleClient() {
// key2 does not exists
}
-func main() {
- ExampleNewClient()
-}
+//func main() {
+ //ExampleClient()
+// if setRed("scheisse","geil",10) {
+// fmt.Println("euda")
+// }
+//}