Example #1
0
func (h *articleHandler) read() error {
	glob := filepath.Join(h.dir, "*.md")
	matches, err := filepath.Glob(glob)
	if err != nil {
		return err
	}
	h.m = make(map[string]http.Handler, len(matches))
	h.slice = make([]article, len(matches))
	for i, file := range matches {
		base := filepath.Base(file)
		ext := filepath.Ext(base)
		withoutExt := base[:len(base)-len(ext)]
		rel, err := filepath.Rel("content", file)
		if err != nil {
			return err
		}
		p := pages.Static(rel, &h.slice[i])
		h.slice[i].Path = "/" + withoutExt
		h.m[withoutExt] = p
	}
	return nil
}
Example #2
0
// this is all kind of dumb. j4k.co/pages is an old idea and is a bit
// unwieldy. probably could be simplified. need to take a deep look at
// similar packages that could replace it, or just use straight up
// html/template.
func assemble() http.Handler {
	assetMap := map[string]string{}
	pages.NewDefault("content", "content/layouts")
	pages.Funcs(template.FuncMap{
		"cdn": func(s string) string {
			return "/" + s
		},
		"asset": func(s string) string {
			m, ok := assetMap[s]
			if !ok {
				return s
			}
			return m
		},
	})
	if production {
		pages.SetPrecache(true)
	}

	j4kco := mux.NewRouter().StrictSlash(true)
	j4kco.Handle("/", http.RedirectHandler("http://james4k.com", 302))

	notfound := pages.Dynamic("404.html", &notFoundHandler{})
	//articles := pages.Dir("articles", nil, notfound)
	articles := collectArticles("content/articles", notfound)
	root := mux.NewRouter().StrictSlash(true)
	if production {
		j4kcoAndPkgs := goimport.Handle(j4kco, github.Packages{
			User:             "******",
			FilterByHomepage: "^(http://)?j4k.co",
		})
		root.Host("j4k.co").Handler(j4kcoAndPkgs)
	}
	root.Handle("/", pages.Static("index.html", map[string]interface{}{
		"articles": articles.Collection(),
	}))
	root.Handle("/works", pages.Static("works.html", nil))
	//root.Handle("/games", pages.Static("games.html", nil))
	//root.Handle("/software", pages.Static("software.html", nil))
	assets := root.PathPrefix("/assets/")
	root.Handle("/{any}", articles)
	root.NotFoundHandler = notfound

	if production {
		assetMap = mender.VersionMap("content/mend-versions.json")
		assets.Handler(http.FileServer(http.Dir("content")))
	} else {
		assetServer := mender.Watch("content/mend.json", "content",
			http.FileServer(http.Dir("content")), os.Stderr)
		assetServer.OnChange = func() {
			assetMap = assetServer.VersionMap()
			log.Println("--- mend ---")
			for _, v := range assetMap {
				log.Println(v)
			}
			log.Println("------------")
		}
		assets.Handler(assetServer)
	}
	return root
}