关于golang:搞一个自娱自乐的博客四-友链

2次阅读

共计 3322 个字符,预计需要花费 9 分钟才能阅读完成。

性能形容

将首页革新为友链,性能蕴含链接题目和链接 因为后盾管理系统还没选好 临时不做增加性能 由数据库增加数据,页面做展现应用。

后盾代码

models

title 题目构造体

package models

type Title struct {
    Id   int    `form:"id"`
    Name string `form:"name"`
}

link 寄存具体链接

package models

type Link struct {
    Id    int    `form:"id"`
    Pid   int    `form:"pid"`
    Title string `form:"title"`
    Path  string `form:"path"`
}

repo

title_repo 链接数据库查问所有题目

package repo

import (
    "github.com/go-xorm/xorm"
    "log"
    "myCommunity/models"
)

type TitleRepo struct {engine *xorm.Engine}

func TitleDao(engine *xorm.Engine) *TitleRepo {
    return &TitleRepo{engine: engine,}
}

func (repo TitleRepo) GetAll() []models.Title {var datalist []models.Title
    err := repo.engine.Where("1=1").Find(&datalist)
    if err != nil {log.Println(err)
        return datalist
    } else {return datalist}
}

link_repo 链接数据库查问所有链接

package repo

import (
    "github.com/go-xorm/xorm"
    "log"
    "myCommunity/models"
)

type LinkRepo struct {engine *xorm.Engine}

func LinkDao(engine *xorm.Engine) *LinkRepo {
    return &LinkRepo{engine: engine,}
}

func (repo LinkRepo) GetAll() []models.Link {var datalist []models.Link
    err := repo.engine.Where("1=1").Find(&datalist)
    if err != nil {log.Println(err)
        return datalist
    } else {return datalist}
}

service

title_service 调用 repo 查问所有题目

package service

import (
    "myCommunity/datasource"
    "myCommunity/models"
    "myCommunity/repo"
)

type TitleService interface {GetAll() []models.Title}

func NewTitleService() *titleService {
    return &titleService{dao: repo.TitleDao(datasource.DbHelper()),
    }
}

type titleService struct {dao *repo.TitleRepo}

func (b titleService) GetAll() []models.Title {return b.dao.GetAll()
}

link_service 调用 repo 查问所有链接

package service

import (
    "myCommunity/datasource"
    "myCommunity/models"
    "myCommunity/repo"
)

type LinkService interface {GetAll() []models.Link}

func NewLinkService() *linkService {
    return &linkService{dao: repo.LinkDao(datasource.DbHelper()),
    }
}

type linkService struct {dao *repo.LinkRepo}

func (b linkService) GetAll() []models.Link {return b.dao.GetAll()
}

controller

link_controller 注册 linkService 和 titleSerivce 查问所有题目和链接返回 link.html

package controllers

import (
    "github.com/kataras/iris/v12"
    "github.com/kataras/iris/v12/mvc"
    "myCommunity/service"
)

type LinkController struct {
    LinkService  service.LinkService
    TitleService service.TitleService
}

// Get 返回 message
func (c *LinkController) Get() mvc.Result {
    // 获取所有留言
    linkList := c.LinkService.GetAll()
    titleList := c.TitleService.GetAll()
    return mvc.View{
        Name: "link.html",
        Data: iris.Map{
            "Title": titleList,
            "Link":  linkList,
        },
    }
}

路由

在 route 文件夹中减少 route.go

package route

import (
    "github.com/kataras/iris/v12"
    "github.com/kataras/iris/v12/mvc"
    "myCommunity/controllers"
    "myCommunity/service"
)

func Route(app *iris.Application) {
    // 注册服务
    mvc.Configure(app.Party("/link"), func(app *mvc.Application) {app.Register(service.NewLinkService())
        app.Register(service.NewTitleService())
        app.Handle(new(controllers.LinkController))
    })
}

main.go 中注册路由

    // 注册路由
    route.Route(app)

前端代码

前端只写次要代码 如下

   {{range $d := .Title}}
    <article>
        <section>
            <h1>{{$d.Name}}</h1>
            <p>
                {{range $l := $.Link}}
                {{if eq $l.Pid $d.Id}}
                <a  class="layui-btn layui-btn-primary layui-btn-xs" lay-event="show" href="{{unescaped $l.Path}}" target="_blank">
                    <i class="layui-icon layui-icon-list"></i>{{$l.Title}}
                </a>
                {{end}}
                {{end}}
            </p>
        </section>
    </article>
    {{end}}

其中用到了 range 遍历对象 以及 unescaped, unescaped 目标为不本义 html 代码。go template 自身没有这种语法,所以须要减少一个自定义标签。在 utils 里减少一个文件 htmlengine.go, 具体代码如下:

package utils

import (
    "github.com/kataras/iris/v12/view"
    "html/template"
)

func HtmlEngine(html *view.HTMLEngine) {
    // 自定义标签 不本义 HTML
    html.AddFunc("unescaped", func(str string) template.HTML {return template.HTML(str)
    })
}

这样在 html 里就能够间接应用了 应用办法为 {{unescaped $.item}}

结语

友链就是这样了 当前找个服务器部署下

正文完
 0