登录
首页 >  Golang >  Go教程

技术博客建站教程——使用Beego开发

时间:2023-06-23 12:49:51 343浏览 收藏

对于一个Golang开发者来说,牢固扎实的基础是十分重要的,golang学习网就来带大家一点点的掌握基础知识点。今天本篇文章带大家了解《技术博客建站教程——使用Beego开发》,主要介绍了,希望对大家的知识积累有所帮助,快点收藏起来吧,否则需要时就找不到了!

一、前言
如今,技术博客已成为了程序员交流互动、展示技术、拓宽思路的重要平台之一。对于有一定编程基础的程序员来说,自己开发博客,实现个性化定制和自由扩展,已逐渐成为一种趋势。

本文将引导读者使用Beego框架搭建自己的技术博客,旨在提供一种方便高效且易于扩展的解决方案。

二、Beego框架简介
Beego是基于Go语言开发的Web框架,它的设计灵感来自于Python的Django框架和Python的Tornado框架。Beego是一个轻量级、简单易学、高效灵活的Web框架,同时也支持RESTful API开发。

三、环境搭建
1、安装Go环境
首先需要安装Go环境,具体步骤可查阅官方文档进行安装。
2、安装Beego和Bee工具
Beego和Bee是两个不同的工具,Beego是核心框架,而Bee是一个基于Beego框架的命令行工具,可用于新建项目、创建Controller、Model、View等,大大提高了开发效率。

使用命令安装:go get github.com/astaxie/beego
go get github.com/beego/bee

3、创建项目及配置
创建一个名为myblog的项目:bee new myblog
然后进入myblog目录:cd myblog
现在在myblog目录下会有一个名为conf的文件夹,里面的app.conf即为配置文件,我们可以在这里进行相关配置,如数据库的连接地址、端口等。

四、实现博客功能
1、模型设计
首先,需在models目录下编写blog.go文件,用于创建数据库的表,如下所示:

package models

import (

"github.com/astaxie/beego/orm"
"time"

)

//数据结构
//文章
type Article struct {

Id       int64     `orm:"auto"`
Title    string    `orm:"size(100)"`
Content  string    `orm:"type(text)"`
ImgUrl   string    `orm:"size(200)"`
Category *Category `orm:"-"`
Created  time.Time `orm:"auto_now_add;type(datetime)"`
Updated  time.Time `orm:"auto_now_add;type(datetime)"`

}

//分类
type Category struct {

Id       int64
Title    string
Articles []*Article `orm:"reverse(many)"`

}

2、Controller编写
在controllers目录下编写article.go文件,用于实现与文章相关的控制器方法,如下所示:

package controllers

import (

"myblog/models"
"fmt"
"strconv"
"time"

)

type ArticleController struct {

BaseController

}

func (this *ArticleController) List() {

categoryIdStr := this.GetString("category_id")
categoryId, _ := strconv.ParseInt(categoryIdStr, 10, 64)

categories := models.GetAllCategory()
this.Data["Categories"] = categories

var articles []*models.Article
if categoryId == 0 {
    articles = models.GetAllArticle()
} else {
    articles = models.GetArticleByCategory(categoryId)
}

this.Data["Articles"] = articles
this.Data["CategoryId"] = categoryId

this.TplName = "article/list.html"

}

func (this *ArticleController) Add() {

if this.Ctx.Request.Method == "GET" {
    categories := models.GetAllCategory()
    this.Data["Categories"] = categories
    this.TplName = "article/add.html"
    return
}

title := this.GetString("title")
content := this.GetString("content")
categoryId, _ := this.GetInt64("category_id")
imgUrl := this.GetString("img_url")

article := models.Article{Title: title, Content:content, ImgUrl:imgUrl, Category:&models.Category{Id:categoryId}}
models.AddArticle(&article)

fmt.Println("添加成功")
this.Redirect("/article/list", 302)

}

func (this *ArticleController) Update() {

id, _ := this.GetInt64(":id")

if this.Ctx.Request.Method == "GET" {

    article := models.GetArticleById(id)
    this.Data["Article"] = article

    categories := models.GetAllCategory()
    this.Data["Categories"] = categories

    this.TplName = "article/update.html"
    return
}

title := this.GetString("title")
content := this.GetString("content")
categoryId, _ := this.GetInt64("category_id")
imgUrl := this.GetString("img_url")

article := models.Article{Id: id, Title: title, Content:content, ImgUrl:imgUrl, Category:&models.Category{Id:categoryId}}
models.UpdateArticle(&article)

this.Redirect("/article/list", 302)

}

func (this *ArticleController) Delete() {

id, _ := this.GetInt64(":id")
models.DeleteArticleById(id)

this.Redirect("/article/list", 302)

}

func (this *ArticleController) Detail() {

id, _ := this.GetInt64(":id")

article := models.GetArticleById(id)
this.Data["Article"] = article

this.TplName = "article/detail.html"

}

3、View视图文件
在views目录下编写article目录,存放article相关的视图文件,如下所示:

//article/list.html
{{template "header.html" .}}

文章管理

全部 {{range .Categories}} {{.Title}} {{end}}
{{range .Articles}} {{end}}
Id 标题 分类 发布时间 更新时间 操作
{{.Id}} {{.Title}} {{.Category.Title}} {{.Created.Format "2006-01-02 15:04:05"}} {{.Updated.Format "2006-01-02 15:04:05"}} 查看 修改 删除


{{template "footer.html" .}}

//article/add.html
{{template "header.html" .}}

添加文章

标题:

分类:

图片Url:

内容:


{{template "footer.html" .}}

//article/update.html
{{template "header.html" .}}

修改文章

标题:

分类:

图片Url:

内容:


{{template "footer.html" .}}

//article/detail.html
{{template "header.html" .}}

{{.Article.Title}}

分类:{{.Article.Category.Title}}

发布时间:{{.Article.Created.Format "2006-01-02 15:04:05"}}

更新时间:{{.Article.Updated.Format "2006-01-02 15:04:05"}}

内容:

{{.Article.Content}}


{{template "footer.html" .}}

五、运行项目
在终端使用bee run命令启动项目,然后访问http://localhost:8080/article/list即可访问博客。

六、总结
本文简单介绍了Beego框架的使用,在此基础上实现了一个简单的博客应用程序。通过本文的学习,读者可初步了解Beego框架的基本使用方法,更多详细内容请参考官方文档。

今天关于《技术博客建站教程——使用Beego开发》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于Beego,建站教程,技术博客的内容请关注golang学习网公众号!

相关阅读
更多>
最新阅读
更多>
课程推荐
更多>