Beispiel #1
0
func main() {
	flag.Parse()
	log.Printf("Starting server, os.Args=%s", strings.Join(os.Args, " "))
	if err := readSecrets(); err != nil {
		log.Fatal(err)
	}

	if *srcZip != "" {
		r, err := zip.OpenReader(*srcZip)
		if err != nil {
			log.Fatal(err)
		}
		for _, f := range r.File {
			if strings.HasPrefix(f.Name, "root/") {
				srcFiles[f.Name[len("root/"):]] = f
			}
		}
	}

	if err := parseHTMLTemplates([][]string{
		{"about.html", "common.html", "layout.html"},
		{"bot.html", "common.html", "layout.html"},
		{"cmd.html", "common.html", "layout.html"},
		{"dir.html", "common.html", "layout.html"},
		{"home.html", "common.html", "layout.html"},
		{"importers.html", "common.html", "layout.html"},
		{"imports.html", "common.html", "layout.html"},
		{"file.html", "common.html", "layout.html"},
		{"index.html", "common.html", "layout.html"},
		{"notfound.html", "common.html", "layout.html"},
		{"pkg.html", "common.html", "layout.html"},
		{"results.html", "common.html", "layout.html"},
		{"std.html", "common.html", "layout.html"},
		{"subrepo.html", "common.html", "layout.html"},
		{"graph.html", "common.html"},
	}); err != nil {
		log.Fatal(err)
	}

	if err := parseTextTemplates([][]string{
		{"cmd.txt", "common.txt"},
		{"dir.txt", "common.txt"},
		{"home.txt", "common.txt"},
		{"notfound.txt", "common.txt"},
		{"pkg.txt", "common.txt"},
		{"results.txt", "common.txt"},
		{"opensearch.xml"},
	}); err != nil {
		log.Fatal(err)
	}

	if err := parsePresentTemplates([][]string{
		{".article", "article.tmpl", "action.tmpl"},
		{".slide", "slides.tmpl", "action.tmpl"},
	}); err != nil {
		log.Fatal(err)
	}

	present.PlayEnabled = true

	var err error
	db, err = database.New()
	if err != nil {
		log.Fatal(err)
	}

	go runBackgroundTasks()

	staticConfig := &web.StaticConfig{
		Header:      web.Header{web.HeaderCacheControl: {"public, max-age=3600"}},
		Directory:   *assetsDir,
		GzDirectory: *gzAssetsDir,
	}
	presentStaticConfig := &web.StaticConfig{
		Header:    web.Header{web.HeaderCacheControl: {"public, max-age=3600"}},
		Directory: *presentDir,
	}

	h := web.NewHostRouter()

	r := web.NewRouter()
	r.Add("/").GetFunc(servePresentHome)
	r.Add("/compile").PostFunc(serveCompile)
	r.Add("/favicon.ico").Get(staticConfig.FileHandler("favicon.ico"))
	r.Add("/google3d2f3cd4cc2bb44b.html").Get(staticConfig.FileHandler("google3d2f3cd4cc2bb44b.html"))
	r.Add("/humans.txt").Get(staticConfig.FileHandler("humans.txt"))
	r.Add("/play.js").Get(dataHandler("play.js", "text/javascript", *presentDir, "js/jquery.js", "js/playground.js", "js/play.js"))
	r.Add("/robots.txt").Get(staticConfig.FileHandler("presentRobots.txt"))
	r.Add("/static/<path:.*>").Get(presentStaticConfig.DirectoryHandler("static"))
	if *redirGoTalks {
		r.Add("/code.google.com/p/go.talks/<path:.+>").GetFunc(func(resp web.Response, req *web.Request) error {
			return web.Redirect(resp, req, "http://talks.golang.org/"+req.RouteVars["path"], 301, nil)
		})
	}
	r.Add("/<path:.+>").GetFunc(servePresentation)

	h.Add("talks.<:.*>", web.ErrorHandler(handlePresentError, web.FormAndCookieHandler(6000, false, r)))

	r = web.NewRouter()
	r.Add("/favicon.ico").Get(staticConfig.FileHandler("favicon.ico"))
	r.Add("/google3d2f3cd4cc2bb44b.html").Get(staticConfig.FileHandler("google3d2f3cd4cc2bb44b.html"))
	r.Add("/humans.txt").Get(staticConfig.FileHandler("humans.txt"))
	r.Add("/robots.txt").Get(staticConfig.FileHandler("presentRobots.txt"))
	r.Add("/search").GetFunc(serveAPISearch)
	r.Add("/packages").GetFunc(serveAPIPackages)
	r.Add("/importers/<path:.+>").GetFunc(serveAPIImporters)

	h.Add("api.<:.*>", web.ErrorHandler(handleAPIError, web.FormAndCookieHandler(6000, false, r)))

	r = web.NewRouter()
	r.Add("/-/site.js").Get(dataHandler("site.js", "text/javascript", *assetsDir,
		"third_party/jquery.timeago.js",
		"third_party/typeahead.min.js",
		"third_party/bootstrap/js/bootstrap.min.js",
		"site.js"))
	r.Add("/-/site.css").Get(dataHandler("site.css", "text/css", *assetsDir,
		"third_party/bootstrap/css/bootstrap.min.css", "site.css"))
	r.Add("/").GetFunc(serveHome)
	r.Add("/-/about").GetFunc(serveAbout)
	r.Add("/-/bot").GetFunc(serveBot)
	r.Add("/-/opensearch.xml").GetFunc(serveOpenSearchDescription)
	r.Add("/-/typeahead").GetFunc(serveTypeahead)
	r.Add("/-/go").GetFunc(serveGoIndex)
	r.Add("/-/subrepo").GetFunc(serveGoSubrepoIndex)
	r.Add("/-/index").GetFunc(serveIndex)
	r.Add("/-/refresh").PostFunc(serveRefresh)
	r.Add("/-/static/<path:.*>").Get(staticConfig.DirectoryHandler("static"))
	r.Add("/a/index").Get(web.RedirectHandler("/-/index", 301))
	r.Add("/about").Get(web.RedirectHandler("/-/about", 301))
	r.Add("/favicon.ico").Get(staticConfig.FileHandler("favicon.ico"))
	r.Add("/google3d2f3cd4cc2bb44b.html").Get(staticConfig.FileHandler("google3d2f3cd4cc2bb44b.html"))
	r.Add("/humans.txt").Get(staticConfig.FileHandler("humans.txt"))
	r.Add("/robots.txt").Get(staticConfig.FileHandler("robots.txt"))
	r.Add("/BingSiteAuth.xml").Get(staticConfig.FileHandler("BingSiteAuth.xml"))
	r.Add("/C").Get(web.RedirectHandler("http://golang.org/doc/articles/c_go_cgo.html", 301))
	r.Add("/<path:.+>").GetFunc(servePackage)

	h.Add("<:.*>", web.ErrorHandler(handleError, web.FormAndCookieHandler(1000, false, r)))

	listener, err := net.Listen("tcp", *httpAddr)
	if err != nil {
		log.Fatal("Listen", err)
		return
	}
	defer listener.Close()
	s := &server.Server{Listener: listener, Handler: h} // add logger
	err = s.Serve()
	if err != nil {
		log.Fatal("Server", err)
	}
}
Beispiel #2
0
func main() {
	flag.Parse()
	log.Printf("Starting server, os.Args=%s", strings.Join(os.Args, " "))
	if err := readSecrets(); err != nil {
		log.Fatal(err)
	}

	if err := parseHTMLTemplates([][]string{
		{"about.html", "common.html", "layout.html"},
		{"bot.html", "common.html", "layout.html"},
		{"cmd.html", "common.html", "layout.html"},
		{"home.html", "common.html", "layout.html"},
		{"importers.html", "common.html", "layout.html"},
		{"imports.html", "common.html", "layout.html"},
		{"interface.html", "common.html", "layout.html"},
		{"index.html", "common.html", "layout.html"},
		{"notfound.html", "common.html", "layout.html"},
		{"pkg.html", "common.html", "layout.html"},
		{"results.html", "common.html", "layout.html"},
		{"std.html", "common.html", "layout.html"},
		{"graph.html", "common.html"},
	}); err != nil {
		log.Fatal(err)
	}

	if err := parseTextTemplates([][]string{
		{"cmd.txt", "common.txt"},
		{"home.txt", "common.txt"},
		{"notfound.txt", "common.txt"},
		{"pkg.txt", "common.txt"},
		{"results.txt", "common.txt"},
		{"opensearch.xml"},
	}); err != nil {
		log.Fatal(err)
	}

	if err := parsePresentTemplates([][]string{
		{".article", "article.tmpl", "action.tmpl"},
		{".slide", "slides.tmpl", "action.tmpl"},
	}); err != nil {
		log.Fatal(err)
	}

	present.PlayEnabled = true

	var err error
	db, err = database.New()
	if err != nil {
		log.Fatal(err)
	}

	if *crawlInterval > 0 {
		go crawl(*crawlInterval)
	}

	if *githubInterval > 0 {
		go crawlGithubUpdates(*githubInterval)
	}

	playScript, err := readPlayScript(*presentDir)
	if err != nil {
		log.Fatal(err)
	}

	staticConfig := &web.StaticConfig{
		Header:      web.Header{web.HeaderCacheControl: {"public, max-age=3600"}},
		Directory:   *assetsDir,
		GzDirectory: *gzAssetsDir,
	}
	presentStaticConfig := &web.StaticConfig{
		Header:    web.Header{web.HeaderCacheControl: {"public, max-age=3600"}},
		Directory: *presentDir,
	}

	h := web.NewHostRouter()

	r := web.NewRouter()
	r.Add("/").GetFunc(servePresentHome)
	r.Add("/compile").PostFunc(serveCompile)
	r.Add("/favicon.ico").Get(staticConfig.FileHandler("favicon.ico"))
	r.Add("/google3d2f3cd4cc2bb44b.html").Get(staticConfig.FileHandler("google3d2f3cd4cc2bb44b.html"))
	r.Add("/humans.txt").Get(staticConfig.FileHandler("humans.txt"))
	r.Add("/play.js").Get(web.DataHandler(playScript, web.Header{web.HeaderContentType: {"text/javascript"}}))
	r.Add("/robots.txt").Get(staticConfig.FileHandler("presentRobots.txt"))
	r.Add("/static/<path:.*>").Get(presentStaticConfig.DirectoryHandler("static"))
	r.Add("/<path:.+>").GetFunc(servePresentation)

	h.Add("talks.<:.*>", web.ErrorHandler(handlePresentError, web.FormAndCookieHandler(6000, false, r)))

	r = web.NewRouter()
	r.Add("/favicon.ico").Get(staticConfig.FileHandler("favicon.ico"))
	r.Add("/google3d2f3cd4cc2bb44b.html").Get(staticConfig.FileHandler("google3d2f3cd4cc2bb44b.html"))
	r.Add("/humans.txt").Get(staticConfig.FileHandler("humans.txt"))
	r.Add("/robots.txt").Get(staticConfig.FileHandler("presentRobots.txt"))
	r.Add("/search").GetFunc(serveAPISearch)
	r.Add("/packages").GetFunc(serveAPIPackages)

	h.Add("api.<:.*>", web.ErrorHandler(handleAPIError, web.FormAndCookieHandler(6000, false, r)))

	r = web.NewRouter()
	r.Add("/").GetFunc(serveHome)
	r.Add("/-/about").GetFunc(serveAbout)
	r.Add("/-/bot").GetFunc(serveBot)
	r.Add("/-/opensearch.xml").GetFunc(serveOpenSearchDescription)
	r.Add("/-/typeahead").GetFunc(serveTypeahead)
	r.Add("/-/go").GetFunc(serveGoIndex)
	r.Add("/-/index").GetFunc(serveIndex)
	r.Add("/-/refresh").PostFunc(serveRefresh)
	r.Add("/-/static/<path:.*>").Get(staticConfig.DirectoryHandler("static"))
	r.Add("/a/index").Get(web.RedirectHandler("/-/index", 301))
	r.Add("/about").Get(web.RedirectHandler("/-/about", 301))
	r.Add("/favicon.ico").Get(staticConfig.FileHandler("favicon.ico"))
	r.Add("/google3d2f3cd4cc2bb44b.html").Get(staticConfig.FileHandler("google3d2f3cd4cc2bb44b.html"))
	r.Add("/humans.txt").Get(staticConfig.FileHandler("humans.txt"))
	r.Add("/robots.txt").Get(staticConfig.FileHandler("robots.txt"))
	r.Add("/BingSiteAuth.xml").Get(staticConfig.FileHandler("BingSiteAuth.xml"))
	r.Add("/C").Get(web.RedirectHandler("http://golang.org/doc/articles/c_go_cgo.html", 301))
	r.Add("/<path:.+>").GetFunc(servePackage)

	h.Add("<:.*>", web.ErrorHandler(handleError, web.FormAndCookieHandler(1000, false, r)))

	listener, err := net.Listen("tcp", *httpAddr)
	if err != nil {
		log.Fatal("Listen", err)
		return
	}
	defer listener.Close()
	s := &server.Server{Listener: listener, Handler: h} // add logger
	err = s.Serve()
	if err != nil {
		log.Fatal("Server", err)
	}
}
Beispiel #3
0
func main() {
	flag.Parse()
	log.Printf("Starting server, os.Args=%s", strings.Join(os.Args, " "))
	if err := readSecrets(); err != nil {
		log.Fatal(err)
	}

	if err := parseHTMLTemplates([][]string{
		{"about.html", "common.html", "skeleton.html"},
		{"bot.html", "common.html", "skeleton.html"},
		{"cmd.html", "common.html", "skeleton.html"},
		{"home.html", "common.html", "skeleton.html"},
		{"importers.html", "common.html", "skeleton.html"},
		{"imports.html", "common.html", "skeleton.html"},
		{"index.html", "common.html", "skeleton.html"},
		{"notfound.html", "common.html", "skeleton.html"},
		{"pkg.html", "common.html", "skeleton.html"},
		{"results.html", "common.html", "skeleton.html"},
		{"std.html", "common.html", "skeleton.html"},
		{"graph.html", "common.html"},
	}); err != nil {
		log.Fatal(err)
	}

	if err := parseTextTemplates([][]string{
		{"cmd.txt", "common.txt"},
		{"home.txt", "common.txt"},
		{"notfound.txt", "common.txt"},
		{"pkg.txt", "common.txt"},
		{"results.txt", "common.txt"},
	}); err != nil {
		log.Fatal(err)
	}

	if err := parsePresentTemplates([][]string{
		{".article", "article.tmpl", "action.tmpl"},
		{".slide", "slides.tmpl", "action.tmpl"},
	}); err != nil {
		log.Fatal(err)
	}

	present.PlayEnabled = true

	var err error
	db, err = database.New()
	if err != nil {
		log.Fatal(err)
	}

	if *popularInterval > 0 {
		go updatePopularPackages(*popularInterval)
	}

	if *crawlInterval > 0 {
		go crawl(*crawlInterval)
	}

	sfo := &web.ServeFileOptions{
		Header: web.Header{
			web.HeaderCacheControl: {"public, max-age=3600"},
		},
	}

	r := web.NewRouter()
	r.Add("/").GetFunc(servePresentHome)
	r.Add("/compile").PostFunc(serveCompile)
	r.Add("/favicon.ico").Get(web.FileHandler(filepath.Join(*baseDir, "static", "favicon.ico"), nil))
	r.Add("/google3d2f3cd4cc2bb44b.html").Get(web.FileHandler(filepath.Join(*baseDir, "static", "google3d2f3cd4cc2bb44b.html"), nil))
	r.Add("/humans.txt").Get(web.FileHandler(filepath.Join(*baseDir, "static", "humans.txt"), nil))
	r.Add("/play.js").Get(web.CatFilesHandler(sfo, filepath.Join(*presentBaseDir, "js"), "jquery.js", "playground.js", "play.js"))
	r.Add("/robots.txt").Get(web.FileHandler(filepath.Join(*baseDir, "static", "presentRobots.txt"), nil))
	r.Add("/static/<path:.*>").Get(web.DirectoryHandler(filepath.Join(*presentBaseDir, "static"), sfo))
	r.Add("/<path:.+>").GetFunc(servePresentation)

	h := web.NewHostRouter()
	h.Add("talks.<:.*>", web.ErrorHandler(handlePresentError, web.FormAndCookieHandler(6000, false, r)))

	r = web.NewRouter()
	r.Add("/").GetFunc(serveHome)
	r.Add("/-/about").GetFunc(serveAbout)
	r.Add("/-/bot").GetFunc(serveBot)
	r.Add("/-/go").GetFunc(serveGoIndex)
	r.Add("/-/index").GetFunc(serveIndex)
	r.Add("/-/refresh").PostFunc(serveRefresh)
	r.Add("/-/static/<path:.*>").Get(web.DirectoryHandler(filepath.Join(*baseDir, "static"), sfo))
	r.Add("/a/index").Get(web.RedirectHandler("/-/index", 301))
	r.Add("/about").Get(web.RedirectHandler("/-/about", 301))
	r.Add("/favicon.ico").Get(web.FileHandler(filepath.Join(*baseDir, "static", "favicon.ico"), nil))
	r.Add("/google3d2f3cd4cc2bb44b.html").Get(web.FileHandler(filepath.Join(*baseDir, "static", "google3d2f3cd4cc2bb44b.html"), nil))
	r.Add("/humans.txt").Get(web.FileHandler(filepath.Join(*baseDir, "static", "humans.txt"), nil))
	r.Add("/robots.txt").Get(web.FileHandler(filepath.Join(*baseDir, "static", "robots.txt"), nil))
	r.Add("/C").Get(web.RedirectHandler("http://golang.org/doc/articles/c_go_cgo.html", 301))
	r.Add("/<path:.+>").GetFunc(servePackage)

	h.Add("<:.*>", web.ErrorHandler(handleError, web.FormAndCookieHandler(1000, false, r)))

	listener, err := net.Listen("tcp", *httpAddr)
	if err != nil {
		log.Fatal("Listen", err)
		return
	}
	defer listener.Close()
	s := &server.Server{Listener: listener, Handler: h} // add logger
	err = s.Serve()
	if err != nil {
		log.Fatal("Server", err)
	}
}