From d8bbb75de36ce129246c0750b2ffb96b8f9e9c1f Mon Sep 17 00:00:00 2001 From: Manfred Touron Date: Sun, 27 Jan 2019 07:46:00 +0100 Subject: [PATCH] feat: add track page --- templates/layout/base.tmpl | 30 ++++++++++++------------- templates/old/track.html | 41 ---------------------------------- templates/track.tmpl | 45 ++++++++++++++++++++++++++++++++++++++ views/funcmap.go | 30 +++++++++++++++++-------- views/views.go | 21 +++++++++++++++++- 5 files changed, 101 insertions(+), 66 deletions(-) delete mode 100644 templates/old/track.html create mode 100644 templates/track.tmpl diff --git a/templates/layout/base.tmpl b/templates/layout/base.tmpl index f02c2f4e..029b4d91 100644 --- a/templates/layout/base.tmpl +++ b/templates/layout/base.tmpl @@ -17,10 +17,10 @@ - - - - + + + + @@ -51,13 +51,13 @@ - + {{ block "extra_css" . }}{{ end }} @@ -217,14 +217,14 @@ {{ if neige }}{{end}} - - - - - - - - {{ if neige }}{{end}} + + + + + + + + {{ if neige }}{{end}} {{ block "extra_js" . }}{{ end }} diff --git a/templates/old/track.html b/templates/old/track.html deleted file mode 100644 index b628e75c..00000000 --- a/templates/old/track.html +++ /dev/null @@ -1,41 +0,0 @@ -{% set active_page = 'muzik' %} -{% extends "base.html" %} -{% set page_title = track.title %} - -{% block title %}Du son {{ mot_cool() }}{% endblock %} -{% block subtitle %}{{track.title}}{% endblock %} - -{% set layout = 'two_columns' %} -{% set track_url_split = track.url.split('/') %} - -{% block main_column %} - - -
{{ track.description | urlize(50, true) }}
-{% endblock %} - -{% block secondary_column %} - - {{ track.title }} - - -
- {{ track.created_at }} -
- - {# FIXME: handle tags etc in html headers for SEO #} - -
- {% for tag in track.tags %} - #{{ tag }} - {% endfor %} -
- -{% endblock %} diff --git a/templates/track.tmpl b/templates/track.tmpl new file mode 100644 index 00000000..9b243bc1 --- /dev/null +++ b/templates/track.tmpl @@ -0,0 +1,45 @@ +{{define "title"}}{{.track.Title}}{{end}} +{{define "h1"}}Du son {{ mot_cool }}{{end}} +{{define "h1small"}}{{.track.Title}}{{end}} + +{{define "content"}} + + + + + + {{ if .track.Description }} +
{{ .track.Description | linkify }}
+ {{end}} +{{end}} + +{{/* +{% block main_column %} +{% endblock %} + +{% block secondary_column %} + + {{ track.title }} + + +
+ {{ track.created_at }} +
+ + {# FIXME: handle tags etc in html headers for SEO #} + +
+ {% for tag in track.tags %} + #{{ tag }} + {% endfor %} +
+ +{% endblock %} +*/}} diff --git a/views/funcmap.go b/views/funcmap.go index e622ed77..7bc70fa5 100644 --- a/views/funcmap.go +++ b/views/funcmap.go @@ -36,10 +36,12 @@ func getFuncmap(opts *Options) *ctxFuncmap { fm["invalid_cache"] = f.invalidCache fm["logo_alternate"] = random.AlternateLogo fm["mot_du_jour"] = random.WOTD + fm["mot_cool"] = random.MotCool fm["megahertz"] = f.megahertz fm["mot_debile_qui_se_mange"] = random.MotDebileQuiSeMange fm["neige"] = func() bool { return false } fm["cache_external_asset"] = f.cacheExternalAsset + fm["linkify"] = f.linkify f.fm = fm return f } @@ -50,6 +52,11 @@ type ctxFuncmap struct { req *http.Request } +func (f *ctxFuncmap) linkify(input string) string { + // FIXME: replace URLs with links + return input +} + func (f *ctxFuncmap) devel() bool { return f.opts.Debug } func (f *ctxFuncmap) sharingImageURL() string { @@ -61,16 +68,17 @@ func (f *ctxFuncmap) activePage() string { } func (f *ctxFuncmap) activeMenu() string { - switch f.req.URL.Path { - case "/": + switch path := f.req.URL.Path; { + case path == "/": return "home" - case "/muzik": - // FIXME: support albums, songs + case path == "/muzik" || + strings.HasPrefix(f.req.URL.Path, "/track/") || + strings.HasPrefix(f.req.URL.Path, "/album/"): return "muzik" - case "/copaings": + case path == "/copaings": return "copaings" - case "/hackz": - // FIXME: support hackz URLs + case path == "/hackz" || + strings.HasPrefix(f.req.URL.Path, "/hackz"): return "hackz" default: return "home" @@ -118,6 +126,10 @@ func (f *ctxFuncmap) cacheExternalAsset(input string) (string, error) { func (f *ctxFuncmap) resize(opts ...string) string { path := opts[len(opts)-1] opts = opts[:len(opts)-1] + urlAppend := "" + if f.opts.Debug { + urlAppend += "?src=" + path + } var err error if path, err = f.cacheExternalAsset(path); err != nil { @@ -131,7 +143,7 @@ func (f *ctxFuncmap) resize(opts ...string) string { newpath := fmt.Sprintf("./static/img/cache/%x%s", h, filepath.Ext(path)) if _, err := os.Stat(newpath); !os.IsNotExist(err) { - return strings.Replace(newpath, "./static/", "./", -1) + return strings.Replace(newpath, "./static/", "/", -1) + urlAppend } logger := zap.L().With( @@ -184,7 +196,7 @@ func (f *ctxFuncmap) resize(opts ...string) string { logger.Warn("failed to save resized image", zap.Error(err)) return path } - return strings.Replace(newpath, "./static/", "./", -1) + return strings.Replace(newpath, "./static/", "/", -1) + urlAppend } func (f *ctxFuncmap) yomymanStyle() string { diff --git a/views/views.go b/views/views.go index d028e0cf..fd0b42ba 100644 --- a/views/views.go +++ b/views/views.go @@ -2,6 +2,7 @@ package views import ( "net/http" + "strconv" "sync" "github.com/gorilla/mux" @@ -24,6 +25,7 @@ func Setup(opts *Options) error { opts.Router.HandleFunc("/muzik", handlers.muzikHandler) opts.Router.HandleFunc("/hackz", handlers.hackzHandler) opts.Router.HandleFunc("/copaings", handlers.copaingsHandler) + opts.Router.HandleFunc("/track/{track_id:[0-9]+}", handlers.trackHandler) // // old routes (to be imported) @@ -44,7 +46,6 @@ func Setup(opts *Options) error { // /hackz/moijaime // /hackz/phazms GET/POST // /album/ - // /track/ // /scorz/inc/// // /sitemap.xml @@ -92,6 +93,24 @@ func (h *handlers) muzikHandler(w http.ResponseWriter, r *http.Request) { h.render(w, r, "muzik.tmpl", data) } +func (h *handlers) trackHandler(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + trackId, err := strconv.ParseUint(vars["track_id"], 10, 64) + if err != nil { + h.renderError(w, r, err) + return + } + track, err := h.opts.Svc.SoundcloudTrack(r.Context(), &api.SoundcloudTrackInput{ + TrackId: trackId, + }) + if err != nil { + h.renderError(w, r, err) + return + } + data := renderData{"track": track} + h.render(w, r, "track.tmpl", data) +} + func (h *handlers) copaingsHandler(w http.ResponseWriter, r *http.Request) { h.setDefaultHeaders(w) data := renderData{"friends": crew.CALC.Friends}