feat: added users db, login, register, logout

This commit is contained in:
2023-09-19 13:39:59 +01:00
parent b22d64a568
commit f2bf34b931
15 changed files with 586 additions and 50 deletions

33
main.go
View File

@@ -2,25 +2,36 @@ package main
import (
"fmt"
"net/http"
"database/sql"
_ "github.com/lib/pq"
)
const (
host = "localhost"
port = 5432
user = "postgres"
password = "verysafepassword"
dbname = "aistuff"
)
func main() {
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
"password=%s dbname=%s sslmode=disable",
host, port, user, password, dbname)
db, err := sql.Open("postgres", psqlInfo)
if err != nil {
panic(err)
}
defer db.Close()
fmt.Println("Starting server on :8000!")
handle := NewHandler()
handle := NewHandler(db)
handle.GetHTML("/", AnswerTemplate("index.html", nil))
handle.GetHTML("/login", AnswerTemplate("login.html", nil))
handle.Post("/login", func(mode AnswerType, w http.ResponseWriter, r *http.Request) *Error {
if mode == JSON {
return &Error{code: 404}
}
w.Header().Set("Location", "/")
w.WriteHeader(http.StatusSeeOther)
return nil
})
usersEndpints(db, handle)
handle.Startup()
}