如何使用原生go开发一个web项目
循序渐进,掌握编程思维和思路
初步具有工程思维,能适应一般的开发工作
1. 搭建项目
package main
import (
"encoding/json"
"log"
"net/http"
)
type indexdata struct {
title string `json:"title"`
desc string `json:"desc"`
}
func index(w http.responsewriter,r *http.request) {
w.header().set("content-type","application/json")
var indexdata indexdata
indexdata.title = "码神之路go博客"
indexdata.desc = "现在是入门教程"
jsonstr,_ := json.marshal(indexdata)
w.write(jsonstr)
}
func main() {
//程序入口,一个项目 只能有一个入口
//web程序,http协议 ip port
server := http.server{
addr: "127.0.0.1:8080",
}
http.handlefunc("/",index)
if err := server.listenandserve();err != nil{
log.println(err)
}
}
2. 响应页面
func indexhtml(w http.responsewriter,r *http.request) {
t := template.new("index.html")
viewpath, _ := os.getwd()
t,_ = t.parsefiles(viewpath + "/template/index.html")
var indexdata indexdata
indexdata.title = "码神之路go博客"
indexdata.desc = "现在是入门教程"
err := t.execute(w,indexdata)
fmt.println(err)
}
func main() {
//程序入口,一个项目 只能有一个入口
//web程序,http协议 ip port
server := http.server{
addr: "127.0.0.1:8080",
}
http.handlefunc("/",index)
http.handlefunc("/index.html",indexhtml)
if err := server.listenandserve();err != nil{
log.println(err)
}
}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>title</title>
</head>
<body>
hello mszlu blog!!
{{.title}}
{{.desc}}
</body>
</html>
3. 首页
3.1 页面解析
func index(w http.responsewriter,r *http.request) {
var indexdata indexdata
indexdata.title = "码神之路go博客"
indexdata.desc = "现在是入门教程"
t := template.new("index.html")
//1. 拿到当前的路径
path,_ := os.getwd()
//访问博客首页模板的时候,因为有多个模板的嵌套,解析文件的时候,需要将其涉及到的所有模板都进行解析
home := path + "/template/home.html"
header := path + "/template/layout/header.html"
footer := path + "/template/layout/footer.html"
personal := path + "/template/layout/personal.html"
post := path + "/template/layout/post-list.html"
pagination := path + "/template/layout/pagination.html"
t,_ = t.parsefiles(path + "/template/index.html",home,header,footer,personal,post,pagination)
//页面上涉及到的所有的数据,必须有定义
t.execute(w,indexdata)
}
3.2 首页数据格式定义
config/config.go
package config
type viewer struct {
title string
description string
logo string
navigation []string
bilibili string
avatar string
username string
userdesc string
}
type systemconfig struct {
appname string
version float32
currentdir string
cdnurl string
qiniuaccesskey string
qiniusecretkey string
valine bool
valineappid string
valineappkey string
valineserverurl string
}
models/category.go
package models
type category struct {
cid int
name string
createat string
updateat string
}
models/post.go
package models
import (
"goblog/config"
"html/template"
"time"
)
type post struct {
pid int `json:"pid"` // 文章id
title string `json:"title"` // 文章id
slug string `json:"slug"` // 自定也页面 path
content string `json:"content"` // 文章的html
markdown string `json:"markdown"` // 文章的markdown
categoryid int `json:"categoryid"` //分类id
userid int `json:"userid"` //用户id
viewcount int `json:"viewcount"` //查看次数
type int `json:"type"` //文章类型 0 普通,1 自定义文章
createat time.time `json:"createat"` // 创建时间
updateat time.time `json:"updateat"` // 更新时间
}
type postmore struct {
pid int `json:"pid"` // 文章id
title string `json:"title"` // 文章id
slug string `json:"slug"` // 自定也页面 path
content template.html `json:"content"` // 文章的html
categoryid int `json:"categoryid"` // 文章的markdown
categoryname string `json:"categoryname"` // 分类名
userid int `json:"userid"` // 用户id
username string `json:"username"` // 用户名
viewcount int `json:"viewcount"` // 查看次数
type int `json:"type"` // 文章类型 0 普通,1 自定义文章
createat string `json:"createat"`
updateat string `json:"updateat"`
}
type postreq struct {
pid int `json:"pid"`
title string `json:"title"`
slug string `json:"slug"`
content string `json:"content"`
markdown string `json:"markdown"`
categoryid int `json:"categoryid"`
userid int `json:"userid"`
type int `json:"type"`
}
type searchresp struct {
pid int `orm:"pid" json:"pid"` // 文章id
title string `orm:"title" json:"title"`
}
type postres struct {
config.viewer
config.systemconfig
article postmore
}
models/home.go
package models
type homedata struct {
config.viewer
categorys []category
posts []postmore
total int
page int
pages []int
pageend bool
}
4. 配置文件读取
config.toml:
[viewer]
title = "码神之路go语言博客"
description = "码神之路go语言博客"
logo = "/resource/images/logo.png"
navigation = ["首页","/", "go语言","/golang", "归档","/pigeonhole", "关于","/about"]
bilibili = "https://space.bilibili.com/473844125"
zhihu = "https://www.zhihu.com/people/ma-shen-zhi-lu"
avatar = "https://gimg2.baidu.com/image_search/src=http%3a%2f%2finews.gtimg.com%2fnewsapp_bt%2f0%2f13147603927%2f1000.jpg&refer=http%3a%2f%2finews.gtimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1647242040&t=c6108010ed46b4acebe18955acdd2d24"
username = "码神之路"
userdesc = "长得非常帅的程序员"
[system]
cdnurl = "https://static.mszlu.com/goblog/es6/md-assets"
qiniuaccesskey = "替换自己的"
qiniusecretkey = "替换自己的"
valine = true
valineappid = "替换自己的"
valineappkey = "替换自己的"
valineserverurl = "替换自己的"
package config
import (
"github.com/burntsushi/toml"
"os"
)
type tomlconfig struct {
viewer viewer
system systemconfig
}
type viewer struct {
title string
description string
logo string
navigation []string
bilibili string
avatar string
username string
userdesc string
}
type systemconfig struct {
appname string
version float32
currentdir string
cdnurl string
qiniuaccesskey string
qiniusecretkey string
valine bool
valineappid string
valineappkey string
valineserverurl string
}
var cfg *tomlconfig
func init() {
cfg = new(tomlconfig)
var err error
cfg.system.currentdir, err = os.getwd()
if err != nil {
panic(err)
}
cfg.system.appname = "mszlu-go-blog"
cfg.system.version = 1.0
_,err = toml.decodefile("config/config.toml",&cfg)
if err != nil {
panic(err)
}
}
5. 假数据-显示首页内容
package main
import (
"html/template"
"log"
"ms-go-blog/config"
"ms-go-blog/models"
"net/http"
"time"
)
type indexdata struct {
title string `json:"title"`
desc string `json:"desc"`
}
func isodd(num int) bool {
return num%2 == 0
}
func getnextname(strs []string,index int) string{
return strs[index+1]
}
func date(layout string) string{
return time.now().format(layout)
}
func index(w http.responsewriter,r *http.request) {
t := template.new("index.html")
//1. 拿到当前的路径
path := config.cfg.system.currentdir
//访问博客首页模板的时候,因为有多个模板的嵌套,解析文件的时候,需要将其涉及到的所有模板都进行解析
home := path + "/template/home.html"
header := path + "/template/layout/header.html"
footer := path + "/template/layout/footer.html"
personal := path + "/template/layout/personal.html"
post := path + "/template/layout/post-list.html"
pagination := path + "/template/layout/pagination.html"
t.funcs(template.funcmap{"isodd":isodd,"getnextname":getnextname,"date":date})
t,err := t.parsefiles(path + "/template/index.html",home,header,footer,personal,post,pagination)
if err != nil {
log.println(err)
}
//页面上涉及到的所有的数据,必须有定义
var categorys = []models.category{
{
cid: 1,
name: "go",
},
}
var posts = []models.postmore{
{
pid: 1,
title: "go博客",
content: "内容",
username: "码神",
viewcount: 123,
createat: "2022-02-20",
categoryid:1,
categoryname: "go",
type:0,
},
}
var hr = &models.homeresponse{
config.cfg.viewer,
categorys,
posts,
1,
1,
[]int{1},
true,
}
t.execute(w,hr)
}
func main() {
//程序入口,一个项目 只能有一个入口
//web程序,http协议 ip port
server := http.server{
addr: "127.0.0.1:8080",
}
http.handlefunc("/",index)
http.handle("/resource/",http.stripprefix("/resource/",http.fileserver(http.dir("public/resource/"))))
if err := server.listenandserve();err != nil{
log.println(err)
}
}
后续内容在gitee上面: 传送门
到此这篇关于基于原生go语言开发一个博客系统的文章就介绍到这了,更多相关go博客系统内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论