Esempio n. 1
0
func index(ctx *web.Context, val string) string {
	switch val {
	case "Liberator":
		ctx.Redirect(302, util.Settings.WebHome())
	}
	return val
}
Esempio n. 2
0
func update(ctx *web.Context) {
	if ctx.Params["submit"] == "Delete" {
		ctx.SetCookie(web.NewCookie(cookieName, "", -1))
	} else {
		ctx.SetCookie(web.NewCookie(cookieName, ctx.Params["cookie"], 0))
	}
	ctx.Redirect(301, "/")
}
Esempio n. 3
0
func (c *Index) ReadPost(ctx *web.Context, postId string) string {
	p := PostModelInit()
	result := p.RenderPost(postId)

	viewVars := make(map[string]interface{})
	viewVars["Title"] = result.Title
	viewVars["Content"] = result.Content
	viewVars["Date"] = time.Unix(result.Created, 0).Format(blogConfig.Get("dateFormat"))
	viewVars["Id"] = objectIdHex(result.Id.String())
	// To be used within the {{Comments}} blog
	viewVars["PostId"] = objectIdHex(result.Id.String())

	if result.Status == 0 {
		sessionH := session.Start(ctx, h)
		defer sessionH.Save()
		if sessionH.Data["logged"] == nil {
			ctx.Redirect(302, "/")
			return ""
		}
	}

	if blogConfig.Get("enableComment") != "" {
		viewVars["EnableComment"] = true
	} else {
		viewVars["EnableComment"] = false
	}

	// Render comments
	comments := make([]map[string]string, 0)
	for i, v := range result.Comments {
		comments = append(comments, map[string]string{
			"Number":  strconv.Itoa(i + 1),
			"Date":    time.Unix(v.Created, 0).Format(blogConfig.Get("dateFormat")),
			"Id":      v.Id[0:9],
			"RealId":  v.Id,
			"Content": v.Content,
			"Author":  v.Author})
	}
	viewVars["Comments"] = comments

	if next, exists := p.GetNextId(objectIdHex(result.Id.String())); exists {
		viewVars["Next"] = next
	}
	if last, exists := p.GetLastId(objectIdHex(result.Id.String())); exists {
		viewVars["Last"] = last
	}

	sessionH := session.Start(ctx, h)
	defer sessionH.Save()
	viewVars["Admin"] = false
	if sessionH.Data["logged"] != nil {
		viewVars["Admin"] = true
	}

	output := mustache.RenderFile("templates/view-post.mustache", viewVars)
	return render(output, result.Title)
}
Esempio n. 4
0
func create_person(ctx *web.Context) {
	var p Personne
	Personnes := gouda.M(p)
	p.Id = 0
	p.Nom = ctx.Request.Params["nom"][0]
	p.Age, _ = strconv.Atoi(ctx.Request.Params["age"][0])
	Personnes.Save(p)
	ctx.Redirect(302, "/")
}
Esempio n. 5
0
/*redirects the shortened URL to the real URL*/
func redirect(ctx *web.Context, key string) {
	/*fetch our URL*/
	url, _ := redisClient.Get(getRedisKey(key))
	if url == nil {
		printError(ctx, "I can't find that URL")
	} else {
		/*redirect*/
		ctx.Redirect(301, string(url))
	}
}
Esempio n. 6
0
func newPostGet(ctx *web.Context) string {
	sessionH := session.Start(ctx, h)
	defer sessionH.Save()
	if sessionH.Data["logged"] == nil {
		ctx.Redirect(302, "/admin/login")
		return ""
	}
	output := mustache.RenderFile("templates/new-post.mustache")
	return render(output, "New Post")
}
Esempio n. 7
0
func update_person(ctx *web.Context) {
	var p Personne
	Personnes := gouda.M(p)
	i, _ := strconv.Atoi(ctx.Request.Params["id"][0])
	p = Personnes.Where(gouda.F("id").Eq(i)).First().(Personne)
	p.Nom = ctx.Request.Params["nom"][0]
	p.Age, _ = strconv.Atoi(ctx.Request.Params["age"][0])
	Personnes.Save(p)
	ctx.Redirect(302, "/person/"+fmt.Sprint(p.Id))
}
Esempio n. 8
0
func adminLoginGet(ctx *web.Context) string {
	sessionH := session.Start(ctx, h)
	defer sessionH.Save()
	if sessionH.Data["logged"] != nil {
		ctx.Redirect(302, "/admin/post/list")
		return ""
	}
	output := mustache.RenderFile("templates/admin-login.mustache")
	return render(output, "Login")
}
Esempio n. 9
0
func adminIndexGet(ctx *web.Context) string {
	sessionH := session.Start(ctx, h)
	defer sessionH.Save()
	if sessionH.Data["logged"] != nil {
		ctx.Redirect(302, "/admin/post/list")
		return ""
	}
	ctx.Redirect(302, "/admin/login")
	return ""
}
Esempio n. 10
0
// renders /
func index(ctx *web.Context) string {
	css, ok := ctx.Params["css"]
	if ok {
		SetCSS(ctx, css)
		ctx.Redirect(302, "/")
		return "ok"
	}
	//posts := postsForMonth(time.LocalTime()) //Db.GetLastNPosts(10)

	//	posts := lastPosts(0xff)
	posts := postsForLastNDays(4)
	if len(posts) <= 0 {
		posts = lastPosts(23)
	}
	//fmt.Printf("posts: %#v\n", posts)
	//embedded struct - our mustache templates need a NumOfComments field to render
	//but we don't want to put that field into the BlogPost Struct so it won't get stored
	//into the DB
	type MyPost struct {
		BlogPost
		NumOfComments int
	}

	//posts ordered by date. this is ugly. TODO: look up if mustache hase something to handle this situation
	type Date struct {
		Date  string
		Posts []MyPost
	}
	Db := DBGet()
	defer Db.Close()

	//loop through our posts and put them into the appropriate date structure
	dates := []Date{}
	var cur_date time.Time
	var date *Date
	for _, p := range posts {
		post_date := time.SecondsToLocalTime(p.Timestamp)
		if !(cur_date.Day == post_date.Day && cur_date.Month == post_date.Month && cur_date.Year == post_date.Year) {
			cur_date = *post_date
			dates = append(dates, Date{Date: cur_date.Format("Mon Jan _2 2006")})
			date = &dates[len(dates)-1]
		}
		p.Comments, _ = Db.GetComments(p.Id)
		mp := MyPost{p, len(p.Comments)}
		date.Posts = append(date.Posts, mp)
	}
	m := map[string]interface{}{
		"Dates": dates,
	}

	tmpl, _ := mustache.ParseFile("templ/index.mustache")
	s := tmpl.Render(&m, getCSS(ctx))
	return s
}
Esempio n. 11
0
// function to resolve a shorturl and redirect
func resolve(ctx *web.Context, short string) {
	kurl, err := load(short)
	if err == nil {
		go redis.Hincrby(kurl.Key, "Clicks", 1)
		ctx.Redirect(http.StatusMovedPermanently,
			kurl.LongUrl)
	} else {
		ctx.Redirect(http.StatusMovedPermanently,
			ROLL)
	}
}
Esempio n. 12
0
func newPostPost(ctx *web.Context) {
	sessionH := session.Start(ctx, h)
	defer sessionH.Save()
	if sessionH.Data["logged"] == nil {
		ctx.Redirect(302, "/admin/login")
		return
	}
	p := PostModelInit()
	p.Create(ctx.Params["title"], ctx.Params["content"])
	ctx.Redirect(302, "/admin/post/list")
}
Esempio n. 13
0
func adminDelPage(ctx *web.Context, id string) {
	sessionH := session.Start(ctx, h)
	defer sessionH.Save()
	if sessionH.Data["logged"] == nil {
		ctx.Redirect(302, "/admin/login")
		return
	}
	p := PageModelInit()
	p.Delete(id)

	ctx.Redirect(302, "/admin/page/list")
}
Esempio n. 14
0
func adminEditPagePost(ctx *web.Context, id string) {
	sessionH := session.Start(ctx, h)
	defer sessionH.Save()
	if sessionH.Data["logged"] == nil {
		ctx.Redirect(302, "/admin/login")
		return
	}
	p := PageModelInit()
	p.Update(ctx.Params["title"], ctx.Params["content"], id)

	ctx.Redirect(302, "/admin/page/list")
}
Esempio n. 15
0
func delPost(ctx *web.Context, postId string) {
	sessionH := session.Start(ctx, h)
	defer sessionH.Save()
	if sessionH.Data["logged"] == nil {
		ctx.Redirect(302, "/admin/login")
		return
	}
	p := PostModelInit()
	p.Delete(postId)

	ctx.Redirect(302, "/admin/post/list")
}
Esempio n. 16
0
func adminListPagesGet(ctx *web.Context) string {
	sessionH := session.Start(ctx, h)
	defer sessionH.Save()
	if sessionH.Data["logged"] == nil {
		ctx.Redirect(302, "/admin/login")
		return ""
	}
	p := PageModelInit()
	results := p.List()

	output := mustache.RenderFile("templates/list-pages.mustache", map[string][]map[string]string{"pages": results})
	return render(output, "List Pages")
}
Esempio n. 17
0
func editPostGet(ctx *web.Context, postId string) string {
	sessionH := session.Start(ctx, h)
	defer sessionH.Save()
	if sessionH.Data["logged"] == nil {
		ctx.Redirect(302, "/admin/login")
		return ""
	}
	p := PostModelInit()
	result := p.Get(postId)

	output := mustache.RenderFile("templates/edit-post.mustache", map[string]string{"Title": result.Title, "Content": result.Content, "id": objectIdHex(result.Id.String())})
	return render(output, "Edit Post")
}
Esempio n. 18
0
func person_delete(ctx *web.Context, val string) {
	var p Personne
	var c Car
	Personnes := gouda.M(p)
	Cars := gouda.M(c)
	i, _ := strconv.Atoi(val)
	p = Personnes.Where(gouda.F("id").Eq(i)).First().(Personne)
	cars := Personnes.GetAssociated("cars", p).([]interface{})
	for _, c := range cars {
		Cars.Delete(c.(Car))
	}
	Personnes.Delete(p)
	ctx.Redirect(302, "/")
}
Esempio n. 19
0
// function to display the info about a KurzUrl given by it's Key
func info(ctx *web.Context, short string) {

	if strings.HasSuffix(short, "+") {
		short = strings.Replace(short, "+", "", 1)
	}
	kurl, err := load(short)
	if err == nil {
		ctx.SetHeader("Content-Type", "application/json", true)
		ctx.Write(kurl.Json())
		ctx.WriteString("\n")
	} else {
		ctx.Redirect(http.StatusNotFound, ROLL)
	}
}
Esempio n. 20
0
func adminLoginPost(ctx *web.Context) {
	sessionH := session.Start(ctx, h)
	defer sessionH.Save()
	if sessionH.Data["logged"] != nil {
		ctx.Redirect(302, "/admin/post/list")
		return
	}
	if ctx.Params["username"] == config.Get("adminuser") && ctx.Params["password"] == config.Get("adminpasswd") {
		t := time.LocalTime()
		var h hash.Hash = sha1.New()
		h.Write([]byte(strconv.Itoa64(t.Seconds())))
		sessionH.Data["logged"] = hex.EncodeToString(h.Sum())
	}
	ctx.Redirect(302, "/admin/post/list")
}
Esempio n. 21
0
// function to shorten and store a url
func shorten(ctx *web.Context, data string) {
	host := config.GetStringDefault("hostname", "localhost")
	r, _ := ctx.Request.Params["url"]
	theUrl, err := isValidUrl(string(r))
	if err == nil {
		ctr, _ := redis.Incr(COUNTER)
		encoded := Encode(ctr)
		location := fmt.Sprintf("%s://%s/%s", HTTP, host, encoded)
		store(encoded, location, theUrl.Raw)
		// redirect to the info page
		ctx.Redirect(http.StatusMovedPermanently, location+"+")
	} else {
		ctx.Redirect(http.StatusNotFound, ROLL)
	}
}
Esempio n. 22
0
func listPost(ctx *web.Context) string {
	sessionH := session.Start(ctx, h)
	defer sessionH.Save()
	if sessionH.Data["logged"] == nil {
		ctx.Redirect(302, "/admin/login")
		return ""
	}
	page := 0
	if temp, exists := ctx.Params["page"]; exists {
		page, _ = strconv.Atoi(temp)
	}
	p := PostModelInit()
	results := p.PostListing(page)

	output := mustache.RenderFile("templates/list-post.mustache", map[string][]map[string]string{"posts": results})
	return render(output, "List Posts")
}
Esempio n. 23
0
func shorten(ctx *web.Context) {
	/*fetch URL and convert to string type*/
	url := fmt.Sprintf("%s", ctx.Request.Params["url"])
	/*crude REGEX to make sure URL is more or less a URL*/
	isURL, _ := regexp.MatchString("^http(s)?://.*", url)

	/*I think this is probably supposed to be a switch statement*/
	/*but it is my first Go app so I didn't get too crazy*/
	if url == "" {
		printError(ctx, "URL is missing. Return to Go. Do not collect $200.")
	} else if !isURL {
		printError(ctx, "That doesn't look like a URL.")
	} else {
		/*generate short key*/
		key := keyOf([]byte(url))
		/*set URL in Redis using a Redis-ized key*/
		err := redisClient.Set(getRedisKey(key), []byte(url))
		/*redirect to show page*/
		ctx.Redirect(301, fmt.Sprintf("/s/%s", key))
	}
}
Esempio n. 24
0
func editPost(ctx *web.Context) {
	if !checkGodLevel(ctx) {
		ctx.Redirect(301, "/")
		return
	}

	id, _ := strconv.Atoi64(ctx.Params["postid"])
	post, err := Db.GetPost(id)
	if err != nil {
		ctx.WriteString("couldn't load post with given id!")
		return
	}
	post.Content = ctx.Params["content"]
	_, err = Db.StorePost(&post)
	if err != nil {
		ctx.WriteString("couldn't store post!")
		return
	}

	ctx.WriteString(successpage)
}
Esempio n. 25
0
func adminPost(ctx *web.Context) {
	level := ctx.Params["godlevel"]
	godlevel := godHash(level)

	if ctx.Params["what"] == "login" {
		if godlevel == admin_pass {
			ctx.SetSecureCookie("godlevel", level, 3600)
			ctx.Redirect(301, "/admin")
			return
		}
		ctx.SetSecureCookie("godlevel", "fefe", 3600)
		ctx.Redirect(301, "/")
		return
	}

	if !checkGodLevel(ctx) {
		ctx.SetSecureCookie("godlevel", "fefe", 3600)
		ctx.Redirect(301, "/")
		return
	}

	if ctx.Params["what"] == "post" {
		err := createNewPost(ctx.Params["content"])
		if err != nil {
			ctx.WriteString("couldn't post: " + err.String())
			ctx.WriteString("<br><br><A href='/'>Index</a>")
			return
		}
		ctx.WriteString(successpage)
		return
	}
}
Esempio n. 26
0
func redirect(ctx *web.Context, handler, title string) {
	ctx.Redirect(302, urlPrefix+handler+"/"+safeTitle(title))
}
Esempio n. 27
0
// serves the index page aka "/", which is a redirect to
// whatever has been configured externally or a very
// special music video
func index(ctx *web.Context) {
	redirect := config.GetStringDefault("index", ROLL)
	ctx.Redirect(http.StatusMovedPermanently, redirect)
}
Esempio n. 28
0
func (c *Index) NewComment(ctx *web.Context, postId string) {
	p := PostModelInit()
	p.InsertComment(postId, ctx.Params["comment"], ctx.Params["author"])
	ctx.Redirect(302, "/post/"+postId)
}