summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--cmd/perseus/main.go26
-rw-r--r--handler/web/page.go12
-rw-r--r--handler/web/register.go73
-rw-r--r--web/register.html55
4 files changed, 166 insertions, 0 deletions
diff --git a/cmd/perseus/main.go b/cmd/perseus/main.go
index 1533a3c..b94793e 100644
--- a/cmd/perseus/main.go
+++ b/cmd/perseus/main.go
@@ -1,10 +1,36 @@
 package main
 
 import (
+	"fmt"
+	"log"
+	"net/http"
+	"os"
+	"time"
+
+	"tildegit.org/andinus/perseus/handler/web"
 	"tildegit.org/andinus/perseus/storage"
 )
 
 func main() {
 	db := storage.Init()
 	defer db.Conn.Close()
+
+	envPort, exists := os.LookupEnv("PERSEUS_PORT")
+	if !exists {
+		envPort = "8080"
+	}
+	addr := fmt.Sprintf("127.0.0.1:%s", envPort)
+
+	srv := &http.Server{
+		Addr:         addr,
+		WriteTimeout: 8 * time.Second,
+		ReadTimeout:  8 * time.Second,
+	}
+
+	http.HandleFunc("/register", func(w http.ResponseWriter, r *http.Request) {
+		web.HandleRegister(w, r, db)
+	})
+
+	log.Printf("main/main.go: listening on port %s...", envPort)
+	log.Fatal(srv.ListenAndServe())
 }
diff --git a/handler/web/page.go b/handler/web/page.go
new file mode 100644
index 0000000..cf96395
--- /dev/null
+++ b/handler/web/page.go
@@ -0,0 +1,12 @@
+package web
+
+import "html/template"
+
+// Page holds page information
+type Page struct {
+	SafeList []template.HTML
+	List     []string
+	Error    []string
+	Success  []string
+	Notice   []string
+}
diff --git a/handler/web/register.go b/handler/web/register.go
new file mode 100644
index 0000000..232768e
--- /dev/null
+++ b/handler/web/register.go
@@ -0,0 +1,73 @@
+package web
+
+import (
+	"fmt"
+	"html/template"
+	"log"
+	"net/http"
+	"strings"
+
+	"tildegit.org/andinus/perseus/auth"
+	"tildegit.org/andinus/perseus/storage/sqlite3"
+)
+
+// HandleRegister handles /register pages.
+func HandleRegister(w http.ResponseWriter, r *http.Request, db *sqlite3.DB) {
+	p := Page{}
+	p.Notice = []string{
+		"Only [a-z] & [0-9] allowed for username",
+		"Password length must be greater than 8 characters",
+	}
+	switch r.Method {
+	case http.MethodGet:
+		t, _ := template.ParseFiles("web/register.html")
+		t.Execute(w, p)
+
+	case http.MethodPost:
+		if err := r.ParseForm(); err != nil {
+			log.Printf("web/register.go: 400 Bad Request :: %s", err.Error())
+			http.Error(w, "400 Bad Request", http.StatusBadRequest)
+			return
+		}
+
+		// Get form values
+		uInfo := make(map[string]string)
+		uInfo["username"] = r.FormValue("username")
+		uInfo["password"] = r.FormValue("password")
+
+		// Perform registration
+		err := auth.Register(db, uInfo)
+
+		if err != nil {
+			log.Printf("web/register.go: %s :: %s :: %s",
+				"registration failed",
+				uInfo["username"],
+				err.Error())
+
+			error := []string{}
+			error = append(error,
+				fmt.Sprintf("Registration failed"))
+
+			// Check if the error was because of username
+			// not being unique.
+			if strings.HasPrefix(err.Error(), "UNIQUE constraint failed") {
+				error = append(error,
+					fmt.Sprintf("Username not unique"))
+			}
+			p.Error = error
+		} else {
+			success := []string{}
+			success = append(success,
+				fmt.Sprintf("Registration successful"))
+			p.Success = success
+		}
+
+		t, _ := template.ParseFiles("web/register.html")
+		t.Execute(w, p)
+
+	default:
+		w.WriteHeader(http.StatusMethodNotAllowed)
+		log.Printf("web/register.go: %v not allowed on %v", r.Method, r.URL)
+	}
+
+}
diff --git a/web/register.html b/web/register.html
new file mode 100644
index 0000000..d39e2cf
--- /dev/null
+++ b/web/register.html
@@ -0,0 +1,55 @@
+<!DOCTYPE html>
+<html lang="en">
+  <head>
+    <meta charset="utf-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1">
+    <title>Register &middot; Perseus</title>
+    <link rel="stylesheet" href="https://andinus.nand.sh/static/style.css">
+    <link rel="stylesheet" href="https://andinus.nand.sh/static/perseus/style.css">
+    <link rel="icon" href="https://andinus.nand.sh/static/perseus/favicon.png" type="image/png">
+  </head>
+  <body>
+    <div id="content">
+      <h1 class="title">Perseus</h1>
+      <p>
+	Perseus is a simple link aggregation and discussion program.
+	It is written in Go &amp; uses sqlite3 for storage.
+      </p>
+      <hr>{{ if .SafeList }}
+      <ul>{{ range .SafeList }}
+        <li>{{ . }}</li>{{ end }}
+      </ul>{{end}} {{ if .List }}
+      <ul>{{ range .List }}
+        <li>{{ . }}</li>{{ end }}
+      </ul>{{end}} {{ if .Error }}
+      <ul class="error">{{ range .Error }}
+        <li>{{ . }}</li>{{ end }}
+      </ul>{{end}} {{ if .Success }}
+      <ul class="success">{{ range .Success }}
+        <li>{{ . }}</li>{{ end }}
+      </ul>{{end}} {{ if .Notice }}
+      <ul class="notice">{{ range .Notice }}
+        <li>{{ . }}</li>{{ end }}
+      </ul>{{end}}
+      <form action="./register" method="post">
+	<h4>Username</h4>
+        <input type="text" name="username" required>
+	<h4>Password</h4>
+	<input type="password" name="password" required>
+        <input type="submit" name="submit" value="Go!">
+      </form>
+    </div>
+    <div id="postamble" class="status">
+      <p class="postamble">
+	<a href="https://andinus.nand.sh/">Andinus</a>
+	&nbsp;/&nbsp;
+	<a href="https://andinus.nand.sh/perseus">Perseus</a>
+	<span style="float:right">
+	  <a href="https://tildegit.org/andinus/perseus">
+	    Source Code
+	  </a>
+	</span>
+      </p>
+    </div>
+  </body>
+</html>