summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--getuuid.go15
-rw-r--r--jsonrest.go96
-rw-r--r--signup.go105
3 files changed, 216 insertions, 0 deletions
diff --git a/getuuid.go b/getuuid.go
new file mode 100644
index 0000000..b9cb7ae
--- /dev/null
+++ b/getuuid.go
@@ -0,0 +1,15 @@
+package main
+
+import (
+ "fmt"
+ "log"
+ "os/exec"
+)
+
+func main() {
+ out,err := exec.Command("uuidgen").Output()
+ if err != nil {
+ log.Fatal(err)
+ }
+ fmt.Printf("%s", out)
+}
diff --git a/jsonrest.go b/jsonrest.go
new file mode 100644
index 0000000..e6e948e
--- /dev/null
+++ b/jsonrest.go
@@ -0,0 +1,96 @@
+package main
+
+import (
+ "net/http"
+ "log"
+ "sync"
+ "github.com/ant0ine/go-json-rest/rest"
+)
+
+func main() {
+ api := rest.NewApi()
+ api.Use(rest.DefaultDevStack...)
+// api.SetApp(rest.AppSimple(func(w rest.ResponseWriter, r *rest.Request) {
+// w.WriteJson(map[string]string{"Body": "Hello World!"})
+// }))
+// log.Fatal(http.ListenAndServe(":8086", api.MakeHandler()))
+ router, err := rest.MakeRouter(
+ rest.Get("/countries", GetAllCountries),
+ rest.Post("/countries", PostCountry),
+ rest.Get("/countries/:code", GetCountry),
+ rest.Delete("/countries/:code", DeleteCountry),
+ )
+ if err != nil{
+ log.Fatal(err)
+ }
+ api.SetApp(router)
+ log.Fatal(http.ListenAndServe(":8086", api.MakeHandler()))
+}
+
+type Country struct {
+ Code string
+ Name string
+}
+
+var store = map[string]*Country{}
+
+var lock = sync.RWMutex{}
+
+func GetCountry(w rest.ResponseWriter, r *rest.Request) {
+ code := r.PathParam("code")
+
+ lock.RLock()
+ var country *Country
+ if store[code] != nil {
+ country = &Country{}
+ *country = *store[code]
+ }
+ lock.RUnlock()
+ if country == nil {
+ rest.NotFound(w, r)
+ return
+ }
+ w.WriteJson(country)
+
+}
+
+func GetAllCountries(w rest.ResponseWriter, r *rest.Request) {
+ lock.RLock()
+ countries := make([]Country, len(store))
+ i := 0
+ for _, country := range store {
+ countries[i] = *country
+ i++
+ }
+ lock.RUnlock()
+ w.WriteJson(&countries)
+}
+
+func PostCountry(w rest.ResponseWriter, r *rest.Request) {
+ country := Country{}
+ err := r.DecodeJsonPayload(&country)
+ if err != nil {
+ rest.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+ if country.Code == "" {
+ rest.Error(w, "country code required", 400)
+ return
+ }
+ if country.Name == "" {
+ rest.Error(w, "country name required", 400)
+ return
+ }
+ lock.Lock()
+ store[country.Code] = &country
+ lock.Unlock()
+ w.WriteJson(&country)
+}
+
+func DeleteCountry(w rest.ResponseWriter, r *rest.Request) {
+ code := r.PathParam("code")
+ lock.Lock()
+ delete(store, code)
+ lock.Unlock()
+ w.WriteHeader(http.StatusOK)
+}
diff --git a/signup.go b/signup.go
new file mode 100644
index 0000000..e659505
--- /dev/null
+++ b/signup.go
@@ -0,0 +1,105 @@
+package main
+
+import "database/sql"
+import _ "github.com/go-sql-driver/mysql"
+
+import "golang.org/x/crypto/bcrypt"
+
+import "net/http"
+
+import "os/exec"
+
+var db *sql.DB
+var err error
+
+func signupPage(res http.ResponseWriter, req *http.Request) {
+ if req.Method != "POST" {
+ http.ServeFile(res, req, "html/signup.html")
+ return
+ }
+
+ username := req.FormValue("username")
+ password := req.FormValue("password")
+
+ var user string
+
+ err := db.QueryRow("SELECT username FROM players WHERE username=?",username).Scan(&user)
+
+ switch {
+ case err == sql.ErrNoRows:
+ 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)
+ return
+ }
+ newuuid, uiderr := exec.Command("uuidgen").Output()
+ if uiderr != nil{
+ http.Error(res, "Server Error creating UUID. Unable to create account! You Fool...", 500)
+ return
+ }
+
+ _, err = db.Exec("INSERT INTO players(username, password, uuid) VALUES(?, ?, ?)", username, hashedPassword, newuuid)
+ if err != nil {
+ http.Error(res, "Server Error Inserting User. Unable to create account! You Fool...", 500)
+ return
+ }
+ res.Write([]byte("User Created Successfully!"))
+ return
+ case err != nil:
+ http.Error(res, "Server Error! Something fucked up", 500)
+ return
+ default:
+ http.Redirect(res, req, "/", 301)
+ }
+}
+
+func loginPage(res http.ResponseWriter, req *http.Request) {
+ if req.Method != "POST" {
+ http.ServeFile(res, req, "html/login.html")
+ return
+ }
+
+ username := req.FormValue("username")
+ password := req.FormValue("password")
+
+ var databaseUsername string
+ var databasePassword string
+ var databaseUUID string
+
+ err := db.QueryRow("SELECT username, password, uuid FROM players WHERE username=?", username).Scan(&databaseUsername, &databasePassword, &databaseUUID)
+
+ if err != nil {
+ http.Redirect(res, req, "/login", 301)
+ return
+ }
+
+ err = bcrypt.CompareHashAndPassword([]byte(databasePassword), []byte(password))
+ if err != nil {
+ http.Redirect(res, req, "/login", 301)
+ return
+ }
+
+ res.Write([]byte("Hello "+ databaseUsername + "! Your UUID is "+ databaseUUID))
+}
+
+func homePage(res http.ResponseWriter, req *http.Request) {
+ http.ServeFile(res, req, "html/index.html")
+}
+
+func main() {
+ db, err = sql.Open("mysql", "hexmaster:waSIdocHneTdUobeRoarscH@tcp(192.168.0.16)/hexmaster")
+ if err != nil {
+ panic(err.Error())
+ }
+ defer db.Close()
+
+ err = db.Ping()
+ if err != nil {
+ panic(err.Error())
+ }
+
+ http.HandleFunc("/signup", signupPage)
+ http.HandleFunc("/login", loginPage)
+ http.HandleFunc("/", homePage)
+ http.ListenAndServe(":8088", nil)
+}