Skip to content

Commit

Permalink
feat: profile page
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Jul 7, 2020
1 parent 6cdc192 commit 5a30662
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
1 change: 1 addition & 0 deletions pkg/sgtm/driver_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ func (svc *Service) httpServer() (*http.Server, error) {
{
r.Get("/", svc.indexPage(box))
r.Get("/settings", svc.settingsPage(box))
r.Get("/@{user_slug}", svc.profilePage(box))
}

// auth
Expand Down
39 changes: 36 additions & 3 deletions pkg/sgtm/http_pages.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/Masterminds/sprig"
"github.com/go-chi/chi"
packr "github.com/gobuffalo/packr/v2"
"go.uber.org/zap"
"moul.io/sgtm/pkg/sgtmpb"
Expand All @@ -22,7 +23,6 @@ func (svc *Service) indexPage(box *packr.Box) func(w http.ResponseWriter, r *htt
svc.errRenderHTML(w, r, err, http.StatusUnprocessableEntity)
return
}
// FIXME: load homepage stuff
if svc.opts.DevMode {
tmpl = loadTemplate(box, "index.tmpl.html")
}
Expand All @@ -43,7 +43,6 @@ func (svc *Service) settingsPage(box *packr.Box) func(w http.ResponseWriter, r *
svc.errRenderHTML(w, r, err, http.StatusUnprocessableEntity)
return
}
// FIXME: load homepage stuff
if svc.opts.DevMode {
tmpl = loadTemplate(box, "settings.tmpl.html")
}
Expand All @@ -55,6 +54,32 @@ func (svc *Service) settingsPage(box *packr.Box) func(w http.ResponseWriter, r *
}
}

func (svc *Service) profilePage(box *packr.Box) func(w http.ResponseWriter, r *http.Request) {
tmpl := loadTemplate(box, "profile.tmpl.html")
return func(w http.ResponseWriter, r *http.Request) {
started := time.Now()
data, err := svc.newTemplateData(r)
if err != nil {
svc.errRenderHTML(w, r, err, http.StatusUnprocessableEntity)
return
}

userSlug := chi.URLParam(r, "user_slug")
if err := svc.db.Where(sgtmpb.User{Slug: userSlug}).First(&data.Profile.User).Error; err != nil {
data.Error = err.Error()
}

if svc.opts.DevMode {
tmpl = loadTemplate(box, "profile.tmpl.html")
}
data.Duration = time.Since(started)
if err := tmpl.Execute(w, &data); err != nil {
svc.errRenderHTML(w, r, err, http.StatusUnprocessableEntity)
return
}
}
}

func (svc *Service) newTemplateData(r *http.Request) (templateData, error) {
data := templateData{
Title: "SGTM",
Expand Down Expand Up @@ -91,7 +116,6 @@ func loadTemplate(box *packr.Box, filepath string) *template.Template {
strings.TrimSpace(src),
strings.TrimSpace(base),
}, "\n")
fmt.Println(allInOne)
tmpl, err := template.New("index").Funcs(sprig.FuncMap()).Parse(allInOne)
if err != nil {
panic(err)
Expand All @@ -100,6 +124,8 @@ func loadTemplate(box *packr.Box, filepath string) *template.Template {
}

type templateData struct {
// common

Title string
Date time.Time
JWTToken string
Expand All @@ -108,4 +134,11 @@ type templateData struct {
Opts Opts
Lang string
User sgtmpb.User
Error string

// specific

Index struct{}
Settings struct{}
Profile struct{ User sgtmpb.User }
}
12 changes: 11 additions & 1 deletion static/base.tmpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,21 @@ <h4 class="text-white">@{{.User.Slug}}</h4>
</div>
</header>

{{if .Error}}
<section>
<div class="container">
<div class="row alert alert-danger" role="alert">
{{.Error}}
</div>
</div>
</section>
{{end}}

<main role="main">
{{template "content" .}}
</main>

<footer class="text-muted">
<footer class="page-footer text-muted bg-light">
<div class="container">
<p class="float-right">
<a href="#">Back to top</a>
Expand Down
13 changes: 13 additions & 0 deletions static/profile.tmpl.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{{ template "base" . }}

{{define "content"}}
<div class="container">
<div class="row">
{{if .Profile.User.Slug}}
<code><pre>{{.Profile.User|toPrettyJson}}</pre></code>
{{else}}
No such user.
{{end}}
</div>
</div>
{{end}}

0 comments on commit 5a30662

Please sign in to comment.