Exemple #1
0
func main() {
	sl, err := gurren.New([]string{"http://127.0.0.1:9200"}, "test", runtime.NumCPU())
	if err != nil {
		panic(err)
	}

	mux := routes.New()

	// Do handling here

	mux.Get("/", func(rw http.ResponseWriter, r *http.Request) {
		tpl, err := ace.Load("views/layout", "views/index", nil)
		if err != nil {
			http.Error(rw, err.Error(), http.StatusInternalServerError)
			return
		}

		if err := tpl.Execute(rw, nil); err != nil {
			http.Error(rw, err.Error(), http.StatusInternalServerError)
			return
		}
	})

	n := negroni.Classic()

	middleware.Inject(n)
	n.Use(sl)
	n.UseHandler(mux)

	n.Run(":3000")
}
Exemple #2
0
func homeHandler(w http.ResponseWriter, r *http.Request) {

	tpl, _ := ace.Load("templates/base", "templates/home", &ace.Options{DynamicReload: true})
	tpl.Execute(w, []string{"1", "2", "3", "4", "5", "6"})
	// t, _ := template.ParseFiles("templates/home.html", "templates/_header.html")
	// t.Execute(w, "Hello World!")
}
Exemple #3
0
// HandleError renders an error as a HTML page to the user.
func HandleError(rw http.ResponseWriter, r *http.Request, err error) {
	rw.WriteHeader(500)

	if globals.Config.Site.Testing {
		panic(err)
	}

	data := struct {
		Path     string
		Reason   string
		SiteName string
	}{
		Path:     r.URL.String(),
		Reason:   err.Error(),
		SiteName: globals.Config.Site.Name,
	}

	tpl, err := ace.Load("views/layout", "views/error", &ace.Options{
		FuncMap: funcs,
	})
	if err != nil {
		http.Error(rw, err.Error(), http.StatusInternalServerError)
		return
	}

	if err := tpl.Execute(rw, Wrap(r, data)); err != nil {
		http.Error(rw, err.Error(), http.StatusInternalServerError)
		return
	}
}
Exemple #4
0
func handler(w http.ResponseWriter, r *http.Request) {

	tpl, err := ace.Load("rainbow", "", nil)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	rand.Seed(time.Now().UnixNano())
	messages := []string{
		"You are awesome,",
		"Pure awesomeness",
		"Coolest person alive:",
		"Literally Awesome:",
		"The amazing",
		"YOLO 420 -",
		"Knows how to tie their shoes:",
		"Makes delicious cakes:",
	}
	selectedMessage := messages[rand.Intn(len(messages))]

	templateErr := tpl.Execute(w, map[string]string{"Message": selectedMessage, "Counter": strconv.Itoa(i), "Url": r.URL.Path[1:]})
	if templateErr != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	} else {
		fmt.Println("Serving " + selectedMessage + " " + r.URL.Path[1:] + " for " + r.RemoteAddr)
		if r.URL.Path[1:] != "favicon.ico" {
			i++
		}
	}
}
Exemple #5
0
func handler(w http.ResponseWriter, r *http.Request) {
	tpl, err := ace.Load("base", "inner", nil)
	if err != nil {
		fmt.Printf("error %s", err.Error())
	}
	handleError(err, w)

	// Generate the SSA representation
	if r.Method == "POST" {
		err = r.ParseForm()
		handleError(err, w)

		// Iterate over the checkboxes
		// content[cb.Name] is used in the toSSA algorithm
		cbs := content["cbs"].([]Cb)
		for i, cb := range cbs {
			if r.PostFormValue(cb.Name) == "true" {
				content[cb.Name] = "true"
				cbs[i].Checked = true
			} else {
				content[cb.Name] = "false"
				cbs[i].Checked = false
			}
		}

		ssaBytes := bytes.NewBufferString(r.PostFormValue("source"))
		ssafs, err := toSSA(ssaBytes, "main.go", "main")
		handleError(err, w)
		content["sourceCode"] = r.PostFormValue("source")
		content["ssa"] = ssafs
	}

	err = tpl.Execute(w, content)
	handleError(err, w)
}
func (a *AceRenderer) Load(paths ...string) (*template.Template, error) {
	var base, inner string
	if len(paths) > 0 {
		base = paths[0]
	} else if len(paths) > 1 {
		inner = paths[1]
	}
	return ace.Load(base, inner, &a.Options)
}
Exemple #7
0
// Load calls the `Load` function of the Ace template engine.
func (p *Proxy) Load(basePath, innerPath string, opts *ace.Options) (*template.Template, error) {
	var o *ace.Options

	if opts == nil {
		o = p.Opts
	} else {
		o = opts
	}

	return ace.Load(basePath, innerPath, o)
}
Exemple #8
0
func handler(w http.ResponseWriter, r *http.Request) {
	tpl, err := ace.Load("example", "", &ace.Options{DynamicReload: true})
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	if err := tpl.Execute(w, nil); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}
Exemple #9
0
func root(c web.C, w http.ResponseWriter, r *http.Request) {
	tpl, err := ace.Load("index", "", nil)
	if err != nil {
		panic(err)
	}
	data := map[string]interface{}{"Title": "Kanban"}
	err = tpl.Execute(w, data)
	if err != nil {
		panic(err)
	}
}
Exemple #10
0
func handler(w http.ResponseWriter, r *http.Request) {
	tpl, err := ace.Load("base", "inner", nil)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	if err := tpl.Execute(w, nil); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}
