温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

golang实现页面静态化操作的示例代码

发布时间:2020-09-12 05:46:33 来源:脚本之家 阅读:142 作者:lu569368 栏目:编程语言

什么是页面静态化:

简单的说,我们如果访问一个链接 ,服务器对应的模块会处理这个请求,转到对应的go方法,最后生成我们想要看到的数据。这其中的缺点是显而易见的:因为每次请求服务器都会进行处理,如 果有太多的高并发请求,那么就会加重应用服务器的压力,弄不好就把服务器 搞down 掉了。那么如何去避免呢?如果我们把请求后的结果保存成一个 html 文件,然后每次用户都去访问 ,这样应用服务器的压力不就减少了?

那么静态页面从哪里来呢?总不能让我们每个页面都手动处理吧?这里就牵涉到我们要讲解的内容了,静态页面生成方案… 我们需要的是自动的生成静态页面,当用户访问 ,会自动生成html文件 ,然后显示给用户。

为了路由方便我用的gin框架但是不管用在什么框架上面都是一样的

项目目录:

project

-tem

--index.html

-main.go

main.go文件代码:

package main

import (
  "fmt"
  "net/http"
  "os"
  "path/filepath"
  "text/template"

  "github.com/gin-gonic/gin"
)

type Product struct {
  Id  int64 `json:"id"` //字段一定要大写不然各种问题
  Name string `json:"name"`
}

//模拟从数据库查询过来的消息
var allproduct []*Product = []*Product{
  {1, "苹果手机"},
  {2, "苹果电脑"},
  {3, "苹果耳机"},
}
var (
  //生成的Html保存目录
  htmlOutPath = "./tem"
  //静态文件模版目录
  templatePath = "./tem"
)

func main() {
  r := gin.Default()
  r.LoadHTMLGlob("tem/*")
  r.GET("/index", func(c *gin.Context) {
    GetGenerateHtml()
    c.HTML(http.StatusOK, "index.html", gin.H{"allproduct": allproduct})
  })
  r.GET("/index2", func(c *gin.Context) {
    c.HTML(http.StatusOK, "htmlindex.html", gin.H{})
  })
  r.Run()
}

//生成静态文件的方法
func GetGenerateHtml() {
  //1.获取模版
  contenstTmp, err := template.ParseFiles(filepath.Join(templatePath, "index.html"))
  if err != nil {
    fmt.Println("获取模版文件失败")
  }
  //2.获取html生成路径
  fileName := filepath.Join(htmlOutPath, "htmlindex.html")
  //4.生成静态文件
  generateStaticHtml(contenstTmp, fileName, gin.H{"allproduct": allproduct})
}

//生成静态文件
func generateStaticHtml(template *template.Template, fileName string, product map[string]interface{}) {
  //1.判断静态文件是否存在
  if exist(fileName) {
    err := os.Remove(fileName)
    if err != nil {
      fmt.Println("移除文件失败")
    }
  }
  //2.生成静态文件
  file, err := os.OpenFile(fileName, os.O_CREATE|os.O_WRONLY, os.ModePerm)
  if err != nil {
    fmt.Println("打开文件失败")
  }
  defer file.Close()
  template.Execute(file, &product)
}

//判断文件是否存在
func exist(fileName string) bool {
  _, err := os.Stat(fileName)
  return err == nil || os.IsExist(err)
}

tem/index.html文件代码:

{{define "index.html"}}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>商品列表页</title>
</head>
<tbody>

<div><a href="#" rel="external nofollow" >商品列表页</a></div>
<table border="1">
  <thead>
  <tr>
    <th>ID</th>
    <th>商品名称</th>
  </tr>
  </thead>
  <tbody>
  {{range .allproduct}}
  <tr>
    <td>{{.Id}}</td>
    <td>{{.Name}}</td>
  </tr>
  {{end}}
  </tbody>
</table>
</html>
{{end}}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI