Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
woann committed Dec 20, 2018
1 parent 07f975f commit a08d87b
Show file tree
Hide file tree
Showing 603 changed files with 144,387 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
68 changes: 68 additions & 0 deletions common/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package common

import (
"bytes"
"github.com/astaxie/beego"
"math"
)


func MergeString (args ...string) string {
buffer := bytes.Buffer{}
for i:=0; i<len(args); i++ {
buffer.WriteString(args[i])
}
return buffer.String()
}

func Asset (path string) string {
return MergeString(beego.AppConfig.String("resources_url"), path)
}
func Paginator(page, pagesize int, nums int64) map[string]interface{} {

var prepage int //前一页地址
var nextpage int //后一页地址
//根据nums总数,和pagesize每页数量 生成分页总数
totalpages := int(math.Ceil(float64(nums) / float64(pagesize))) //page总数
if page > totalpages {
page = totalpages
}
if page <= 0 {
page = 1
}
var pages []int
switch {
case page >= totalpages-8 && totalpages > 8: //最后8页
start := totalpages - 8 + 1
prepage = page - 1
nextpage = int(math.Min(float64(totalpages), float64(page+1)))
pages = make([]int, 8)
for i, _ := range pages {
pages[i] = start + i
}
case page >= 5 && totalpages > 8:
start := page - 5 + 1
pages = make([]int, 8)
prepage = page - 5
for i, _ := range pages {
pages[i] = start + i
}
prepage = page - 1
nextpage = page + 1
default:
pages = make([]int, int(math.Min(8, float64(totalpages))))
for i, _ := range pages {
pages[i] = i + 1
}
prepage = int(math.Max(float64(1), float64(page-1)))
nextpage = page + 1
//fmt.Println(pages)
}
paginatorMap := make(map[string]interface{})
paginatorMap["pages"] = pages
paginatorMap["totalpages"] = totalpages
paginatorMap["prepage"] = prepage
paginatorMap["nextpage"] = nextpage
paginatorMap["currpage"] = page
return paginatorMap
}
5 changes: 5 additions & 0 deletions conf/app.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
appname = blog
httpport = 8080
runmode = "prod"
include "database.conf"
include "user.conf"
7 changes: 7 additions & 0 deletions conf/database.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mysqluser = "root"
mysqlpass = "wqg951122"
mysqlhost = "127.0.0.1"
mysqlport= "3306"
mysqldb = "blog"
prefix = "b_"
charset = "utf8mb4"
1 change: 1 addition & 0 deletions conf/user.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
resources_url = "https://admin.woann.cn"
67 changes: 67 additions & 0 deletions controllers/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package controllers

import (
"blog/common"
"blog/models"
"github.com/astaxie/beego"
"html/template"
)

type ErrorController struct {
beego.Controller
}

func (this *ErrorController) Error404() {
//获取博客标题,声明,寄语
config := models.GetConfig("blog_title", "blog_avatar", "favicon_icon", "notice", "word", "keywords", "description", "website_title")
if value, ok := config.(map[string]string); ok {
this.Data["title"] = value["blog_title"]
this.Data["avatar"] = common.Asset(value["blog_avatar"])
this.Data["favicon_icon"] = common.Asset(value["favicon_icon"])
this.Data["notice"] = value["notice"]
this.Data["word"] = template.HTML(value["word"])
this.Data["keywords"] = value["keywords"]
this.Data["description"] = value["description"]
this.Data["website_title"] = "404"
}
this.Data["categorys"] = models.CategoryList(0)
this.TplName = "system/404.tpl"
this.Layout = "base/layout.tpl"
}

func (this *ErrorController) Error501() {
//获取博客标题,声明,寄语
config := models.GetConfig("blog_title", "blog_avatar", "favicon_icon", "notice", "word", "keywords", "description", "website_title")
if value, ok := config.(map[string]string); ok {
this.Data["title"] = value["blog_title"]
this.Data["avatar"] = common.Asset(value["blog_avatar"])
this.Data["favicon_icon"] = common.Asset(value["favicon_icon"])
this.Data["notice"] = value["notice"]
this.Data["word"] = template.HTML(value["word"])
this.Data["keywords"] = value["keywords"]
this.Data["description"] = value["description"]
this.Data["website_title"] = "404"
}
this.Data["categorys"] = models.CategoryList(0)
this.TplName = "system/404.tpl"
this.Layout = "base/layout.tpl"
}


func (this *ErrorController) ErrorDb() {
//获取博客标题,声明,寄语
config := models.GetConfig("blog_title", "blog_avatar", "favicon_icon", "notice", "word", "keywords", "description", "website_title")
if value, ok := config.(map[string]string); ok {
this.Data["title"] = value["blog_title"]
this.Data["avatar"] = common.Asset(value["blog_avatar"])
this.Data["favicon_icon"] = common.Asset(value["favicon_icon"])
this.Data["notice"] = value["notice"]
this.Data["word"] = template.HTML(value["word"])
this.Data["keywords"] = value["keywords"]
this.Data["description"] = value["description"]
this.Data["website_title"] = "404"
}
this.Data["categorys"] = models.CategoryList(0)
this.TplName = "system/404.tpl"
this.Layout = "base/layout.tpl"
}
150 changes: 150 additions & 0 deletions controllers/index.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package controllers

import (
"blog/common"
"blog/models"
"fmt"
"github.com/astaxie/beego"
"html/template"
"strconv"
)

