Go学习日记5

1.goadmin plugin
解析:plugin有自己的路由与控制器方法,在接收由adapter转换过来的context后经控制器方法处理返回给adapter再输出到web框架中去。

2.goadmin template
解析:template是前端代码对应的golang实体化,前端代码对应的组件部分,比如表单,行,列等实体化为golang的一个接口,因此可以通过调用接口方法获取到该组件的html代码,此功能提供给插件去调用。

3.AddField方法
解析:
[1]第一个参数为标题
[2]第二参数为字段名
[3]第三个参数为字段的数据库类型

4.GoAdmin页面模块化
解析:页面自定义需要调用引擎的Content方法,需要返回一个对象types.Panel。如下所示:

type Panel struct {
    Content     template.HTML   // 页面内容
    Title       string          // 页面标题
    Description string          // 页面描述
    Url         string
}

5.定义Map
解析:可以使用内建函数make也可以使用map关键字来定义Map:

/* 声明变量,默认map是nil */
var map_variable map[key_data_type]value_data_type
/* 使用make函数 */
map_variable := make(map[key_data_type]value_data_type)

6.:=
解析:它是声明并赋值,并且系统自动推断类型,不需要var关键字。

7.T-SQL
解析:T-SQL即Transact-SQL,它是SQL在Microsoft SQL Server上的增强版,它是用来让应用程序与SQL Server沟通的主要语言。T-SQL提供标准SQL的DDL和DML功能,加上延伸的函数、系统预存程序以及程式设计结构[例如IF和WHILE]让程式设计更有弹性。

8.导入包后自定义引用的包名
解析:

import (
    "crypto/rand"
    mrand "math/rand"
)

9.Adminlte内置UI组件
解析:
[1]infobox
[2]progress-group
[3]productlist
[4]smallbox
[5]description
[6]chart legend
说明:GoAdmin系统提供的组件chartjs。

10.GoAdmin Col列
解析:一个col列对应的是ColAttribute这个类型,有三个方法:

type ColAttribute interface {
    SetSize(value map[string]string) ColAttribute   // 设置大小
    SetContent(value template.HTML) ColAttribute    // 设置本列的内容
    GetContent() template.HTML                      // 获取内容
}

11.GoAdmin Row行
解析:一个row行对应的是RowAttribute这个类型,有两个方法如下:

type RowAttribute interface {
    SetContent(value template.HTML) RowAttribute  // 设置内容
    GetContent() template.HTML                    // 获取内容
}

12.types.Panel
解析:
[1]Content:内容
[2]Title:标题
[3]Description:描述
[4]MiniSidebar:是否缩小边栏
[5]AutoRefresh:是否自动刷新页面
[6]RefreshInterval:自动刷新页面时间间隔,单位为秒

13.增加登录组件
解析:template.AddLoginComp(login.GetLoginComponent())

14.goadmin auth
解析:auth实现了对cookie的管理,将前端的cookie存储并转换为登录的用户,同时实现了对权限的拦截。

15.make lint
解析:代码规范。

16.Go语言type关键字
解析:
[1]定义结构体
[2]定义接口
[3]类型别名
[4]类型定义
[5]类型开关

17.GoAdmin适配器中的Use
解析:Use接收两个参数,第一个参数类型为interface{},是web框架的context,第二参数为插件数组。返回值是一个error。Use的作用是利用传入的插件数组,将web框架的路由与将插件数组中的控制器方法关联起来,实现web框架与GoAdmin插件方法的对应。

18.GoAdmin适配器中的Content
解析:Content方法接收两个参数,第一个参数类型为interface{},是web框架的context,第二参数为types.GetPanel类型。

type GetPanel func(ctx interface{}) (Panel, error)

Content的作用就是自定义页面的时候,传入web框架的context,然后往web框架的context写入自定页面内容的返回。

19.GoAdmin适配器中的init
解析:init方法往engine中注入该适配器,从而能够被别人去使用。

func init() {
    engine.Register(new(Beego))
}

20.GoAdmin插件
解析:一个插件需要实现对应的三个接口:

type Plugin interface {
    // 以下这两个接口内容基本是固定的,可参考后面的example插件,照写即可
    // 获取请求
    GetRequest() []context.Path
    // 获取控制器方法
    GetHandler(url, method string) context.Handlers
    // 初始化插件,接口框架的服务列表
    InitPlugin(services service.List)
}

21.GoAdmin模板开发
解析:主题模板是ui的抽象表示,包括一系列组件和静态资源的集合,会在插件中被调用。在GoAdmin中的定义如下:

type Template interface {
    Form() types.FormAttribute
    Col() types.ColAttribute
    Table() types.TableAttribute
    DataTable() types.DataTableAttribute
    Row() types.RowAttribute
    Tree() types.TreeAttribute
    Paginator() types.PaginatorAttribute
    Label() types.LabelAttribute
    Image() types.ImgAttribute
    Alert() types.AlertAttribute
    Tabs() types.TabsAttribute
    Popup() types.PopupAttribute
    // 资源函数
    GetTmplList() map[string]string
    GetAssetList() []string
    GetAsset(string) ([]byte, error)
    GetTemplate(bool) (*template.Template, string)
}

说明:如果需要开发一个ui主题模板,需要实现以上的Template接口。cli工具会帮助开发一个模板。

22.goadmin中的adm安装
解析:go install github.com/GoAdminGroup/go-admin/adm

23.增加配置与插件,使用Use方法挂载到Web框架中
解析:

_ = eng.AddConfig(cfg).AddPlugins(adminPlugin).Use(r)

