init
This commit is contained in:
förälder
8055020aac
incheckning
ac9804f7f3
|
@ -0,0 +1,11 @@
|
|||
package helpers
|
||||
|
||||
import "io/ioutil"
|
||||
|
||||
func LoadFile(fileName string) (string, error) {
|
||||
bytes, err := ioutil.ReadFile(fileName)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(bytes), nil
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package helpers
|
||||
|
||||
func IsEmpty(data string) bool {
|
||||
if len(data) <= 0 {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"username": "paul",
|
||||
"password": "lolbad",
|
||||
"email": "email@definitely.real",
|
||||
"gender": 1
|
||||
}
|
|
@ -0,0 +1,78 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
_ "github.com/lib/pq"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// The "db" package level variable will hold the reference to our database instance
|
||||
var db *sql.DB
|
||||
|
||||
const (
|
||||
host = "localhost"
|
||||
port = 5432
|
||||
user = "postgres"
|
||||
password = "sqldawg!"
|
||||
dbname = "maplestory"
|
||||
)
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/login", Login)
|
||||
http.HandleFunc("/register", Register)
|
||||
initDB()
|
||||
log.Fatal(http.ListenAndServe(":42069", nil))
|
||||
}
|
||||
|
||||
func initDB() {
|
||||
var err error
|
||||
|
||||
db, err = sql.Open("postgres", "host=localhost port=5432 user=postgres password=sqldawg! dbname=maplestory sslmode=disable")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
type Credentials struct {
|
||||
Password string `json:"password", db:"password"`
|
||||
Username string `json:"username", db:"username"`
|
||||
Email string `json:"email", db:"email"`
|
||||
Gender int `json:"gender", db:"gender"`
|
||||
}
|
||||
|
||||
func Login(w http.ResponseWriter, r *http.Request) {
|
||||
ip := strings.Split(r.RemoteAddr, ":")[0]
|
||||
fmt.Println(ip)
|
||||
fmt.Fprintf(w, ip)
|
||||
}
|
||||
|
||||
func Register(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
creds := &Credentials{}
|
||||
err := json.NewDecoder(r.Body).Decode(creds)
|
||||
ip := strings.Split(r.RemoteAddr, ":")[0]
|
||||
if ip == "[" {
|
||||
ip = "127.0.0.1"
|
||||
}
|
||||
|
||||
fmt.Println(ip + "/register")
|
||||
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(creds.Password), 8)
|
||||
|
||||
if _, err = db.Query("INSERT INTO maplestory.accounts (username,password,email,creation,last_login,last_ip,ban,admin,gender) values ($1, $2, $3, $4, $5, $6, $7, $8, $9)",
|
||||
creds.Username, string(hashedPassword), creds.Email, time.Now(), time.Now(), ip, '0', '0', creds.Gender); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
panic(err)
|
||||
return
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"username": "paul",
|
||||
"password": "lolbad",
|
||||
"email": "email@definitely.real",
|
||||
"gender": 1
|
||||
}
|
Laddar…
Referens i nytt ärende