Esempio n. 1
0
func ExampleHandle() {
	// root could be your router from gorilla/mux (or your router of
	// choice), or a subhandler if you want to use a path prefix like
	// /pkg/ or /go/.
	root := http.NotFoundHandler()
	http.Handle("/", goimport.Handle(root, goimport.Packages{
		{
			VCS:       "git",
			Path:      "repo1",
			TargetURL: "github.com/username/repo1",
		},
		{
			VCS:       "bzr",
			Path:      "repo2",
			TargetURL: "launchpad.net/repo2",
		},
	}, github.Packages{
		User:             "******",
		FilterByHomepage: "(http://)?j4k.co/",
	}))
}
Esempio n. 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
}