Exemple #11
0
func doTemplate(name string, rw http.ResponseWriter, r *http.Request, data interface{}) {
	tpl, err := ace.Load("views/layout", name, nil)
	if err != nil {
		handleError(rw, r, err)
		return
	}

	if err := tpl.Execute(rw, data); err != nil {
		handleError(rw, r, err)
		return
	}
}
Exemple #12
0
// ChangelogHandle changelog page
func ChangelogHandle(w http.ResponseWriter, r *http.Request) {
	tpl, err := ace.Load(common.GetConfig().Server.ViewPath+"/layout", common.GetConfig().Server.ViewPath+"/changelog", nil)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	w.Header().Set("Content-type", "text/html")
	if err := tpl.Execute(w, nil); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}
Exemple #13
0
func f(w http.ResponseWriter, r *http.Request) {

	tpl, err := ace.Load("base", "inner", nil)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	if err := tpl.Execute(w, map[string]string{"Msg": "Hello Ace"}); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}
Exemple #14
0
func handler(w http.ResponseWriter, r *http.Request) {
	tpl, err := ace.Load("views/example", "", &ace.Options{
		Asset: Asset,
	})
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	if err := tpl.Execute(w, nil); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}
Exemple #15
0
func main() {
	// create an output directory (the assets subdirectory here because its
	// parent will be created as a matter of course)
	err := os.MkdirAll(TargetAssetsDir, 0755)
	if err != nil {
		log.Fatal(err.Error())
	}

	template, err := ace.Load("index", "", &ace.Options{
		DynamicReload: true,
		FuncMap:       templateFuncMap(),
	})
	if err != nil {
		log.Fatal(err.Error())
	}

	db, err := wgt2.LoadDatabase(DBFilename)
	if err != nil {
		log.Fatal(err.Error())
	}

	var artists []*wgt2.Artist
	for _, artist := range db.Artists.Data {
		artists = append(artists, artist)
	}

	// Go doesn't exactly make sorting easy ...
	sort.Sort(artistSlice(artists))

	var playlists []*wgt2.Playlist
	for _, playlist := range db.Playlists.Data {
		playlists = append(playlists, playlist)
	}

	file, err := os.Create(TargetDir + "index")
	if err != nil {
		log.Fatal(err.Error())
	}
	defer file.Close()

	writer := bufio.NewWriter(file)
	defer writer.Flush()

	data := map[string]interface{}{
		"artists":   artists,
		"playlists": playlists,
	}
	if err := template.Execute(writer, data); err != nil {
		log.Fatal(err.Error())
	}
}
Exemple #16
0
// WrapperHandle static html log wrapper
func WrapperHandle(w http.ResponseWriter, r *http.Request) {
	tpl, err := ace.Load(common.GetConfig().Server.ViewPath+"/layout", common.GetConfig().Server.ViewPath+"/wrapper", nil)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	w.Header().Set("Content-type", "text/html; charset=UTF-8")
	path := r.URL.Path + ".txt"
	if r.URL.RawQuery != "" {
		path += "?" + r.URL.RawQuery
	}
	if err := tpl.Execute(w, struct{ Path string }{Path: path}); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}
