Esempio n. 1
0
func TestBaseDirectory(t *testing.T) {
	mustStr := "Hello from template_tests/base_dir_test/"

	fs := pongo2.MustNewLocalFileSystemLoader("")
	s := pongo2.NewSet("test set with base directory", fs)
	s.Globals["base_directory"] = "template_tests/base_dir_test/"
	if err := fs.SetBaseDir(s.Globals["base_directory"].(string)); err != nil {
		t.Fatal(err)
	}

	matches, err := filepath.Glob("./template_tests/base_dir_test/subdir/*")
	if err != nil {
		t.Fatal(err)
	}
	for _, match := range matches {
		if "windows" == runtime.GOOS {
			match = strings.Replace(match, "template_tests\\base_dir_test\\", "", -1)
		} else {
			match = strings.Replace(match, "template_tests/base_dir_test/", "", -1)
		}

		tpl, err := s.FromFile(match)
		if err != nil {
			t.Fatal(err)
		}
		out, err := tpl.Execute(nil)
		if err != nil {
			t.Fatal(err)
		}
		if out != mustStr {
			t.Errorf("%s: out ('%s') != mustStr ('%s')", match, out, mustStr)
		}
	}
}
Esempio n. 2
0
func CreateFrontend(cfg FrontendConfig) (frontend *Frontend, err error) {
	frontend = &Frontend{cfg: cfg}

	// Create logger
	frontend.Log = log.New(frontend.cfg.LogOutput, "frontend: ", log.Ldate|log.Lshortfile)

	// Create an ElasticSearch connection
	frontend.elasticSearch, err = CreateElasticSearch(frontend.cfg.ElasticServer)
	if err != nil {
		return
	}

	// Create a pongo2 template set
	frontend.templates = pongo2.NewSet("torture")
	frontend.templates.SetBaseDirectory("templates")

	// Sub-Apps
	search, err := CreateSearch(SearchConfig{
		Frontend: frontend,
	})
	if err != nil {
		return
	}

	mux := httprouter.New()
	mux.Handle("GET", "/s", search.Handler)
	mux.Handler("GET", "/", http.RedirectHandler("/s", 301))
	mux.ServeFiles("/static/*filepath", http.Dir("static"))

	log.Fatal(http.ListenAndServe(frontend.cfg.HttpListen, mux))

	return
}
Esempio n. 3
0
File: pongo.go Progetto: Carbyn/iris
func (p *Engine) buildFromDir() (templateErr error) {
	if p.Config.Directory == "" {
		return nil //we don't return fill error here(yet)
	}
	dir := p.Config.Directory

	fsLoader, err := pongo2.NewLocalFileSystemLoader(dir) // I see that this doesn't read the content if already parsed, so do it manually via filepath.Walk
	if err != nil {
		return err
	}

	set := pongo2.NewSet("", fsLoader)
	set.Globals = getPongoContext(p.Config.Pongo.Globals)
	// Walk the supplied directory and compile any files that match our extension list.
	filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
		// Fix same-extension-dirs bug: some dir might be named to: "users.tmpl", "local.html".
		// These dirs should be excluded as they are not valid golang templates, but files under
		// them should be treat as normal.
		// If is a dir, return immediately (dir is not a valid golang template).
		if info == nil || info.IsDir() {
			return nil
		}

		rel, err := filepath.Rel(dir, path)
		if err != nil {
			return err
		}

		ext := ""
		if strings.Index(rel, ".") != -1 {
			ext = filepath.Ext(rel)
		}

		for _, extension := range p.Config.Extensions {
			if ext == extension {
				buf, err := ioutil.ReadFile(path)
				if err != nil {
					templateErr = err
					break
				}
				if err != nil {
					templateErr = err
					break
				}
				name := filepath.ToSlash(rel)
				p.templateCache[name], templateErr = set.FromString(string(buf))

				if templateErr != nil {
					return templateErr
				}
				break
			}
		}
		return nil
	})

	return
}
Esempio n. 4
0
func (d *Data) renderLowLevel(dst string, src string, prefix string, r io.Reader) error {
	var err error

	// Determine the filename and whether we're dealing with a template
	var tpl *pongo2.Template = nil
	filename := src
	if strings.HasSuffix(filename, ".tpl") {
		var buf bytes.Buffer
		if _, err := io.Copy(&buf, r); err != nil {
			return err
		}

		base := filepath.Dir(filename)
		if prefix != "" {
			base = filepath.Join(prefix, base)
		}

		// Create the template set so we can control loading
		tplSet := pongo2.NewSet("otto", &tplLoader{
			Data: d,
			Base: base,
		})

		// Parse the template
		dst = strings.TrimSuffix(dst, ".tpl")
		tpl, err = tplSet.FromString(buf.String())
		if err != nil {
			return err
		}
	}

	// Make the directory containing the final path.
	dir := filepath.Dir(dst)
	if err := os.MkdirAll(dir, 0755); err != nil {
		return err
	}

	// Create the file itself
	f, err := os.Create(dst)
	if err != nil {
		return err
	}
	defer f.Close()

	// If it isn't a template, do a direct byte copy
	if tpl == nil {
		_, err = io.Copy(f, r)
		return err
	}

	return tpl.ExecuteWriter(d.Context, f)
}
Esempio n. 5
0
func BenchmarkCache(b *testing.B) {
	cacheSet := pongo2.NewSet("cache set", pongo2.MustNewLocalFileSystemLoader(""))
	for i := 0; i < b.N; i++ {
		tpl, err := cacheSet.FromCache("template_tests/complex.tpl")
		if err != nil {
			b.Fatal(err)
		}
		err = tpl.ExecuteWriterUnbuffered(tplContext, ioutil.Discard)
		if err != nil {
			b.Fatal(err)
		}
	}
}
Esempio n. 6
0
func BenchmarkExecuteComplexWithoutSandbox(b *testing.B) {
	s := pongo2.NewSet("set without sandbox", pongo2.MustNewLocalFileSystemLoader(""))
	tpl, err := s.FromFile("template_tests/complex.tpl")
	if err != nil {
		b.Fatal(err)
	}
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		err = tpl.ExecuteWriterUnbuffered(tplContext, ioutil.Discard)
		if err != nil {
			b.Fatal(err)
		}
	}
}
Esempio n. 7
0
func BenchmarkParallelExecuteComplexWithoutSandbox(b *testing.B) {
	s := pongo2.NewSet("set without sandbox", pongo2.MustNewLocalFileSystemLoader(""))
	tpl, err := s.FromFile("template_tests/complex.tpl")
	if err != nil {
		b.Fatal(err)
	}
	b.ResetTimer()
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			err := tpl.ExecuteWriterUnbuffered(tplContext, ioutil.Discard)
			if err != nil {
				b.Fatal(err)
			}
		}
	})
}
Esempio n. 8
0
func httpHelper(f httpHelperFunc) http.HandlerFunc {
	return func(res http.ResponseWriter, r *http.Request) {
		sess, _ := cookieStore.Get(r, "cloudkeys-go")
		ctx := pongo2.Context{}

		if errFlash := sess.Flashes("error"); len(errFlash) > 0 {
			ctx["error"] = errFlash[0].(string)
		}

		template, err := f(res, r, sess, &ctx)
		if err != nil {
			http.Error(res, "An error ocurred.", http.StatusInternalServerError)
			fmt.Printf("ERR: %s\n", err)
			return
		}

		if template != nil {
			// Postponed until https://github.com/flosch/pongo2/issues/68
			//
			// tplsrc, err := Asset("templates/" + *template)
			// if err != nil {
			// 	fmt.Printf("ERR: Could not find template '%s'\n", *template)
			// 	http.Error(res, "An error ocurred.", http.StatusInternalServerError)
			// 	return
			// }
			ts := pongo2.NewSet("frontend")
			ts.SetBaseDirectory("templates")
			tpl, err := ts.FromFile(*template)
			if err != nil {
				fmt.Printf("ERR: Could not parse template '%s': %s\n", *template, err)
				http.Error(res, "An error ocurred.", http.StatusInternalServerError)
				return
			}
			out, err := tpl.Execute(ctx)
			if err != nil {
				fmt.Printf("ERR: Unable to execute template '%s': %s\n", *template, err)
				http.Error(res, "An error ocurred.", http.StatusInternalServerError)
				return
			}

			res.Write([]byte(out))
		}
	}
}
Esempio n. 9
0
File: pongo.go Progetto: Carbyn/iris
func (p *Engine) buildFromAsset() error {
	var templateErr error
	dir := p.Config.Directory
	fsLoader, err := pongo2.NewLocalFileSystemLoader(dir)
	if err != nil {
		return err
	}
	set := pongo2.NewSet("", fsLoader)
	set.Globals = getPongoContext(p.Config.Pongo.Globals)
	for _, path := range p.Config.AssetNames() {
		if !strings.HasPrefix(path, dir) {
			continue
		}

		rel, err := filepath.Rel(dir, path)
		if err != nil {
			panic(err)
		}

		ext := ""
		if strings.Index(rel, ".") != -1 {
			ext = "." + strings.Join(strings.Split(rel, ".")[1:], ".")
		}

		for _, extension := range p.Config.Extensions {
			if ext == extension {

				buf, err := p.Config.Asset(path)
				if err != nil {
					templateErr = err
					break
				}
				name := filepath.ToSlash(rel)
				p.templateCache[name], err = set.FromString(string(buf))
				if err != nil {
					templateErr = err
					break
				}
				break
			}
		}
	}
	return templateErr
}
Esempio n. 10
0
func BenchmarkCompileAndExecuteComplexWithoutSandbox(b *testing.B) {
	buf, err := ioutil.ReadFile("template_tests/complex.tpl")
	if err != nil {
		b.Fatal(err)
	}
	preloadedTpl := string(buf)

	s := pongo2.NewSet("set without sandbox", pongo2.MustNewLocalFileSystemLoader(""))

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		tpl, err := s.FromString(preloadedTpl)
		if err != nil {
			b.Fatal(err)
		}

		err = tpl.ExecuteWriterUnbuffered(tplContext, ioutil.Discard)
		if err != nil {
			b.Fatal(err)
		}
	}
}
Esempio n. 11
0
	"github.com/flosch/pongo2"

	. "gopkg.in/check.v1"
)

