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

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