From 003373834f2310b89ca23718dad5a60b5a954a61 Mon Sep 17 00:00:00 2001 From: m15o Date: Wed, 24 Nov 2021 07:30:19 +0100 Subject: [PATCH] Add endpoint to get status as json --- Makefile | 2 + web/handler/common.go | 1 - web/handler/handler.go | 15 ++++-- web/handler/html.go | 75 ++++++++++++++++++++++------- web/handler/html/common/layout.html | 1 - web/handler/html/create_status.html | 59 +++++++++++++++++------ web/handler/html/index.html | 5 +- web/handler/html/user.html | 11 +++++ web/handler/index_show.go | 7 --- web/handler/register.go | 2 +- web/handler/status_create.go | 16 +----- web/handler/status_edit.go | 2 - web/handler/status_save.go | 24 ++++----- web/handler/user_view.go | 52 ++++++++++++++++++++ web/web.go | 50 +++++++++---------- 15 files changed, 221 insertions(+), 101 deletions(-) create mode 100644 Makefile create mode 100644 web/handler/html/user.html create mode 100644 web/handler/user_view.go diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..22889c5 --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +build: + CGO_ENABLED=0 GOOS=linux go build -o bin/statuscafe main.go diff --git a/web/handler/common.go b/web/handler/common.go index 33b306b..692b426 100644 --- a/web/handler/common.go +++ b/web/handler/common.go @@ -22,7 +22,6 @@ var TplCommonMap = map[string]string{ {{ template "content" . }} diff --git a/web/handler/handler.go b/web/handler/handler.go index aa55a7d..84871c1 100644 --- a/web/handler/handler.go +++ b/web/handler/handler.go @@ -64,21 +64,26 @@ func New(cfg *config.Config, sess *session.Session, data *storage.Storage) (http } h.initTpl() - // Index router.HandleFunc("/", h.showIndexView).Methods(http.MethodGet) router.HandleFunc("/login", h.showLoginView).Methods(http.MethodGet) router.HandleFunc("/check-login", h.checkLogin).Methods(http.MethodPost) router.HandleFunc("/register", h.handleRegister) router.HandleFunc("/logout", h.logout).Methods(http.MethodGet) - //router.HandleFunc("/statuses/new", h.showNewStatusView).Methods(http.MethodGet) + router.HandleFunc("/status-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("/status-remove", h.handleRemoveStatus) - router.PathPrefix("/").Handler(http.FileServer(http.Dir(cfg.AssetsDir))) + router.HandleFunc("/users/{user}", h.showUserView).Methods(http.MethodGet) + router.HandleFunc("/users/{user}/status.json", h.showUserStatusView).Methods(http.MethodGet) + router.PathPrefix("/assets/").Handler( + http.StripPrefix("/assets/", + http.FileServer( + http.Dir(cfg.AssetsDir), + ), + ), + ) return router, nil } diff --git a/web/handler/html.go b/web/handler/html.go index 12f519c..4157482 100644 --- a/web/handler/html.go +++ b/web/handler/html.go @@ -11,21 +11,50 @@ Are you sure you you want to delete the following status? {{ end }}`, - "create_status": `{{ define "content" }} -

New status

-{{ if .form.Error }} -

{{ .form.Error }}

-{{ end }} -{{ if .flash }} -

{{ .flash }}

-{{ end }} -
-
- -
- -
-{{ end }}`, + "create_status": ` + + + + + status cafe + + + +
+

New status

+ {{ if .form.Error }} +

{{ .form.Error }}

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

{{ .flash }}

+ {{ end }} +
+
+ +
+
+ +
+
+ + + +`, "edit_status": `{{ define "content" }}

Edit status

{{ if .form.Error }} @@ -55,17 +84,18 @@ Are you sure you you want to delete the following status? {{ end }}
- +

+

Drag to your bookmarks to update from anywhere: Set status

Status stream

{{ range .statuses }} {{ end }} @@ -111,5 +141,16 @@ Are you sure you you want to delete the following status?
+{{ end }}`, + "user": `{{ define "content" }} +
+

{{ .user }}

+ {{ range .statuses }} +
+
{{ .TimeAgo }}
+

{{ .Content }}

+
+ {{ end }} +
{{ end }}`, } diff --git a/web/handler/html/common/layout.html b/web/handler/html/common/layout.html index b0a40df..48a31ec 100644 --- a/web/handler/html/common/layout.html +++ b/web/handler/html/common/layout.html @@ -17,7 +17,6 @@ {{ template "content" . }}
- This is the footer
diff --git a/web/handler/html/create_status.html b/web/handler/html/create_status.html index 5348955..f77d2a4 100644 --- a/web/handler/html/create_status.html +++ b/web/handler/html/create_status.html @@ -1,15 +1,44 @@ -{{ define "content" }} -

New status

-{{ if .form.Error }} -

{{ .form.Error }}

-{{ end }} -{{ if .flash }} -

{{ .flash }}

-{{ end }} -
-
- -
- -
-{{ end }} \ No newline at end of file + + + + + + status cafe + + + +
+

New status

+ {{ if .form.Error }} +

{{ .form.Error }}

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

{{ .flash }}

+ {{ end }} +
+
+ +
+
+ +
+
+ + + + \ No newline at end of file diff --git a/web/handler/html/index.html b/web/handler/html/index.html index 79b7797..91fec5b 100644 --- a/web/handler/html/index.html +++ b/web/handler/html/index.html @@ -10,17 +10,18 @@ {{ end }}
- +

+

Drag to your bookmarks to update from anywhere: Set status

Status stream

{{ range .statuses }} {{ end }} diff --git a/web/handler/html/user.html b/web/handler/html/user.html new file mode 100644 index 0000000..dada5dd --- /dev/null +++ b/web/handler/html/user.html @@ -0,0 +1,11 @@ +{{ define "content" }} +
+

{{ .user }}

+ {{ range .statuses }} +
+
{{ .TimeAgo }}
+

{{ .Content }}

+
+ {{ end }} +
+{{ end }} \ No newline at end of file diff --git a/web/handler/index_show.go b/web/handler/index_show.go index f83230c..688971e 100644 --- a/web/handler/index_show.go +++ b/web/handler/index_show.go @@ -1,7 +1,6 @@ package handler import ( - "fmt" "net/http" ) @@ -11,11 +10,6 @@ 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) @@ -31,7 +25,6 @@ func (h *Handler) showIndexView(w http.ResponseWriter, r *http.Request) { 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/register.go b/web/handler/register.go index 4c5224a..09d718a 100644 --- a/web/handler/register.go +++ b/web/handler/register.go @@ -112,6 +112,6 @@ func (h *Handler) register(w http.ResponseWriter, r *http.Request) { serverError(w, err) return } - h.renderLayout(w, "index", nil, user.Name) + http.Redirect(w, r, "/", http.StatusFound) } } diff --git a/web/handler/status_create.go b/web/handler/status_create.go index 3ad1797..6786267 100644 --- a/web/handler/status_create.go +++ b/web/handler/status_create.go @@ -3,22 +3,10 @@ package handler import "net/http" func (h *Handler) showNewStatusView(w http.ResponseWriter, r *http.Request) { - user, err := h.getUser(r) + _, err := h.getUser(r) if err != nil { unauthorized(w) 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) - h.renderLayout(w, "create_status", map[string]interface{}{ - "flash": flash, - }, user) + h.view("create_status").Execute(w, nil) } diff --git a/web/handler/status_edit.go b/web/handler/status_edit.go index f09f930..518d6f6 100644 --- a/web/handler/status_edit.go +++ b/web/handler/status_edit.go @@ -12,11 +12,9 @@ func RouteInt64Param(r *http.Request, param string) int64 { if err != nil { return 0 } - if value < 0 { return 0 } - return value } diff --git a/web/handler/status_save.go b/web/handler/status_save.go index 7a56a94..1f4b13d 100644 --- a/web/handler/status_save.go +++ b/web/handler/status_save.go @@ -28,16 +28,18 @@ func (h *Handler) saveStatus(w http.ResponseWriter, r *http.Request) { serverError(w, err) return } - session, err := h.sess.Store.Get(r, "ichi") - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return + if r.URL.Query().Get("silent") != "1" { + session, err := h.sess.Store.Get(r, "ichi") + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + session.AddFlash("Status updated!") + err = session.Save(r, w) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + http.Redirect(w, r, "/", http.StatusFound) } - session.AddFlash("Status updated!") - err = session.Save(r, w) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - http.Redirect(w, r, "/", http.StatusFound) } diff --git a/web/handler/user_view.go b/web/handler/user_view.go new file mode 100644 index 0000000..e915c72 --- /dev/null +++ b/web/handler/user_view.go @@ -0,0 +1,52 @@ +package handler + +import ( + "encoding/json" + "github.com/gorilla/mux" + "net/http" +) + +func (h *Handler) showUserView(w http.ResponseWriter, r *http.Request) { + user := mux.Vars(r)["user"] + if !h.storage.UserExists(user) { + notFound(w) + return + } + statuses, err := h.storage.StatusByUsername(mux.Vars(r)["user"], 20, 0) + if err != nil { + serverError(w, err) + return + } + h.renderLayout(w, "user", map[string]interface{}{ + "user": user, + "statuses": statuses, + }, "") +} + +type statusjson struct { + Author string `json:"author"` + Content string `json:"content"` + TimeAgo string `json:"time_ago"` +} + +func (h *Handler) showUserStatusView(w http.ResponseWriter, r *http.Request) { + user := mux.Vars(r)["user"] + if !h.storage.UserExists(user) { + notFound(w) + return + } + statuses, err := h.storage.StatusByUsername(mux.Vars(r)["user"], 1, 0) + if err != nil { + serverError(w, err) + return + } + w.Header().Set("Access-Control-Allow-Origin", "*") + w.Header().Set("Content-Type", "application/json") + var res statusjson + if len(statuses) > 0 { + res.Author = statuses[0].User + res.Content = statuses[0].Content + res.TimeAgo = statuses[0].TimeAgo() + } + json.NewEncoder(w).Encode(res) +} diff --git a/web/web.go b/web/web.go index 34473e6..1588669 100644 --- a/web/web.go +++ b/web/web.go @@ -10,15 +10,15 @@ import ( "status/web/session" ) -func httpToHTTPSHandler() *http.ServeMux { - handleRedirect := func(w http.ResponseWriter, r *http.Request) { - newURI := "https://" + r.Host + r.URL.String() - http.Redirect(w, r, newURI, http.StatusFound) - } - mux := &http.ServeMux{} - mux.HandleFunc("/", handleRedirect) - return mux -} +//func httpToHTTPSHandler() *http.ServeMux { +// handleRedirect := func(w http.ResponseWriter, r *http.Request) { +// newURI := "https://" + r.Host + r.URL.String() +// http.Redirect(w, r, newURI, http.StatusFound) +// } +// mux := &http.ServeMux{} +// mux.HandleFunc("/", handleRedirect) +// return mux +//} func Serve(data *storage.Storage, cfg *config.Config) error { var err error @@ -27,21 +27,21 @@ func Serve(data *storage.Storage, cfg *config.Config) error { if err != nil { log.Fatal(err) } - switch cfg.Env { - case "PROD": - go func() { - fmt.Printf("Starting HTTP server on :443\n") - err := http.ListenAndServeTLS(":443", cfg.CertFile, cfg.KeyFile, s) - if err != nil { - log.Fatalf("httpsSrv.ListendAndServeTLS() failed with %s", err) - } - }() - fmt.Printf("Starting HTTP to HTTPS server on :80\n") - err = http.ListenAndServe(":80", httpToHTTPSHandler()) - break - default: - fmt.Printf("Starting HTTP server on port 8000\n") - err = http.ListenAndServe(":8000", s) - } + //switch cfg.Env { + //case "PROD": + // go func() { + // fmt.Printf("Starting HTTP server on :443\n") + // err := http.ListenAndServeTLS(":443", cfg.CertFile, cfg.KeyFile, s) + // if err != nil { + // log.Fatalf("httpsSrv.ListendAndServeTLS() failed with %s", err) + // } + // }() + // fmt.Printf("Starting HTTP to HTTPS server on :80\n") + // err = http.ListenAndServe(":80", httpToHTTPSHandler()) + // break + //default: + fmt.Printf("Starting HTTP server on port 8000\n") + err = http.ListenAndServe(":8000", s) + //} return err }