// Hook up gocheck into the "go test" runner.

func Test(t *testing.T) { TestingT(t) }

type TestSuite struct {
	tpl *pongo2.Template
}

var (
	_          = Suite(&TestSuite{})
	testSuite2 = pongo2.NewSet("test suite 2", pongo2.MustNewLocalFileSystemLoader(""))
)

func parseTemplate(s string, c pongo2.Context) string {
	t, err := testSuite2.FromString(s)
	if err != nil {
		panic(err)
	}
	out, err := t.Execute(c)
	if err != nil {
		panic(err)
	}
	return out
}

func parseTemplateFn(s string, c pongo2.Context) func() {
Esempio n. 12
0
func setup() {
	goji.Use(ContentSecurityPolicy(CSPOptions{
		policy: Config.contentSecurityPolicy,
		frame:  Config.xFrameOptions,
	}))

	if Config.noLogs {
		goji.Abandon(middleware.Logger)
	}

	// make directories if needed
	err := os.MkdirAll(Config.filesDir, 0755)
	if err != nil {
		log.Fatal("Could not create files directory:", err)
	}

	err = os.MkdirAll(Config.metaDir, 0700)
	if err != nil {
		log.Fatal("Could not create metadata directory:", err)
	}

	// ensure siteURL ends wth '/'
	if lastChar := Config.siteURL[len(Config.siteURL)-1:]; lastChar != "/" {
		Config.siteURL = Config.siteURL + "/"
	}

	// Template setup
	p2l, err := NewPongo2TemplatesLoader()
	if err != nil {
		log.Fatal("Error: could not load templates", err)
	}
	TemplateSet := pongo2.NewSet("templates", p2l)
	TemplateSet.Globals["sitename"] = Config.siteName
	err = populateTemplatesMap(TemplateSet, Templates)
	if err != nil {
		log.Fatal("Error: could not load templates", err)
	}

	staticBox = rice.MustFindBox("static")
	timeStarted = time.Now()
	timeStartedStr = strconv.FormatInt(timeStarted.Unix(), 10)

	// Routing setup
	nameRe := regexp.MustCompile(`^/(?P<name>[a-z0-9-\.]+)$`)
	selifRe := regexp.MustCompile(`^/selif/(?P<name>[a-z0-9-\.]+)$`)
	selifIndexRe := regexp.MustCompile(`^/selif/$`)
	torrentRe := regexp.MustCompile(`^/(?P<name>[a-z0-9-\.]+)/torrent$`)

	goji.Get("/", indexHandler)
	goji.Get("/paste/", pasteHandler)
	goji.Get("/paste", http.RedirectHandler("/paste/", 301))

	if Config.remoteUploads {
		goji.Get("/upload", uploadRemote)
		goji.Get("/upload/", uploadRemote)
	}

	goji.Post("/upload", uploadPostHandler)
	goji.Post("/upload/", uploadPostHandler)
	goji.Put("/upload", uploadPutHandler)
	goji.Put("/upload/:name", uploadPutHandler)
	goji.Delete("/:name", deleteHandler)

	goji.Get("/static/*", staticHandler)
	goji.Get("/favicon.ico", staticHandler)
	goji.Get("/robots.txt", staticHandler)
	goji.Get(nameRe, fileDisplayHandler)
	goji.Get(selifRe, fileServeHandler)
	goji.Get(selifIndexRe, unauthorizedHandler)
	goji.Get(torrentRe, fileTorrentHandler)
	goji.NotFound(notFoundHandler)
}
Esempio n. 13
0
func setup() *web.Mux {
	mux := web.New()

	// middleware
	mux.Use(middleware.RequestID)

	if Config.realIp {
		mux.Use(middleware.RealIP)
	}

	if !Config.noLogs {
		mux.Use(middleware.Logger)
	}

	mux.Use(middleware.Recoverer)
	mux.Use(middleware.AutomaticOptions)
	mux.Use(ContentSecurityPolicy(CSPOptions{
		policy: Config.contentSecurityPolicy,
		frame:  Config.xFrameOptions,
	}))

	if Config.authFile != "" {
		mux.Use(UploadAuth(AuthOptions{
			AuthFile:      Config.authFile,
			UnauthMethods: []string{"GET", "HEAD", "OPTIONS", "TRACE"},
		}))
	}

	// make directories if needed
	err := os.MkdirAll(Config.filesDir, 0755)
	if err != nil {
		log.Fatal("Could not create files directory:", err)
	}

	err = os.MkdirAll(Config.metaDir, 0700)
	if err != nil {
		log.Fatal("Could not create metadata directory:", err)
	}

	// ensure siteURL ends wth '/'
	if lastChar := Config.siteURL[len(Config.siteURL)-1:]; lastChar != "/" {
		Config.siteURL = Config.siteURL + "/"
	}

	parsedUrl, err := url.Parse(Config.siteURL)
	if err != nil {
		log.Fatal("Could not parse siteurl:", err)
	}

	Config.sitePath = parsedUrl.Path

	// Template setup
	p2l, err := NewPongo2TemplatesLoader()
	if err != nil {
		log.Fatal("Error: could not load templates", err)
	}
	TemplateSet := pongo2.NewSet("templates", p2l)
	TemplateSet.Globals["sitename"] = Config.siteName
	TemplateSet.Globals["siteurl"] = Config.siteURL
	TemplateSet.Globals["sitepath"] = Config.sitePath
	TemplateSet.Globals["using_auth"] = Config.authFile != ""
	err = populateTemplatesMap(TemplateSet, Templates)
	if err != nil {
		log.Fatal("Error: could not load templates", err)
	}

	staticBox = rice.MustFindBox("static")
	timeStarted = time.Now()
	timeStartedStr = strconv.FormatInt(timeStarted.Unix(), 10)

	// Routing setup
	nameRe := regexp.MustCompile("^" + Config.sitePath + `(?P<name>[a-z0-9-\.]+)$`)
	selifRe := regexp.MustCompile("^" + Config.sitePath + `selif/(?P<name>[a-z0-9-\.]+)$`)
	selifIndexRe := regexp.MustCompile("^" + Config.sitePath + `selif/$`)
	torrentRe := regexp.MustCompile("^" + Config.sitePath + `(?P<name>[a-z0-9-\.]+)/torrent$`)

	if Config.authFile == "" {
		mux.Get(Config.sitePath, indexHandler)
		mux.Get(Config.sitePath+"paste/", pasteHandler)
	} else {
		mux.Get(Config.sitePath, http.RedirectHandler(Config.sitePath+"API", 303))
		mux.Get(Config.sitePath+"paste/", http.RedirectHandler(Config.sitePath+"API/", 303))
	}
	mux.Get(Config.sitePath+"paste", http.RedirectHandler(Config.sitePath+"paste/", 301))

	mux.Get(Config.sitePath+"API/", apiDocHandler)
	mux.Get(Config.sitePath+"API", http.RedirectHandler(Config.sitePath+"API/", 301))

	if Config.remoteUploads {
		mux.Get(Config.sitePath+"upload", uploadRemote)
		mux.Get(Config.sitePath+"upload/", uploadRemote)

		if Config.remoteAuthFile != "" {
			remoteAuthKeys = readAuthKeys(Config.remoteAuthFile)
		}
	}

	mux.Post(Config.sitePath+"upload", uploadPostHandler)
	mux.Post(Config.sitePath+"upload/", uploadPostHandler)
	mux.Put(Config.sitePath+"upload", uploadPutHandler)
	mux.Put(Config.sitePath+"upload/", uploadPutHandler)
	mux.Put(Config.sitePath+"upload/:name", uploadPutHandler)

	mux.Delete(Config.sitePath+":name", deleteHandler)

	mux.Get(Config.sitePath+"static/*", staticHandler)
	mux.Get(Config.sitePath+"favicon.ico", staticHandler)
	mux.Get(Config.sitePath+"robots.txt", staticHandler)
	mux.Get(nameRe, fileDisplayHandler)
	mux.Get(selifRe, fileServeHandler)
	mux.Get(selifIndexRe, unauthorizedHandler)
	mux.Get(torrentRe, fileTorrentHandler)
	mux.NotFound(notFoundHandler)

	return mux
}
Esempio n. 14
0
	"github.com/sharpner/pobin/fallback"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("Test the loader", func() {
	Context("No Fallback loading works", func() {
		var (
			loader      pongo2.TemplateLoader
			templateSet *pongo2.TemplateSet
		)

		BeforeEach(func() {
			loader = NewMemoryTemplateLoader(assets_test.Asset)
			templateSet = pongo2.NewSet("testing", loader)
		})

		It("should be validly parsed", func() {
			loginTemplate, err := templateSet.FromFile("templates/sites/index.tpl")
			Expect(err).ToNot(HaveOccurred())

			out, err := loginTemplate.Execute(pongo2.Context{
				"action": "/login",
			})

			Expect(err).ToNot(HaveOccurred())
			Expect(string(out)).To(ContainSubstring("Example login page"))
			Expect(string(out)).To(ContainSubstring("<title>"))
			Expect(string(out)).To(ContainSubstring("/login"))
		})