From 087e5a2f4c8d7526e8f4a562f1c195d26ce16745 Mon Sep 17 00:00:00 2001 From: m15o Date: Mon, 22 Nov 2021 21:23:30 +0100 Subject: [PATCH] Quite a few things --- assets/style.css | 23 ++++++++++++ config/cfg.go | 2 + web/handler/common.go | 18 +++++++-- web/handler/form/status.go | 5 +++ web/handler/handler.go | 9 +++-- web/handler/html.go | 41 ++++++++++++++++----- web/handler/html/common/layout.html | 17 +++++++-- web/handler/html/confirm_remove_status.html | 3 +- web/handler/html/edit_status.html | 4 +- web/handler/html/index.html | 34 +++++++++++++---- web/handler/index_show.go | 18 +++++++++ web/handler/status_remove.go | 4 +- web/handler/status_save.go | 2 +- 13 files changed, 151 insertions(+), 29 deletions(-) create mode 100644 assets/style.css diff --git a/assets/style.css b/assets/style.css new file mode 100644 index 0000000..950a69e --- /dev/null +++ b/assets/style.css @@ -0,0 +1,23 @@ +body { + max-width: 940px; + margin: 0 auto; + padding: 1em; + font-family: Verdana; +} + +section { + padding: 1em; + background-color: peachpuff; +} + +.cols { + display: grid; + row-gap: 1em; +} + +@media (min-width: 650px) { + .cols { + grid-template-columns: repeat(2, 1fr); + grid-gap: 1em; + } +} \ No newline at end of file diff --git a/config/cfg.go b/config/cfg.go index 0ad327e..dfcf43f 100644 --- a/config/cfg.go +++ b/config/cfg.go @@ -9,6 +9,7 @@ type ( Env string CertFile string KeyFile string + AssetsDir string } ) @@ -19,5 +20,6 @@ func New() *Config { Env: os.Getenv("ENV"), CertFile: os.Getenv("CERT_FILE"), KeyFile: os.Getenv("CERT_KEY_FILE"), + AssetsDir: os.Getenv("ASSETS_DIR"), } } diff --git a/web/handler/common.go b/web/handler/common.go index 7771ded..33b306b 100644 --- a/web/handler/common.go +++ b/web/handler/common.go @@ -9,14 +9,26 @@ var TplCommonMap = map[string]string{ - - Status + status cafe + + {{ template "head" . }} +
+

status.cafe

+
+
{{ template "content" . }} +
+ + + {{ end }} -{{ define "head" }}{{ end }}`, +{{ define "head" }}{{ end }} +`, } diff --git a/web/handler/form/status.go b/web/handler/form/status.go index fb9034c..888574b 100644 --- a/web/handler/form/status.go +++ b/web/handler/form/status.go @@ -2,15 +2,20 @@ package form import ( "net/http" + "strconv" ) type StatusForm struct { + Id int64 Content string Error string } func NewStatusForm(r *http.Request) *StatusForm { + var id int64 + id, _ = strconv.ParseInt(r.FormValue("id"), 10, 64) return &StatusForm{ + Id: id, Content: r.FormValue("content"), } } diff --git a/web/handler/handler.go b/web/handler/handler.go index c80aadc..aa55a7d 100644 --- a/web/handler/handler.go +++ b/web/handler/handler.go @@ -71,11 +71,14 @@ func New(cfg *config.Config, sess *session.Session, data *storage.Storage) (http router.HandleFunc("/register", h.handleRegister) router.HandleFunc("/logout", h.logout).Methods(http.MethodGet) - router.HandleFunc("/statuses/new", h.showNewStatusView).Methods(http.MethodGet) - router.HandleFunc("/statuses/save", h.saveStatus).Methods(http.MethodPost) + //router.HandleFunc("/statuses/new", h.showNewStatusView).Methods(http.MethodGet) + router.HandleFunc("/status-save", h.saveStatus).Methods(http.MethodPost) router.HandleFunc("/statuses/{id}/edit", h.showEditStatusView).Methods(http.MethodGet) router.HandleFunc("/statuses/{id}/update", h.updateStatus).Methods(http.MethodPost) - router.HandleFunc("/statuses/{id}/remove", h.handleRemoveStatus) + //router.HandleFunc("/statuses/{id}/remove", h.handleRemoveStatus) + //router.HandleFunc("/status-remove", h.handleRemoveStatus) + + router.PathPrefix("/").Handler(http.FileServer(http.Dir(cfg.AssetsDir))) return router, nil } diff --git a/web/handler/html.go b/web/handler/html.go index 48ef973..12f519c 100644 --- a/web/handler/html.go +++ b/web/handler/html.go @@ -6,7 +6,8 @@ var TplMap = map[string]string{ "confirm_remove_status": `{{ define "content" }} Are you sure you you want to delete the following status?

{{ .status.Content }}

-
+ +
{{ end }}`, @@ -35,19 +36,41 @@ Are you sure you you want to delete the following status? {{ end }}
- +
+ +
{{ end }}`, "index": `{{ define "content" }} -

Update your status

-{{ range .statuses }} -
-
{{ .User }}, {{ .TimeAgo }}
-

{{ .Content }}

-
-{{ end }} +
+
+

Set your status

+ {{ if .form.Error }} +

{{ .form.Error }}

+ {{ end }} + {{ if .flash }} +

{{ .flash }}

+ {{ end }} +
+
+ +
+
+ +
+
+
+

Status stream

+ {{ range .statuses }} + + {{ end }} +
+
{{ end }}`, "login": `{{ define "content" }}

Login

diff --git a/web/handler/html/common/layout.html b/web/handler/html/common/layout.html index 887e31b..b0a40df 100644 --- a/web/handler/html/common/layout.html +++ b/web/handler/html/common/layout.html @@ -4,13 +4,24 @@ - - Status + status cafe + + {{ template "head" . }} +
+

status.cafe

+
+
{{ template "content" . }} +
+ + + {{ end }} -{{ define "head" }}{{ end }} \ No newline at end of file +{{ define "head" }}{{ end }} diff --git a/web/handler/html/confirm_remove_status.html b/web/handler/html/confirm_remove_status.html index 5455ff5..de1344c 100644 --- a/web/handler/html/confirm_remove_status.html +++ b/web/handler/html/confirm_remove_status.html @@ -1,7 +1,8 @@ {{ define "content" }} Are you sure you you want to delete the following status?

{{ .status.Content }}

-
+ +
{{ end }} \ No newline at end of file diff --git a/web/handler/html/edit_status.html b/web/handler/html/edit_status.html index cb5ea9c..2200886 100644 --- a/web/handler/html/edit_status.html +++ b/web/handler/html/edit_status.html @@ -8,7 +8,9 @@ {{ end }}
- +
+ +
diff --git a/web/handler/html/index.html b/web/handler/html/index.html index 3b7e997..79b7797 100644 --- a/web/handler/html/index.html +++ b/web/handler/html/index.html @@ -1,9 +1,29 @@ {{ define "content" }} -

Update your status

-{{ range .statuses }} -
-
{{ .User }}, {{ .TimeAgo }}
-

{{ .Content }}

-
-{{ end }} +
+
+

Set your status

+ {{ if .form.Error }} +

{{ .form.Error }}

+ {{ end }} + {{ if .flash }} +

{{ .flash }}

+ {{ end }} +
+
+ +
+
+ +
+
+
+

Status stream

+ {{ range .statuses }} + + {{ end }} +
+
{{ end }} \ No newline at end of file diff --git a/web/handler/index_show.go b/web/handler/index_show.go index 3835aa7..f83230c 100644 --- a/web/handler/index_show.go +++ b/web/handler/index_show.go @@ -1,6 +1,7 @@ package handler import ( + "fmt" "net/http" ) @@ -10,12 +11,29 @@ type Update struct { } func (h *Handler) showIndexView(w http.ResponseWriter, r *http.Request) { + user, err := h.getUser(r) + if err != nil { + unauthorized(w) + return + } statuses, err := h.storage.LatestStatuses() if err != nil { serverError(w, err) return } + session, err := h.sess.Store.Get(r, "ichi") + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + flash := "" + if flashes := session.Flashes(); len(flashes) > 0 { + flash = flashes[0].(string) + } + session.Save(r, w) + fmt.Println(user) h.renderLayout(w, "index", map[string]interface{}{ "statuses": statuses, + "flash": flash, }, "") } diff --git a/web/handler/status_remove.go b/web/handler/status_remove.go index 19d2060..6d02481 100644 --- a/web/handler/status_remove.go +++ b/web/handler/status_remove.go @@ -2,6 +2,7 @@ package handler import ( "net/http" + "status/web/handler/form" ) func (h *Handler) handleRemoveStatus(w http.ResponseWriter, r *http.Request) { @@ -10,7 +11,8 @@ func (h *Handler) handleRemoveStatus(w http.ResponseWriter, r *http.Request) { unauthorized(w) return } - status, err := h.storage.StatusById(RouteInt64Param(r, "id")) + f := form.NewStatusForm(r) + status, err := h.storage.StatusById(f.Id) if err != nil { serverError(w, err) return diff --git a/web/handler/status_save.go b/web/handler/status_save.go index 7522fe0..7a56a94 100644 --- a/web/handler/status_save.go +++ b/web/handler/status_save.go @@ -39,5 +39,5 @@ func (h *Handler) saveStatus(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusInternalServerError) return } - http.Redirect(w, r, "/statuses/new", http.StatusFound) + http.Redirect(w, r, "/", http.StatusFound) }