Ejemplo n.º 1
0
// run does the following:
// - creates the dest directory
// - every asset is saved as a file
// - every asset is gzipped saved as a file + .gz if it's size is above threshold
func (ae *assetsRestore) run() {
	if _, err := os.Stat(ae.destdir); err == nil {
		ae.logger.Fatalf("%s: File exists\n", ae.destdir)
	}
	ae.logger.fatalif(assets.RestoreAssets(ae.destdir, ""))
	// ae.logger.fatalif(os.Mkdir(ae.destdir, os.ModePerm))
	for _, name := range assets.AssetNames() {
		text, err := assets.Asset(name)
		if err != nil {
			ae.logger.Printf("assets.Asset: %s: %s", name, err)
			continue
		}
		full := filepath.Join(ae.destdir, name)
		/* dir := filepath.Dir(full)
		if _, err := os.Stat(dir); err != nil {
			ae.logger.fatalif(os.MkdirAll(dir, os.ModePerm))
		}

		file, err := os.Create(full)
		ae.logger.fatalif(err)

		_, err = file.Write(text)
		ae.logger.fatalif(err)
		file.Close() // */

		now := time.Now()
		ae.logger.fatalif(os.Chtimes(full, now, now))

		if len(text) <= 1024 {
			continue
		}

		gzfile, err := os.Create(full + ".gz")
		ae.logger.fatalif(err)

		gzwriter := gzip.NewWriter(gzfile)
		_, err = gzwriter.Write(text)
		ae.logger.fatalif(err)

		gzwriter.Close()
		gzfile.Close()

		ae.logger.fatalif(os.Chtimes(full+".gz", now, now))
	}
}
Ejemplo n.º 2
0
Archivo: serve.go Proyecto: vadv/ostent
func Serve(listener net.Listener, taggedbin bool, extramap ostent.Muxmap) error {
	mux, chain, access := ostent.NewServery(taggedbin, extramap)
	errlog, errclose := ostent.NewErrorLog()
	defer errclose()

	if index := chain.ThenFunc(ostent.IndexFunc(taggedbin, templates.IndexTemplate,
		PeriodFlag)); true {
		mux.Handler("GET", "/", index)
		mux.Handler("HEAD", "/", index)
	}

	if formred := ostent.FormRedirectFunc(PeriodFlag, chain.ThenFunc); true {
		mux.GET("/form/*Q", formred)
		mux.POST("/form/*Q", formred)
	}

	// chain is not used -- access is passed to log with.
	mux.HandlerFunc("GET", "/index.ws", ostent.IndexWSFunc(access, errlog, PeriodFlag))
	mux.HandlerFunc("GET", "/index.sse", ostent.IndexSSEFunc(access, PeriodFlag))

	if !taggedbin { // dev-only
		mux.Handler("GET", "/panic", chain.ThenFunc(
			func(w http.ResponseWriter, r *http.Request) {
				panic("/panic")
			}))
	}

	logger := log.New(os.Stderr, "[ostent] ", 0)
	for _, path := range assets.AssetNames() {
		hf := chain.Then(ostent.ServeContentFunc(
			AssetReadFunc(assets.Asset),
			AssetInfoFunc(assets.AssetInfo),
			path, logger))
		mux.Handler("GET", "/"+path, hf)
		mux.Handler("HEAD", "/"+path, hf)
	}

	ostent.Banner(listener.Addr().String(), "ostent", logger)
	s := &http.Server{
		ErrorLog: errlog,
		Handler:  mux,
	}
	return s.Serve(listener)
}