Exemple #17
0
func compileResultFromFile(baseFile, innerFile string) (string, error) {
	base := baseFile[:len(baseFile)-len(filepath.Ext(baseFile))]

	var inner string
	if len(innerFile) > 0 {
		inner = innerFile[:len(innerFile)-len(filepath.Ext(innerFile))]
	}
	name := base + ":" + inner

	tpl, err := ace.Load(base, inner, nil)
	if err != nil {
		return "", err
	}
	return tpl.Lookup(name).Tree.Root.String(), nil
}
Exemple #18
0
func (c controller) Render(w http.ResponseWriter, view string) {
	options := &ace.Options{Asset: Asset}

	tpl, err := ace.Load("backend/views/layout", view, options)
	if err != nil {
		http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
		return
	}

	err = tpl.Execute(w, nil)
	if err != nil {
		http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
		return
	}
}
Exemple #19
0
// DoTemplate does a template with the given data to pass to it. It will be
// wrapped as .Data.
func DoTemplate(name string, rw http.ResponseWriter, r *http.Request, data interface{}) {
	tpl, err := ace.Load("views/layout", "views/"+name, &ace.Options{
		FuncMap: funcs,
	})
	if err != nil {
		HandleError(rw, r, err)
		return
	}

	wrapped := Wrap(r, data)

	if err := tpl.Execute(rw, wrapped); err != nil {
		HandleError(rw, r, err)
		return
	}
}
Exemple #20
0
func handler(w http.ResponseWriter, r *http.Request) {
	tpl, err := ace.Load("example", "", &ace.Options{
		DelimLeft:  "<%",
		DelimRight: "%>",
	})
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	data := map[string]interface{}{
		"Msg": "Hello Ace",
	}
	if err := tpl.Execute(w, data); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}
Exemple #21
0
func handler(w http.ResponseWriter, r *http.Request) {
	tpl, err := ace.Load("example", "", nil)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	data := map[string]interface{}{
		"Pets": []Pet{
			Pet{Species: "Dog", Name: "Taro", Age: 5},
			Pet{Species: "Cat", Name: "Hanako", Age: 10},
			Pet{Species: "Rabbit", Name: "Jiro", Age: 1},
		},
	}
	if err := tpl.Execute(w, data); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}
Exemple #22
0
func handler(w http.ResponseWriter, r *http.Request) {
	funcMap := template.FuncMap{
		"Greeting": func(s string) string {
			return "Hello " + s
		},
	}
	tpl, err := ace.Load("example", "", &ace.Options{
		FuncMap: funcMap,
	})
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	if err := tpl.Execute(w, nil); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}
Exemple #23
0
func handler(w http.ResponseWriter, r *http.Request) {
	tpl, err := ace.Load("example", "", nil)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	data := map[string]interface{}{
		"Title": "Actions",
		"Msgs": []string{
			"Message1",
			"Message2",
			"Message3",
		},
	}
	if err := tpl.Execute(w, data); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}
