src

Go monorepo.
git clone git://code.dwrz.net/src
Log | Files | Refs

new.go (2289B)


      1 package site
      2 
      3 import (
      4 	"bytes"
      5 	"fmt"
      6 
      7 	"code.dwrz.net/src/cmd/web/site/entry"
      8 	"code.dwrz.net/src/cmd/web/site/page"
      9 	"code.dwrz.net/src/cmd/web/site/templates"
     10 	"code.dwrz.net/src/pkg/log"
     11 )
     12 
     13 type Params struct {
     14 	Debug bool
     15 	Log   *log.Logger
     16 }
     17 
     18 func (p *Params) Validate() error {
     19 	if p.Log == nil {
     20 		return fmt.Errorf("missing logger")
     21 	}
     22 
     23 	return nil
     24 }
     25 
     26 func New(p Params) (*Site, error) {
     27 	if err := p.Validate(); err != nil {
     28 		return nil, fmt.Errorf("invalid params: %v", err)
     29 	}
     30 
     31 	var site = &Site{
     32 		debug: p.Debug,
     33 		files: static,
     34 		log:   p.Log,
     35 		pages: map[string]*bytes.Buffer{},
     36 	}
     37 
     38 	// Load the templates.
     39 	tmpl, err := templates.Parse()
     40 	if err != nil {
     41 		return nil, fmt.Errorf("failed to load templates: %v", err)
     42 	}
     43 	site.tmpl = tmpl
     44 
     45 	// Load the entries.
     46 	entries, err := entry.Load(entry.LoadParams{Log: p.Log})
     47 	if err != nil {
     48 		return nil, fmt.Errorf("failed to load entries: %v", err)
     49 	}
     50 
     51 	// Render contact page.
     52 	var contact = &bytes.Buffer{}
     53 	if err := site.tmpl.ExecuteTemplate(
     54 		contact, "base", &page.Contact{},
     55 	); err != nil {
     56 		return nil, fmt.Errorf("failed to exec template: %v", err)
     57 	}
     58 	site.pages["/contact/"] = contact
     59 
     60 	// Render cv page.
     61 	var cv = &bytes.Buffer{}
     62 	if err := site.tmpl.ExecuteTemplate(
     63 		cv, "base", &page.CV{},
     64 	); err != nil {
     65 		return nil, fmt.Errorf("failed to exec template: %v", err)
     66 	}
     67 	site.pages["/cv/"] = cv
     68 
     69 	// Render the home page.
     70 	var home = &bytes.Buffer{}
     71 	if err := site.tmpl.ExecuteTemplate(
     72 		home, "base", &page.Home{},
     73 	); err != nil {
     74 		return nil, fmt.Errorf("failed to exec template: %v", err)
     75 	}
     76 	site.pages["/"] = home
     77 
     78 	// Render the timeline page.
     79 	var timeline = &bytes.Buffer{}
     80 	if err := site.tmpl.ExecuteTemplate(
     81 		timeline, "base", &page.Timeline{
     82 			Years: entry.SortYear(entries),
     83 		},
     84 	); err != nil {
     85 		return nil, fmt.Errorf("failed to exec template: %v", err)
     86 	}
     87 	site.pages["/timeline/"] = timeline
     88 
     89 	// Render the entry pages.
     90 	for _, e := range entries {
     91 		var p = &bytes.Buffer{}
     92 		if err := site.tmpl.ExecuteTemplate(
     93 			p, "base", &page.Entry{Entry: e},
     94 		); err != nil {
     95 			return nil, fmt.Errorf(
     96 				"failed to exec template: %v", err,
     97 			)
     98 		}
     99 
    100 		path := fmt.Sprintf(
    101 			"/timeline/%s/", e.Date.Format(entry.DateFormat),
    102 		)
    103 		site.pages[path] = p
    104 	}
    105 
    106 	return site, nil
    107 }