Beispiel #1
0
/*
管理员注册
*/
func (this *Admin) RegisterHandler(rw http.ResponseWriter, req *http.Request) {
	logger.Infoln("entered Register()")
	switch req.Method {
	case "GET":
		t, err := template.ParseFiles("views/admin/register.html")
		if nil != err {
			logger.Errorln(err)
			return
		}
		if err = t.Execute(rw, nil); nil != err {
			logger.Errorln(err)
			return
		}
	case "POST":
		req.ParseForm()
		name := req.FormValue("username")
		psw := req.FormValue("password")
		confirmPsw := req.FormValue("confirm_password")
		if psw != confirmPsw {
			//TBD
		}
		model := models.Model{}
		model.AddUser(name, psw)
		http.Redirect(rw, req, "/", http.StatusFound)
	}
}
Beispiel #2
0
/*
管理员登录
*/
func (this *Admin) LoginHandler(rw http.ResponseWriter, req *http.Request) {
	logger.Infoln("entered Login()")
	switch req.Method {
	case "GET":
		t, err := template.ParseFiles("views/admin/login.html")
		if nil != err {
			logger.Errorln(err)
			return
		}
		if err = t.Execute(rw, nil); nil != err {
			logger.Errorln(err)
			return
		}
	case "POST":
		req.ParseForm()
		name := req.FormValue("username")
		psw := req.FormValue("password")
		if err := CheckNamePsw(name, psw); nil != err {
			logger.Errorln(err)
			http.Redirect(rw, req, "/login", http.StatusFound)
			return
		}
		SetCookie(rw, CreateSessionID(name))
		http.Redirect(rw, req, "/add", http.StatusFound)
	}
}
Beispiel #3
0
func main() {
	log.Println("listen on port :8888")
	logger.Infoln("Server Started, listen on port: 8888")

	register()
	web.Run(config.Config["host"])
}
Beispiel #4
0
func (this *Admin) AddBlogHandler(rw http.ResponseWriter, req *http.Request) {
	logger.Infoln("entered AddBlogHandler()")
	req.ParseForm()
	//name,err :=CheckCookie(r)
	//if err != nil{
	//	log.Println(err)
	//	http.Redirect(w,r,"/", http.StatusFound)
	//	return
	//	}
	switch req.Method {
	case "GET":
		t, err := template.ParseFiles("views/admin/add_blog.html")
		if nil != err {
			logger.Errorln(err)
			return
		}
		//查询博客标签
		m := models.Model{}
		tags, err := m.QueryTags()
		if nil != err {
			logger.Errorln(err)
		}
		//为了使用查询出来的博客标签,格式化查询结果
		type tmp struct {
			Tags []models.Tag
		}
		tmp2 := tmp{tags}
		if err = t.Execute(rw, tmp2); nil != err {
			logger.Errorln(err)
			return
		}
	case "POST":
		title := req.FormValue("title")
		content := req.FormValue("content")
		tag := req.FormValue("tag")
		tagId, _ := strconv.Atoi(tag) //从前端返回的是tag的ID
		logger.Debugln("title: ", title)
		logger.Debugln("content: ", content)
		logger.Debugln("arcticleTag: ", tag)

		model := models.Model{}
		model.AddBlog(title, content, tagId)
		http.Redirect(rw, req, "/", http.StatusFound)
	}
}
Beispiel #5
0
/*
显示所有文章
*/
func (this *Articles) ArticlesHandler(rw http.ResponseWriter, req *http.Request) {
	logger.Infoln("entered ArticlesHandler()")
	//username,err := CheckCookie(r)
	//if err != nil{
	//	log.Println(err)
	//http.Redirect(w,r,"/", http.StatusFound)
	//	}
	var blogs []models.Blog
	var err error
	var pageId int = 0
	switch req.Method {
	case "GET":
		switch {
		case "" != req.FormValue("title"):
			strTitle := req.FormValue("title")
			logger.Debugln(strTitle)
			titleId, _ := strconv.Atoi(strTitle)
			blogs, err = this.ArticleByTitle(titleId)
		case "" != req.FormValue("tag"):
			strTag := req.FormValue("tag")
			logger.Debugln(strTag)
			tagId, _ := strconv.Atoi(strTag)
			blogs, err = this.ArticleByTitle(tagId)
		case "" != req.FormValue("page"):
			strPage := req.FormValue("page")
			logger.Debugln(strPage)
			pageId, _ = strconv.Atoi(strPage)
			blogs, err = this.ArticleByPage(pageId)
		default:
			err = this.QueryAllBlogs()
			if nil != err {
				logger.Errorln(err)
				return
			}
			blogs = AllBlogs
		}
		//获取所有标签
		var tags []models.Tag
		tags, err = new(models.Model).QueryTags()
		if nil != err {
			logger.Errorln(err)
			return
		}

		//格式化所有博客和标签,以便template包使用
		type tmp struct {
			Blgs    []models.Blog
			Tags    []models.Tag
			Page    []int //多少页博客
			CurPage int   //现在显示哪一页
		}
		//为了在前端显示分页信息,make一个数组
		pageNum := 0
		if len(AllBlogs)%MaxPageNum == 0 {
			pageNum = len(AllBlogs) / MaxPageNum
		} else {
			pageNum = len(AllBlogs)/MaxPageNum + 1
		}
		var pages = make([]int, pageNum, pageNum)
		for i := 0; i < pageNum; i++ {
			pages[i] = i + 1
		}
		if 0 == pageId {
			pageId = pageId + 1
		}
		tmp2 := tmp{blogs, tags, pages, pageId}
		t := template.New("")
		t = t.Funcs(template.FuncMap{"plus": utils.Plus})
		//ParseFiles uses the filename as the template name inside of the template object.
		t, err = t.ParseFiles("views/articles/index.html")
		if nil != err {
			logger.Errorln(err)
			log.Println(err)
			return
		}

		//显示内容
		//ParseFiles uses the filename as the template name inside of the template object.
		//So, Have to use ExecuteTemplate() instead fo Execute(),
		//and use index.html as the name of t
		if err = t.ExecuteTemplate(rw, "index.html", tmp2); nil != err {
			logger.Errorln(err)
			log.Println(err)
			return
		}
	case "POST":
		//TODO
	}
}