Skip to content

Commit

Permalink
set up yaml config for assets
Browse files Browse the repository at this point in the history
need to set up in this way, see:
spf13/viper#390 (comment)

also, only works for the layout for now, need to get other pages working with this config as well.

when it's local, this also enables static file server.
  • Loading branch information
tugberkugurlu committed Dec 23, 2020
1 parent 0637e28 commit b84fdd1
Show file tree
Hide file tree
Showing 524 changed files with 217,757 additions and 16 deletions.
81 changes: 70 additions & 11 deletions cmd/web/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,31 @@ import (
"github.com/gorilla/feeds"
"github.com/gorilla/mux"
"github.com/gosimple/slug"
"github.com/pkg/errors"
"github.com/spf13/viper"
"golang.org/x/net/html"
"gopkg.in/yaml.v2"
)

type Config struct {
AssetsUrl string
AssetsPrefix string
}

func (c Config) IsLocal() bool {
return strings.Index(c.AssetsUrl, "/") == 0
}

func (c Config) FullAssetsUrl() string {
if c.AssetsPrefix == "" {
return c.AssetsUrl
}
return fmt.Sprintf("%s/%s",
strings.TrimSuffix(c.AssetsUrl, "/"),
strings.TrimSuffix(c.AssetsPrefix, "/"),
)
}

