aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Kavon <hawk@alexkavon.com>2023-11-28 05:24:30 -0500
committerAlexander Kavon <hawk@alexkavon.com>2023-11-28 05:24:30 -0500
commit629b0189b7bf20c748a1d37f8803ad0e3ffb8a49 (patch)
tree6692374508230b46be3aee323acd7aa25b3d4a91
parent7d7059d53891bc1abb284d9b288505a5d406b307 (diff)
working container compose w/database, no more auth package, message template partial, working html template writing, auth routes in user/routes.go
-rw-r--r--container-compose.yml3
-rw-r--r--src/auth/routes.go23
-rw-r--r--src/main.go2
-rw-r--r--src/server/ui.go16
-rw-r--r--src/user/routes.go50
-rw-r--r--src/user/user.go1
-rw-r--r--ui/pages/auth/register.tmpl.html16
-rw-r--r--ui/pages/user/create.tmpl.html20
-rw-r--r--ui/pages/user/login.tmpl.html16
-rw-r--r--ui/pages/users/create.tmpl.html20
-rw-r--r--ui/templates/base.tmpl.html2
-rw-r--r--ui/templates/messages.tmpl.html9
12 files changed, 105 insertions, 73 deletions
diff --git a/container-compose.yml b/container-compose.yml
index a471d8b..42e2371 100644
--- a/container-compose.yml
+++ b/container-compose.yml
@@ -15,6 +15,9 @@ services:
database:
image: postgres:latest
restart: always
+ environment:
+ POSTGRES_USER: newsstand
+ POSTGRES_PASSWORD: newsstand
ports:
- "9002:5432"
volumes:
diff --git a/src/auth/routes.go b/src/auth/routes.go
deleted file mode 100644
index 5a28756..0000000
--- a/src/auth/routes.go
+++ /dev/null
@@ -1,23 +0,0 @@
-package auth
-
-import (
- "net/http"
-
- "gitlab.com/alexkavon/newsstand/src/server"
-)
-
-var Routes = server.Routes{
- server.Route{
- Name: "Register",
- Method: "GET",
- Path: "/auth/register",
- AuthRequired: false,
- HandlerFunc: Register,
- },
-}
-
-func Register(s *server.Server) http.HandlerFunc {
- return func(w http.ResponseWriter, r *http.Request) {
- s.Ui.Render(w, "auth/register", nil)
- }
-}
diff --git a/src/main.go b/src/main.go
index d3f6c03..0687aae 100644
--- a/src/main.go
+++ b/src/main.go
@@ -1,7 +1,6 @@
package main
import (
- "gitlab.com/alexkavon/newsstand/src/auth"
"gitlab.com/alexkavon/newsstand/src/conf"
"gitlab.com/alexkavon/newsstand/src/db"
"gitlab.com/alexkavon/newsstand/src/server"
@@ -16,7 +15,6 @@ func main() {
s := server.NewServer(config, db.NewDb(config))
s.BuildUi()
routers := []server.Routes{
- auth.Routes,
user.Routes,
}
for _, r := range routers {
diff --git a/src/server/ui.go b/src/server/ui.go
index ce0ef01..a765d97 100644
--- a/src/server/ui.go
+++ b/src/server/ui.go
@@ -20,14 +20,15 @@ func NewUi(config *conf.Conf) Ui {
func (ui *Ui) CompilePages(uipath string) {
ui.pages = map[string]*template.Template{}
- baseTmpl, err := template.ParseFiles(filepath.Join(uipath, "templates/base.tmpl.html"))
+ baseTmpl, err := template.ParseGlob(filepath.Join(uipath, "templates/*.tmpl.html"))
if err != nil {
log.Fatal(err)
}
- pagesDir := filepath.Join(uipath, "pages")
- tmplGlob := "**/*.tmpl.html"
+ ui.pages["core/messages"] = baseTmpl
- fileglob, err := filepath.Glob(filepath.Join(pagesDir, tmplGlob))
+ pagesDir := filepath.Join(uipath, "pages")
+ tmplGlob := filepath.Join(pagesDir, "**/*.tmpl.html")
+ fileglob, err := filepath.Glob(tmplGlob)
if err != nil {
log.Fatal(err)
}
@@ -47,8 +48,13 @@ func (ui *Ui) CompilePages(uipath string) {
}
func (ui *Ui) Render(w http.ResponseWriter, pageName string, data interface{}) {
+ templateName := "base"
+ if pageName == "core/messages" {
+ templateName = "messages"
+ }
p := ui.pages[pageName]
- err := p.ExecuteTemplate(w, "base", data)
+
+ err := p.ExecuteTemplate(w, templateName, data)
if err != nil {
log.Fatal(err)
}
diff --git a/src/user/routes.go b/src/user/routes.go
index 791213d..09c53b2 100644
--- a/src/user/routes.go
+++ b/src/user/routes.go
@@ -1,6 +1,7 @@
package user
import (
+ "fmt"
"net/http"
"gitlab.com/alexkavon/newsstand/src/server"
@@ -8,16 +9,33 @@ import (
var Routes = server.Routes{
server.Route{
- Name: "Create",
- Method: "GET",
- Path: "/users/create",
- AuthRequired: false,
- HandlerFunc: Create,
+ Name: "Create",
+ Method: "GET",
+ Path: "/user/create",
+ HandlerFunc: Create,
+ },
+ server.Route{
+ Name: "Store",
+ Method: "POST",
+ Path: "/user",
+ HandlerFunc: Store,
+ },
+ server.Route{
+ Name: "LoginForm",
+ Method: "GET",
+ Path: "/user/auth",
+ HandlerFunc: LoginForm,
+ },
+ server.Route{
+ Name: "Authenticate",
+ Method: "POST",
+ Path: "/user/auth",
+ HandlerFunc: Authenticate,
},
server.Route{
Name: "Me",
Method: "GET",
- Path: "/users/me",
+ Path: "/user/me",
AuthRequired: true,
HandlerFunc: Show,
},
@@ -25,10 +43,28 @@ var Routes = server.Routes{
func Create(s *server.Server) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
- s.Ui.Render(w, "users/create", nil)
+ s.Ui.Render(w, "user/create", nil)
+ }
+}
+
+func Store(s *server.Server) http.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) {
+ r.ParseForm()
+ fmt.Println(r.PostForm)
+ s.Ui.Render(w, "core/messages", &struct{ Message string }{Message: "Congrats"})
}
}
+func LoginForm(s *server.Server) http.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) {
+ s.Ui.Render(w, "user/login", nil)
+ }
+}
+
+func Authenticate(s *server.Server) http.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) {}
+}
+
func Show(s *server.Server) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {}
}
diff --git a/src/user/user.go b/src/user/user.go
index 9845b1b..97fe74c 100644
--- a/src/user/user.go
+++ b/src/user/user.go
@@ -3,4 +3,5 @@ package user
type User struct {
Id int64 `json:"id"`
Username string `json:"username"`
+ Karma uint64 `json:"karma"`
}
diff --git a/ui/pages/auth/register.tmpl.html b/ui/pages/auth/register.tmpl.html
deleted file mode 100644
index 7dfd7aa..0000000
--- a/ui/pages/auth/register.tmpl.html
+++ /dev/null
@@ -1,16 +0,0 @@
-{{define "title"}}Register{{end}}
-
-{{define "main"}}
- <h1>Registration</h1>
- <form hx-post="/user">
- <label>
- Username
- <input type="text" placeholder="username" />
- </label>
- <label>
- Password
- <input type="password" placeholder="password" />
- </label>
- <button type="submit">Register</button>
- </form>
-{{end}}
diff --git a/ui/pages/user/create.tmpl.html b/ui/pages/user/create.tmpl.html
new file mode 100644
index 0000000..5e1087a
--- /dev/null
+++ b/ui/pages/user/create.tmpl.html
@@ -0,0 +1,20 @@
+{{define "title"}}Create User{{end}}
+
+{{define "main"}}
+ <h1>Create User</h1>
+ <form hx-post="/user" action="/user" method="POST" hx-target="#messages" hx-swap="outerHTML">
+ <label>
+ Email
+ <input type="email" placeholder="email" name="email" />
+ </label>
+ <label>
+ Username
+ <input type="text" placeholder="username" name="username" />
+ </label>
+ <label>
+ Password
+ <input type="password" placeholder="password" name="password" />
+ </label>
+ <button type="submit" hx-disabled-elt="this">Create</button>
+ </form>
+{{end}}
diff --git a/ui/pages/user/login.tmpl.html b/ui/pages/user/login.tmpl.html
new file mode 100644
index 0000000..5c42f97
--- /dev/null
+++ b/ui/pages/user/login.tmpl.html
@@ -0,0 +1,16 @@
+{{define "title"}}Login{{end}}
+
+{{define "main"}}
+ <h1>Login</h1>
+ <form hx-post="/user" action="/user/auth" method="POST" hx-target="#messages" hx-swap="outerHTML">
+ <label>
+ Username
+ <input type="text" placeholder="username" name="username" />
+ </label>
+ <label>
+ Password
+ <input type="password" placeholder="password" name="password" />
+ </label>
+ <button type="submit" hx-disabled-elt="this">Login</button>
+ </form>
+{{end}}
diff --git a/ui/pages/users/create.tmpl.html b/ui/pages/users/create.tmpl.html
deleted file mode 100644
index 381f6ea..0000000
--- a/ui/pages/users/create.tmpl.html
+++ /dev/null
@@ -1,20 +0,0 @@
-{{define "title"}}Create User{{end}}
-
-{{define "main"}}
- <h1>Registration</h1>
- <form hx-post="/user">
- <label>
- Email
- <input type="email" placeholder="email" />
- </label>
- <label>
- Username
- <input type="text" placeholder="username" />
- </label>
- <label>
- Password
- <input type="password" placeholder="password" />
- </label>
- <button type="submit">Register</button>
- </form>
-{{end}}
diff --git a/ui/templates/base.tmpl.html b/ui/templates/base.tmpl.html
index 4e28bc0..a080925 100644
--- a/ui/templates/base.tmpl.html
+++ b/ui/templates/base.tmpl.html
@@ -3,9 +3,11 @@
<html lang="en">
<head>
<title>{{template "title" .}} | newsstand.nyc</title>
+ <link rel="stylesheet" href="https://unpkg.com/@blaze/css@x.x.x/dist/blaze/blaze.css">
<script src="https://unpkg.com/htmx.org@1.9.9" integrity="sha384-QFjmbokDn2DjBjq+fM+8LUIVrAgqcNW2s0PjAxHETgRn9l4fvX31ZxDxvwQnyMOX" crossorigin="anonymous"></script>
</head>
<body>
+ {{template "messages" .}}
{{template "main" .}}
</body>
</html>
diff --git a/ui/templates/messages.tmpl.html b/ui/templates/messages.tmpl.html
new file mode 100644
index 0000000..b9d327f
--- /dev/null
+++ b/ui/templates/messages.tmpl.html
@@ -0,0 +1,9 @@
+{{define "messages"}}
+<div id="messages">
+ {{ if .Message }}
+ <div class="c-alert c-alert--info" role="alert">
+ {{.Message}}
+ </div>
+ {{ end }}
+</div>
+{{end}}