diff options
Diffstat (limited to 'cmd/server')
| -rw-r--r-- | cmd/server/app.go | 63 | ||||
| -rw-r--r-- | cmd/server/feed.go | 9 | ||||
| -rw-r--r-- | cmd/server/handlers.go | 30 | ||||
| -rw-r--r-- | cmd/server/main.go | 18 |
4 files changed, 79 insertions, 41 deletions
diff --git a/cmd/server/app.go b/cmd/server/app.go new file mode 100644 index 0000000..743a389 --- /dev/null +++ b/cmd/server/app.go @@ -0,0 +1,63 @@ +package main + +import ( + "os" + + "gopkg.in/yaml.v3" + "riedstra.dev/mitch/go-website/page" +) + +var FeedPrefixDefault = ".feeds" + +type App struct { + ReIndexPath string + StaticDirectory string + BaseTemplate string + DocumentSplit string + Suffix string + + // Related to the Atom feed + Title string + Description string // aka, "subtitle" + Author Author + SiteURL string + FeedId string + Updated page.PageTime + FeedPrefix string +} + +func loadConf(fn string) (*App, error) { + fh, err := os.Open(fn) + if err != nil { + return nil, err + } + dec := yaml.NewDecoder(fh) + + app := &App{} + err = dec.Decode(app) + if err != nil { + return nil, err + } + + if app.StaticDirectory == "" { + app.StaticDirectory = "static" + } + if app.FeedPrefix == "" { + app.FeedPrefix = FeedPrefixDefault + } + if app.BaseTemplate != "" { + page.BaseTemplate = app.BaseTemplate + } + if app.DocumentSplit != "" { + page.DocumentSplit = app.DocumentSplit + } + if app.Suffix != "" { + page.Suffix = app.Suffix + } + + page.Global = map[string]interface{}{ + "App": app, + } + + return app, nil +} diff --git a/cmd/server/feed.go b/cmd/server/feed.go index 2d4a75b..073097b 100644 --- a/cmd/server/feed.go +++ b/cmd/server/feed.go @@ -189,6 +189,15 @@ func (a *App) FeedHandler(w http.ResponseWriter, r *http.Request) { Links: []Link{Link{Href: strings.Join([]string{a.SiteURL, p.Path()}, "/")}}, } + if p.AuthorName != "" { + entry.Author = &Author{ + Name: p.AuthorName, + } + if p.AuthorEmail != "" { + entry.Author.Email = p.AuthorEmail + } + } + if addContent { entry.Content = &Content{Type: "html", Data: content.String()} } diff --git a/cmd/server/handlers.go b/cmd/server/handlers.go index 5ea89cd..cb63774 100644 --- a/cmd/server/handlers.go +++ b/cmd/server/handlers.go @@ -9,20 +9,13 @@ import ( "riedstra.dev/mitch/go-website/page" ) -var FeedPrefixDefault = ".feeds" - -type App struct { - ReIndexPath string - StaticDirectory string - - // Related to the Atom feed - Title string - Description string // aka, "subtitle" - Author Author - SiteURL string - FeedId string - Updated page.PageTime - FeedPrefix string +func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request) { + rtr := mux.NewRouter() + rtr.HandleFunc(a.ReIndexPath, a.RebuildIndexHandler) + rtr.PathPrefix("/static/").Handler(a.StaticHandler()) + rtr.PathPrefix(fmt.Sprintf("/%s/{tag}", a.FeedPrefix)).HandlerFunc(a.FeedHandler) + rtr.PathPrefix("/").HandlerFunc(a.PageHandler) + rtr.ServeHTTP(w, r) } func (a *App) PageHandler(w http.ResponseWriter, r *http.Request) { @@ -55,12 +48,3 @@ func (a *App) RebuildIndexHandler(w http.ResponseWriter, r *http.Request) { func (a *App) StaticHandler() http.Handler { return http.StripPrefix("/static/", http.FileServer(http.Dir(a.StaticDirectory))) } - -func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request) { - rtr := mux.NewRouter() - rtr.HandleFunc(a.ReIndexPath, a.RebuildIndexHandler) - rtr.PathPrefix("/static/").Handler(a.StaticHandler()) - rtr.PathPrefix(fmt.Sprintf("/%s/{tag}", a.FeedPrefix)).HandlerFunc(a.FeedHandler) - rtr.PathPrefix("/").HandlerFunc(a.PageHandler) - rtr.ServeHTTP(w, r) -} diff --git a/cmd/server/main.go b/cmd/server/main.go index 4b2c1ad..00aecbf 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -19,18 +19,6 @@ func VersionPrint() { os.Exit(0) } -func loadConf(fn string) (*App, error) { - fh, err := os.Open(fn) - if err != nil { - return nil, err - } - dec := yaml.NewDecoder(fh) - - app := &App{} - err = dec.Decode(app) - return app, err -} - func main() { fl := flag.NewFlagSet("Website", flag.ExitOnError) listen := fl.String("l", "0.0.0.0:8001", "Listening address") @@ -61,12 +49,6 @@ func main() { if app.ReIndexPath == "" || *indexPath != defaultIndexPath { app.ReIndexPath = *indexPath } - if app.StaticDirectory == "" { - app.StaticDirectory = "static" - } - if app.FeedPrefix == "" { - app.FeedPrefix = FeedPrefixDefault - } if *verbose { b, _ := yaml.Marshal(app) |
