首页
学习
活动
专区
工具
TVP
发布

GIN框架博客篇(1)基础模板

GINBLOG篇

Gin博客篇

--基础模板篇

首先创建4个基本的文件夹

前提:在开始之前先配置好mysql,创建名为ginblog的数据库。

config.ini(配置文件)

[server]

#debug开发模式,release生产模式

AppMode=debug

HttpPort=:8080

[database]

Db=mysql

DbHost=127.0.0.1

DbPort=3306

DbUser=root

DbPassWord=123456

DbName=ginblog

setting.go

packageutils

import(

"fmt"

"gopkg.in/ini.v1"

)

var(

AppModestring

HttpPortstring

Dbstring

DbHoststring

DbPoststring

DbUserstring

DbPasswordstring

DbNamestring

)

funcinit() {

file,err:=ini.Load("./config/config.ini")

iferr!=nil{

fmt.Println("配置文件读取错误,请检查文件路径:",err)

}

LoadServer(file)

LoadData(file)

}

funcLoadData(file*ini.File) {

Db=file.Section("database").Key("Db").MustString("mysql")

DbHost=file.Section("database").Key("DbHost").MustString("localhost")

DbPost=file.Section("database").Key("DbPort").MustString("3306")

DbUser=file.Section("database").Key("DbUser").MustString("root")

DbPassword=file.Section("database").Key("DbPassWord").MustString("123456")

DbName=file.Section("database").Key("DbName").MustString("ginblog")

}

funcLoadServer(file*ini.File) {

AppMode=file.Section("server").Key("AppMode").MustString("debug")

HttpPort=file.Section("server").Key("HttpPort").MustString("8080")

}

配置文件这里就配置好了,配置文件的作用就是为了方便修改变量,比如MySQL以及账号,密码,数据库名,再就是接口和本地地址。

Mysql数据库篇:

这里使用GORM来创建三个表,分别用作存储文章内容,文章类型,用户的账户和密码。

文章内容

typeArticlestruct{

CategoryCategory`gorm:"foreignKey:Cid"`

gorm.Model

Titlestring`gorm:"type:varchar(100);not null" json:"title"`

Cidint`gorm:"type:int;not null" json:"cid"`

Descint`gorm:"type:varchar(200)" json:"desc"`

Contentstring`gorm:"type:longtext" json:"content"`

Imgstring`gorm:"type:varchar(100)" json:"img"`

}

文章类型

typeCategorystruct{

gorm.Model

IDuint`gorm:"primary_key;auto_increment" json:"id"`

Namestring`gorm:"type:varchar(20);not null" json:"name"`

}

用户信息

typeUserstruct{

gorm.Model

Usernamestring`gorm:"type:varchar(20);not null " json:"username"`

Passwordstring`gorm:"type:varchar(20);not null" json:"password"`

Roleint`gorm:"type:int" json:"role"`

}

我们的目的是照着这三个结构体的内容在数据库中形成3张表。(使用GORM来创建)

然后使用数据库创建

packagemodel

import(

"fmt"

"ginblog/utils"

"gorm.io/driver/mysql"

"gorm.io/gorm"

)

vardb*gorm.DB

varerrerror

funcInitDb() {

dsn:=fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",

utils.DbUser,

utils.DbPassword,

utils.DbHost,

utils.DbPost,

utils.DbName,

)

db,err:=gorm.Open(mysql.Open(dsn),&gorm.Config{})

iferr!=nil{

fmt.Printf("连接数据库失败,请检查参数: ",err)

}

db.AutoMigrate(&User{},&Category{},&Article{})

//db.AutoMigrate(&User{},&Category{})

}

在main方法中运行一次model.InitDb(),三张表就会在mysql生成,并且每张表后面都会有s。

再就是路由器

packagerouters

import(

"ginblog/utils"

"github.com/gin-gonic/gin"

"http/htp"

)

funcInitRouter() {

gin.SetMode(utils.AppMode)

r:=gin.Default()

router:=r.Group("api/v1")

{

     router.GET("/hello",func(context*gin.Context){

         c.JSON(

             http.StatusOK,gin.H{

                 "message":"hello"

            }

        )

    })

}

r.Run()

}

这就是一个基础的模板,功能的增删改查,路由的控制器写法,将在下次讲解。

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20201231A00G7X00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券