type Post struct {
Body template.HTML
Images []string
Expand Down Expand Up @@ -66,13 +87,26 @@ func rankByTagCount(tagFrequencies map[string]*Tag) TagCountPairList {
return pl
}

var config Config
var layoutConfig LayoutConfig
var tagsList TagCountPairList
var posts []*Post
var postsBySlug map[string]*Post
var postsByTagSlug map[string][]*Post
var tagsBySlug map[string]*Tag

func main() {
var configErr error
config, configErr = parseConfig()
if configErr != nil {
log.Fatal(configErr)
}

layoutConfig = LayoutConfig{
AssetsUrl: config.FullAssetsUrl(),
GoogleAnalyticsTrackingId: os.Getenv("TUGBERKWEB_GoogleAnalytics__TrackingId"),
}

tagsBySlug = make(map[string]*Tag)
postsBySlug = make(map[string]*Post)
postsByTagSlug = make(map[string][]*Post)
Expand Down Expand Up @@ -215,7 +249,9 @@ func main() {
})

fs := http.FileServer(http.Dir("../../web/static"))
r.PathPrefix("/assets/").Handler(http.StripPrefix("/assets/", fs))
if config.IsLocal() {
r.PathPrefix("/assets/").Handler(http.StripPrefix("/assets/", fs))
}
r.PathPrefix("/content/images/").HandlerFunc(legacyBlogImagesRedirector)
r.HandleFunc("/archive/{slug}", blogPostPageHandler)
r.HandleFunc("/tags/{tag_slug}", tagsPageHandler)
Expand All @@ -237,6 +273,30 @@ func main() {
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", serverPortStr), CaselessMatcher(r)))
}

func parseConfig() (Config, error) {
viper.SetConfigType("yaml")
viper.SetConfigName("config")
viper.AddConfigPath("../../")
configErr := viper.ReadInConfig()
if configErr != nil {
return Config{}, fmt.Errorf("Fatal error config file: %s \n", configErr)
}
var assetsUrl string
if viperVal := viper.Get("assets_url"); viperVal == nil {
return Config{}, errors.New("Fatal error: 'assets_url' config value doesn't exist")
} else {
assetsUrl = viperVal.(string)
}
var assetsPrefix string
if viperVal := viper.Get("assets_prefix"); viperVal != nil {
assetsPrefix = viperVal.(string)
}
return Config{
AssetsUrl: assetsUrl,
AssetsPrefix: assetsPrefix,
}, nil
}

func CaselessMatcher(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.URL.Path = strings.ToLower(r.URL.Path)
Expand All @@ -256,6 +316,7 @@ type Layout struct {

type LayoutConfig struct {
GoogleAnalyticsTrackingId string
AssetsUrl string
}

type Home struct {
Expand Down Expand Up @@ -319,7 +380,7 @@ type Page interface {
}

func speakingPageHandler(w http.ResponseWriter, r *http.Request) {
ExecuteTemplate(w, r, []string{
ExecuteTemplate(w, r, layoutConfig, []string{
"../../web/template/speaking.html",
"../../web/template/shared/speaking-activity-card.html",
}, SpeakingPage{
Expand Down Expand Up @@ -384,7 +445,7 @@ func tagsPageHandler(w http.ResponseWriter, r *http.Request) {
return
}

ExecuteTemplate(w, r, []string{
ExecuteTemplate(w, r, layoutConfig, []string{
"../../web/template/tag.html",
"../../web/template/shared/post-item.html",
}, TagsPage{
Expand All @@ -407,7 +468,7 @@ func blogPostPageHandler(w http.ResponseWriter, r *http.Request) {
return
}

ExecuteTemplate(w, r, []string{"../../web/template/post.html"}, PostPage{
ExecuteTemplate(w, r, layoutConfig, []string{"../../web/template/post.html"}, PostPage{
Post: post,
AdTags: strings.Join(post.Metadata.Tags, ","),
})
Expand All @@ -421,12 +482,12 @@ func legacyBlogImagesRedirector(w http.ResponseWriter, r *http.Request) {

func staticPage(page string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
ExecuteTemplate(w, r, []string{fmt.Sprintf("../../web/template/%s.html", page)}, nil)
ExecuteTemplate(w, r, layoutConfig, []string{fmt.Sprintf("../../web/template/%s.html", page)}, nil)
}
}

func homeHandler(w http.ResponseWriter, r *http.Request) {
ExecuteTemplate(w, r, []string{
ExecuteTemplate(w, r, layoutConfig, []string{
"../../web/template/home.html",
"../../web/template/shared/post-item.html",
"../../web/template/shared/speaking-activity-card.html",
Expand All @@ -437,15 +498,15 @@ func homeHandler(w http.ResponseWriter, r *http.Request) {
}

func blogHomeHandler(w http.ResponseWriter, r *http.Request) {
ExecuteTemplate(w, r, []string{
ExecuteTemplate(w, r, layoutConfig, []string{
"../../web/template/blog.html",
"../../web/template/shared/post-item.html",
}, Blog{
Posts: posts,
})
}

func ExecuteTemplate(w http.ResponseWriter, r *http.Request, templatePaths []string, data interface{}) {
func ExecuteTemplate(w http.ResponseWriter, r *http.Request, config LayoutConfig, templatePaths []string, data interface{}) {
t := template.New("")
t = t.Funcs(template.FuncMap{"mod": func(i, j int) bool { return i%j == 0 }})
t, err := t.ParseFiles(append(templatePaths, "../../web/template/layout.html")...)
Expand Down Expand Up @@ -485,9 +546,7 @@ func ExecuteTemplate(w http.ResponseWriter, r *http.Request, templatePaths []str
Data: data,
Section: section,
AdTags: adTags,
Config: LayoutConfig{
GoogleAnalyticsTrackingId: os.Getenv("TUGBERKWEB_GoogleAnalytics__TrackingId"),
},
Config: config,
}
templateErr := t.ExecuteTemplate(w, "layout", pageContext)
if templateErr != nil {
Expand Down
2 changes: 2 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
assets_url: "/assets"
assets_prefix: ""
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ require (
github.com/gorilla/feeds v1.1.1
github.com/gorilla/mux v1.7.4
github.com/gosimple/slug v1.9.0
github.com/pkg/errors v0.8.1
github.com/spf13/viper v1.7.1
golang.org/x/net v0.0.0-20200506145744-7e3656a0809f
gopkg.in/yaml.v2 v2.2.8
)
Loading

0 comments on commit b84fdd1

Please sign in to comment.