Add registration handler

This commit is contained in:
Andinus 2020-03-27 21:20:12 +05:30
parent 97f36a08c7
commit 3dee795567
Signed by: andinus
GPG Key ID: B67D55D482A799FD
4 changed files with 166 additions and 0 deletions

View File

@ -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())
}

12
handler/web/page.go Normal file
View File

@ -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
}

73
handler/web/register.go Normal file
View File

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

55
web/register.html Normal file
View File

@ -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>