Exemple #24
0
func indexHandler(w http.ResponseWriter, r *http.Request) {
	var err error
	defer func() {
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
		}
	}()

	if r.Method != "GET" {
		http.Error(w, "Method not allowed", 405)
		return
	}

	tpl, err := ace.Load("index", "", &ace.Options{DynamicReload: true})
	if err != nil {
		return
	}

	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	err = tpl.Execute(w, r.Host)
}
Exemple #25
0
func homeHandler(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)

	data := map[string]string{
		"isDevelopment":   appEngineEnvironment("isDevelopment"),
		"isProduction":    appEngineEnvironment("isProduction"),
		"currentHostname": appengine.DefaultVersionHostname(c),
	}

	tpl, err := ace.Load("base", "inner", &ace.Options{
		BaseDir:       "views",
		DynamicReload: true})
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	if err := tpl.Execute(w, data); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}
Exemple #26
0
func handleError(rw http.ResponseWriter, r *http.Request, err error) {
	rw.WriteHeader(500)

	data := struct {
		Path   string
		Reason string
	}{
		Path:   r.URL.String(),
		Reason: err.Error(),
	}

	tpl, err := ace.Load("views/layout", "views/error", nil)
	if err != nil {
		http.Error(rw, err.Error(), http.StatusInternalServerError)
		return
	}

	if err := tpl.Execute(rw, data); err != nil {
		http.Error(rw, err.Error(), http.StatusInternalServerError)
		return
	}
}
Exemple #27
0
// serveError ...
func serveError(w http.ResponseWriter, e error) {
	tpl, err := ace.Load(common.GetConfig().Server.ViewPath+"/layout", common.GetConfig().Server.ViewPath+"/error", nil)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	data := map[string]interface{}{}
	if e == ErrNotFound {
		w.WriteHeader(http.StatusNotFound)
		data["Message"] = e.Error()
	} else if e != nil {
		// w.WriteHeader(http.StatusInternalServerError)
		data["Message"] = e.Error()
	} else {
		// w.WriteHeader(http.StatusInternalServerError)
		data["Message"] = "Unknown Error"
	}
	w.Header().Set("Content-type", "text/html")
	if err := tpl.Execute(w, data); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}
Exemple #28
0
func renderView(layout, view, target string, locals map[string]interface{}) error {
	log.Debugf("Rendering: %v", target)

	template, err := ace.Load(layout, view, &ace.Options{FuncMap: templatehelpers.FuncMap})
	if err != nil {
		return err
	}

	file, err := os.Create(target)
	if err != nil {
		return err
	}
	defer file.Close()

	writer := bufio.NewWriter(file)
	defer writer.Flush()

	err = template.Execute(writer, locals)
	if err != nil {
		return err
	}

	return nil
}
Exemple #29
0
// serveDirIndex ...
func serveDirIndex(w http.ResponseWriter, base []string, paths []string) {
	tpl, err := ace.Load(common.GetConfig().Server.ViewPath+"/layout", common.GetConfig().Server.ViewPath+"/directory", nil)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	data := map[string]interface{}{
		"Breadcrumbs": []map[string]string{},
		"Paths":       []map[string]string{},
	}
	basePath := ""
	for _, path := range base {
		basePath += "/" + path
		data["Breadcrumbs"] = append(data["Breadcrumbs"].([]map[string]string), map[string]string{
			"Path": basePath,
			"Name": path,
		})
	}
	basePath += "/"
	for _, path := range paths {
		icon := "file-text"
		if filepath.Ext(path) == "" {
			icon = "folder"
		}
		data["Paths"] = append(data["Paths"].([]map[string]string), map[string]string{
			"Path": basePath + strings.Replace(path, ".txt", "", -1),
			"Name": path,
			"Icon": icon,
		})
	}
	w.Header().Set("Content-type", "text/html")
	if err := tpl.Execute(w, data); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}
Exemple #30
0
func signupHandler(w http.ResponseWriter, r *http.Request) {
	tpl, _ := ace.Load(
		"templates/base", "templates/signup",
		&ace.Options{DynamicReload: true})
	tpl.Execute(w, nil)
}