type IndexController struct {
beego.Controller
}
/**
* 公共部分
*/
func public(this *IndexController, categoryId int){
//获取博客标题,声明,寄语
config := models.GetConfig("blog_title", "blog_avatar", "favicon_icon", "notice", "word", "keywords", "description", "website_title")
if value, ok := config.(map[string]string); ok {
this.Data["title"] = value["blog_title"]
this.Data["avatar"] = common.Asset(value["blog_avatar"])
this.Data["favicon_icon"] = common.Asset(value["favicon_icon"])
this.Data["notice"] = value["notice"]
this.Data["word"] = template.HTML(value["word"])
this.Data["keywords"] = value["keywords"]
this.Data["description"] = value["description"]
this.Data["website_title"] = value["website_title"]
}
this.Data["categorys"] = models.CategoryList(categoryId)
this.Data["top"] = models.ArticleTop()
this.Data["recommend"] = models.ArticleSadeBar("recommend")
this.Data["hot"] = models.ArticleSadeBar("hot")
}

/**
* 首页
*/
func (this *IndexController) Index () {
public(this,0)
//获取搜索关键字
wd := this.GetString("wd")
//获取轮播列表
this.Data["banners"] = models.BannerList()
//分页
page,_ := this.GetInt("page")
if page == 0{
page = 1
}
total := models.ArticleTotal(0)
pagesize := 10
this.Data["paginator"] = common.Paginator(page,pagesize,total)
//获取文章列表
this.Data["list"] = models.ArticleListByTime(page, pagesize, wd)
this.Layout = "base/layout.tpl"
this.TplName = "index.tpl"
this.LayoutSections = make(map[string]string)
this.LayoutSections["SideBar"] = "base/sidebar.tpl"
this.LayoutSections["Script"] = "script/index_js.tpl"
}


/**
* 列表页
*/
func (this *IndexController) List() {
cid := this.Ctx.Input.Param(":id")//获取分类id
categoryId, _ := strconv.Atoi(cid)//转为int
category,err := models.CategoryOne(categoryId)//获取分类信息
//如果分类不存在->404
if err != nil {
this.Abort("404")
}
//加载公共数据
public(this, categoryId)
this.Data["category_id"] = categoryId
this.Data["category"] = category
//分页
page,_ := this.GetInt("page")
if page == 0{
page = 1
}
total := models.ArticleTotal(categoryId)
pagesize := 10
this.Data["paginator"] = common.Paginator(page,pagesize,total)
//分类下文章列表数据
this.Data["list"] = models.ArticleListByCategory(categoryId, page, pagesize)
//模板渲染
this.Layout = "base/layout.tpl"
this.TplName = "list.tpl"
this.LayoutSections = make(map[string]string)
this.LayoutSections["SideBar"] = "base/sidebar.tpl"
}
/**
* 文章详情
*/
func (this *IndexController) Article() {
articleId := this.Ctx.Input.Param(":id")//获取文章id
id, _ := strconv.Atoi(articleId)//转为int
article, err := models.ArticleOne(id)//获取文章数据
fmt.Println(article)
//阅读数+1
models.ArticleIncrViews(id)
//如果文章不存在跳到404页
if err != nil{
this.Abort("404")
}
//加载公共数据
public(this, 0)
preArticle, err := models.ArticleOne(id - 1)
nextArticle, err := models.ArticleOne(id + 1)
fmt.Println(preArticle)
fmt.Println(nextArticle)
this.Data["article"] = article//文章详情
this.Data["about"] = models.ArticleAbout(article.TagSlice)//相关文章
this.Data["pre_article"] = preArticle//上一篇
this.Data["next_article"] = nextArticle//下一篇
//渲染模板
this.Layout = "base/layout.tpl"
this.TplName = "article.tpl"
this.LayoutSections = make(map[string]string)
this.LayoutSections["SideBar"] = "base/sidebar_2.tpl"
this.LayoutSections["Script"] = "script/article_js.tpl"
}

func (this *IndexController) Tag() {
public(this,0)
//获取参数
name := this.GetString("name")
color := this.GetString("color")
this.Data["color"] = color
this.Data["name"] = name
//分页
page,_ := this.GetInt("page")
if page == 0{
page = 1
}
total := models.ArticleTotal(0)
pagesize := 10
this.Data["paginator"] = common.Paginator(page,pagesize,total)
//获取文章列表
this.Data["list"] = models.ArticleListByTime(page, pagesize, name)
//渲染页面
this.Layout = "base/layout.tpl"
this.TplName = "tag.tpl"
this.LayoutSections = make(map[string]string)
this.LayoutSections["SideBar"] = "base/sidebar.tpl"
}

Binary file added main
Binary file not shown.
28 changes: 28 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"blog/controllers"
_ "blog/routers"
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
_ "github.com/go-sql-driver/mysql"
"time"
)
func init() {
orm.Debug = false
orm.RegisterDriver("mysql",orm.DRMySQL)
mysqluser := beego.AppConfig.String("mysqluser")
mysqlpass := beego.AppConfig.String("mysqlpass")
mysqlhost := beego.AppConfig.String("mysqlhost")
mysqlport := beego.AppConfig.String("mysqlport")
mysqldb := beego.AppConfig.String("mysqldb")
charset := beego.AppConfig.String("charset")
orm.RegisterDataBase("default","mysql",mysqluser+":"+mysqlpass+"@tcp("+mysqlhost+":"+mysqlport+")/"+mysqldb+"?charset="+charset)
orm.DefaultTimeLoc = time.UTC
}

func main() {
beego.ErrorController(&controllers.ErrorController{})
beego.Run()
}

Loading

0 comments on commit a08d87b

Please sign in to comment.