24.GoAdmin全局配置项说明
解析:

package config

import (
    "html/template"
)
type Database struct {
    Host         string  // 地址
    Port         string  // 端口
    User         string  // 用户名
    Pwd          string  // 密码
    Name         string  // 数据库名
    MaxIdleCon   int     // 最大闲置连接数
    MaxOpenCon   int     // 最大打开连接数
    Driver       string  // 驱动名
    File         string  // 文件名
}
// 数据库配置
// 为一个map,其中key为数据库连接的名字,value为对应的数据配置
// 注意:key为default的数据库是默认数据库,也是框架所用的数据库,可以
// 配置多个数据库,提供给你的业务表使用,实现对不同数据库的管理
type DatabaseList map[string]Database

// 存储目录:存储头像等上传文件
type Store struct {
    Path   string
    Prefix string
}

type Config struct {
    // 数据库配置
    Databases DatabaseList `json:"database"`
    // 登录域名
    Domain string `json:"domain"`
    // 网站语言
    Language string `json:"language"`
    // 全局的管理前缀
    UrlPrefix string `json:"prefix"`
    // 主题名
    Theme string `json:"theme"`
    // 上传文件存储的位置
    Store Store `json:"store"`
    // 网站的标题
    Title string `json:"title"`
    // 侧边栏上的Logo
    Logo template.HTML `json:"logo"`
    // 侧边栏上的Logo缩小版
    MiniLogo template.HTML `json:"mini_logo"`
    // 登录后跳转的路由
    IndexUrl string `json:"index"`
    // 是否开始debug模式
    Debug bool `json:"debug"`
    // Info日志路径
    InfoLogPath string `json:"info_log"`
    // Error log日志路径
    ErrorLogPath string `json:"error_log"`
    // Access log日志路径
    AccessLogPath string `json:"access_log"`
    // 是否开始数据库Sql操作日志
    SqlLog bool `json:"sql_log"`
    // 是否关闭access日志
    AccessLogOff bool `json:"access_log_off"`
    // 是否关闭info日志
    InfoLogOff   bool `json:"info_log_off"`
    // 是否关闭error日志
    ErrorLogOff  bool `json:"error_log_off"`
    // 网站颜色主题
    ColorScheme string `json:"color_scheme"`
    // Session的有效时间,单位为秒
    SessionLifeTime int `json:"session_life_time"`
    // Cdn链接,为全局js/css配置cdn链接
    AssetUrl string `json:"asset_url"`
    // 文件上传引擎
    FileUploadEngine FileUploadEngine `json:"file_upload_engine"`
    // 自定义头部js/css
    CustomHeadHtml template.HTML `json:"custom_head_html"`
    // 自定义尾部js/css
    CustomFootHtml template.HTML `json:"custom_foot_html"`
    // 登录页面标题
    LoginTitle string `json:"login_title"`
    // 登录页面logo
    LoginLogo template.HTML `json:"login_logo"`
}

25.GoAdmin插件的使用
解析:框架的插件内容包括:控制器,路由以及视图。
[1]第三方包插件

package main

import (
    "github.com/gin-gonic/gin"
    _ "github.com/GoAdminGroup/go-admin/adapter/gin" // 必须引入,如若不引入,则需要自己定义
    _ "github.com/GoAdminGroup/themes/adminlte" // 必须引入,不然报错
    _ "github.com/GoAdminGroup/go-admin/modules/db/drivers/mysql"
    "github.com/GoAdminGroup/go-admin/engine"
    "github.com/GoAdminGroup/go-admin/plugins/admin"
    "github.com/GoAdminGroup/go-admin/plugins/example"
    "github.com/GoAdminGroup/go-admin/modules/config"
    "github.com/GoAdminGroup/go-admin/examples/datamodel"
)

func main() {
    r := gin.Default()
    eng := engine.Default()
    cfg := config.Config{}

    adminPlugin := admin.NewAdmin(datamodel.Generators)
    examplePlugin := example.NewExample()

    eng.AddConfig(cfg).
        AddPlugins(adminPlugin, examplePlugin).  // 加载插件
        Use(r)

    r.Run(":9033")
}

[2]二进制插件

package main

import (
    "github.com/gin-gonic/gin"
    _ "github.com/GoAdminGroup/go-admin/adapter/gin" // 必须引入,如若不引入,则需要自己定义
    _ "github.com/GoAdminGroup/themes/adminlte" // 必须引入,不然报错
    _ "github.com/GoAdminGroup/go-admin/modules/db/drivers/mysql"
    "github.com/GoAdminGroup/go-admin/engine"
    "github.com/GoAdminGroup/go-admin/plugins/admin"
    "github.com/GoAdminGroup/go-admin/plugins"
    "github.com/GoAdminGroup/go-admin/modules/config"
    "github.com/GoAdminGroup/go-admin/examples/datamodel"
)

func main() {
    r := gin.Default()
    eng := engine.Default()
    cfg := config.Config{}

    adminPlugin := admin.NewAdmin(datamodel.Generators)

    // 从.so文件中加载插件
    examplePlugin := plugins.LoadFromPlugin("../datamodel/example.so")

    eng.AddConfig(cfg).
        AddPlugins(adminPlugin, examplePlugin).  // 加载插件
        Use(r)

    r.Run(":9033")
}

说明:example插件是演示例子。

参考文献:
[1]GoAdmin:https://github.com/GoAdminGroup/go-admin/tree/master/adapter
[2]GoAdmin插件的使用:https://book.go-admin.cn/zh/cha-jian/plugins

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章