Exemplo n.º 1
0
func LoadPost(ctx *web.Context, val string) {
	username := ctx.Params["username"]
	password := ctx.Params["password"]

	salt := strconv.Itoa64(time.Nanoseconds()) + username

	var h hash.Hash = sha256.New()
	h.Write([]byte(password + salt))

	s, _err := conn.Prepare("INSERT INTO users VALUES(NULL, ?, ?, ?)")
	utils.ReportErr(_err)

	s.Exec(username, string(h.Sum()), salt)
	s.Finalize()
	conn.Close()
	sidebar := utils.Loadmustache("admin.mustache", &map[string]string{})

	//TESTING, REMOVE LATER
	script := "<script type=\"text/javascript\" src=\"../inc/adminref.js\"></script>"
	content := "Welcome to the admin panel, use the control box on your right to control the site content"
	//ENDTESTING

	mapping := map[string]string{"css": "../inc/site.css",
		"title":   "Proggin: Admin panel",
		"sidebar": sidebar,
		"content": content,
		"script":  script}

	output := utils.Loadmustache("frame.mustache", &mapping)
	ctx.WriteString(output)
}
Exemplo n.º 2
0
func get_delete(ctx *web.Context, id string) {
	log.Printf("get_delete %s\n", id)
	if e, err := Load(id); err == nil {
		ctx.WriteString(page(edit_form("/delete", e.Id, e.Date, e.Body, "Really delete")))
	} else {
		ctx.WriteString(page("<p>Invalid ID</p>"))
	}
}
Exemplo n.º 3
0
func getStyle(context *web.Context) {
	b, err := ioutil.ReadFile("style.css")
	context.SetHeader("Content-Type", "text/css", true)
	if err != nil {
		context.WriteString(err.String())
	}

	context.Write(b)
}
Exemplo n.º 4
0
func Get(ctx *web.Context, val string) {
	v := strings.Split(val, "/", 2)
	controllerName := ""
	actionName := ""
	if len(v) == 2 {
		controllerName, actionName = v[0], v[1]
	} else if len(v) == 1 {
		controllerName = v[0]
		actionName = "index"
	}

	if conType, ok := C.Controllers[controllerName]; ok {
		conTypePtr := reflect.PtrTo(conType)
		actionMethName := strings.ToUpper(string(actionName[0:1])) + actionName[1:]
		var actionMeth reflect.Method
		found := false
		for i := 0; i < conTypePtr.NumMethod(); i++ {
			if conTypePtr.Method(i).Name == actionMethName {
				actionMeth = conTypePtr.Method(i)
				found = true
				break
			}
		}
		if !found {
			return
		}
		conValue := reflect.New(conType)
		conIndirect := reflect.Indirect(conValue)

		// Inject Params
		conIndirect.FieldByName("Params").Set(reflect.ValueOf(ctx.Request.Params))

		// Inject beans
		for beanName, setterFunc := range bean.Registry() {
			if _, ok := conType.FieldByName(beanName); ok {
				if f := conIndirect.FieldByName(beanName); f.IsValid() {
					f.Set(reflect.ValueOf(setterFunc()))
				}
			}
		}

		action := actionMeth.Func
		ret := action.Call([]reflect.Value{conValue})
		if len(ret) == 2 {
			m := ret[0].Interface().(mv.Model)
			v := ret[1].Interface().(mv.View)
			controllerName = v.String()
			ctx.WriteString(mustache.RenderFile("app/view/"+controllerName+"/index.m", m))
		} else if len(ret) == 1 {
			m := ret[0].Interface().(mv.Model)
			ctx.WriteString(mustache.RenderFile("app/view/"+controllerName+"/"+actionName+".m", m))
		}
	}
	return
}
Exemplo n.º 5
0
func get_specific_id(ctx *web.Context, id string) {
	log.Printf("get_specific_id %s\n", id)
	if e, err := Load(id); err == nil {
		t := entry_template
		m := map[string]interface{}{"Id": e.Id, "Date": e.Date, "Body": e.Body}
		s := mustache.Render(t, m)
		ctx.WriteString(page(s))
	} else {
		ctx.WriteString(page(fmt.Sprintf("<p>Invalid ID</p> <!--%v-->", err)))
	}
}
Exemplo n.º 6
0
func pageRouter(ctx *web.Context, route string) {
	switch route {
	case "search":
		json, e := Search(ctx)
		if e != nil {
			ctx.WriteString(e.String())
		}
		ctx.Write(json)
	default:

	}
}
Exemplo n.º 7
0
func get_from(ctx *web.Context, id string) {
	log.Printf("get_root\n")
	p := `{{#entries}}` + entry_template + `{{/entries}}` + footer_template
	t := page(p)
	m := make(map[string]interface{})
	entries, _ := LoadRange(id, *max_entries)
	m["entries"] = entries
	m["id"] = id
	if len(entries) == *max_entries {
		m["from_id"] = entries[len(entries)-1].Id
	}
	s := mustache.Render(t, m)
	ctx.WriteString(s)
}
Exemplo n.º 8
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)
	}
}
Exemplo n.º 9
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
	}
}
Exemplo n.º 10
0
func Get(ctx *web.Context, val string) {
	v := strings.Split(val, "/", 2)
	controllerName, actionName := v[0], v[1]
	if action, ok := C.Controllers[controllerName+"/"+actionName]; ok {
		for beanName, setterFunc := range bean.Registry {
			if target, ok := C.Injectables[controllerName+"."+beanName]; ok {
				v := reflect.ValueOf(setterFunc())
				reflect.Indirect(target).Set(reflect.Indirect(v))
			}
		}
		ret := action.Call([]reflect.Value{})
		if len(ret) == 2 {
			m := ret[0].Interface().(mv.Model)
			v := ret[1].Interface().(mv.View)
			controllerName = v.String()
			ctx.WriteString(mustache.RenderFile("app/view/"+controllerName+"/index.m", m))
		} else if len(ret) == 1 {
			m := ret[0].Interface().(mv.Model)
			ctx.WriteString(mustache.RenderFile("app/view/"+controllerName+"/"+actionName+".m", m))
		}
	}
	return
}
Exemplo n.º 11
0
func get_rss(ctx *web.Context) {
	log.Printf("get_rss\n")
	ctx.SetHeader("Content-Type", "application/rss+xml", false)
	t := rss_template
	m := map[string]interface{}{"title": *title, "url": *url}
	if entries, err := LoadRange("", *max_entries); err == nil {
		type RSSEntry struct {
			Entry
			Guid    string
			RssDate string
		}
		rss_entries := make([]RSSEntry, len(entries))
		for i, _ := range entries {
			re := RSSEntry{entries[i], fmt.Sprintf("%s/%s", *url, entries[i].Id), nctime_to_rsstime(entries[i].Date)}
			rss_entries[i] = re
		}
		m["entries"] = rss_entries
		m["most_recent_date"] = nctime_to_rsstime(entries[0].Date)
		s := mustache.Render(t, m)
		ctx.WriteString(s)
	} else {
		ctx.WriteString(page(fmt.Sprintf("<p>Error generating RSS: %s</p>", err)))
	}
}
Exemplo n.º 12
0
func post_delete(ctx *web.Context) {
	id, id_ok := ctx.Request.Params["form_id"]
	pass, pass_ok := ctx.Request.Params["form_password"]
	if id_ok && pass_ok && len(id) > 0 {
		if err := Delete(id, Hash(pass)); err == nil {
			ctx.WriteString(page("<p>Delete successful</p>"))
		} else {
			ctx.WriteString(page(fmt.Sprintf("<p>Delete failed: %v</p>", err)))
		}
	} else {
		ctx.WriteString(page("<p>Invalid form data</p>"))
	}
}
Exemplo n.º 13
0
func post_post(ctx *web.Context) {
	date, date_ok := ctx.Request.Params["form_date"]
	body, body_ok := ctx.Request.Params["form_body"]
	pass, pass_ok := ctx.Request.Params["form_password"]
	if date_ok && body_ok && pass_ok && len(date) > 0 && len(body) > 0 {
		if err := Save(Entry{"", date, body}, Hash(pass)); err == nil {
			ctx.WriteString(page("<p>Post successful</p>"))
		} else {
			ctx.WriteString(page(fmt.Sprintf("<p>Post failed: %v</p>", err)))
		}
	} else {
		ctx.WriteString(page("<p>Invalid form data</p>"))
	}
}
Exemplo n.º 14
0
//Returns a json array with information about the last shortened urls. If data
// is a valid integer, that's the amount of data it will return, otherwise
// a maximum of 10 entries will be returned.
func latest(ctx *web.Context, data string) {
	howmany, err := strconv.Atoi64(data)
	if err != nil {
		howmany = 10
	}
	c, _ := redis.Get(COUNTER)

	last := c.Int64()
	upTo := (last - howmany)

	ctx.SetHeader("Content-Type", "application/json", true)
	ctx.WriteString("{ \"urls\" : [")
	for i := last; i > upTo && i > 0; i -= 1 {
		kurl, err := load(Encode(i))
		if err == nil {
			ctx.Write(kurl.Json())
			if i != upTo+1 {
				ctx.WriteString(",")
			}
		}
	}
	ctx.WriteString("] }")
	ctx.WriteString("\n")
}
Exemplo n.º 15
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)
}
Exemplo n.º 16
0
func printError(ctx *web.Context, error string) {
	/*this is a 500 error condition*/
	ctx.StartResponse(500)
	/*print boilerplate error page with passed in message*/
	ctx.WriteString(mustache.RenderFile("error.mustache", map[string]string{"error": error}))
}
Exemplo n.º 17
0
func get_css(ctx *web.Context, path string) {
	log.Printf("get_css\n")
	ctx.SetHeader("Content-Type", "text/css", false)
	ctx.WriteString(css_str)
}
Exemplo n.º 18
0
func get_post(ctx *web.Context) {
	log.Printf("get_post\n")
	ctx.WriteString(page(edit_form("/post", "", "", "", "Post")))
}
Exemplo n.º 19
0
/*shows the "shortened" URL to the user*/
func show(ctx *web.Context, key string) {
	ctx.WriteString(mustache.RenderFile("show.mustache", map[string]string{"key": key}))
}
Exemplo n.º 20
0
/*just prints a basic form to receive a URL*/
func index(ctx *web.Context) {
	ctx.WriteString(mustache.RenderFile("index.mustache"))
}