当前位置:网站首页>How to write a blog with Golang - Milu.blog development summary
How to write a blog with Golang - Milu.blog development summary
2022-08-05 11:04:00 【51CTO】
I started a small blog some time ago,A little bit of time to sum up today http://www.milu.blog,Don't miss the friends who pass by.Golang
learn while rubbing,This seemingly uncomplicated little thing has been done before and after2个多月的时间.When the previous technology selection,As a rookie with a little understanding of the front end,Made most of the abandonment on the front end,Instead of choosing the popular technology stack, choose the most primitive technology stack.On the one hand, I hope that I can focus my energy onGolang
开发上,Try not to be obsessed with the details of the front page while completing the small goals of the stage,On the other hand, I hope to be able to quickly fix and update quickly when there are many initial problems.,To users and small partners who feedback problems can respond in a timely manner.Even if such a goal,In the end, I tried three different style templates at the front desk to display,也花费了不少时间,有兴趣的小伙伴可以试试.当然这些都是在Pongo2
Perfect realization with the cooperation of template engine,Update a single page directly without restarting the background service,If you switch templates, you need to restart the service.
The original intention of this project is to consolidate the understanding of the basic code through a basic code exercise.Golang
基础知识的掌握,At the same time, it can also be connected with other knowledge learned,And with the simplest development method, it can be developed quickly and can be released online.Since the focus is onGolang
,The front page is rendered in a simple server-side way,没有npm,webpack,没有安装依赖、压缩,rough and simple.The backend management page is in Layuimini 的基础上采用 iframe+vue
混合开发方式,原来通过 iframe
The function of realizing multi-label and the basic layout framework have not changed,Introduce directly on the business page vue
,element
.The advantage is that it eliminates the need to install dependency packages、Steps for packaging before going live,The disadvantage is that it cannot support complex business.The optional technology stacks are:
- Front page is available
Nuxt3 + ElementPlus
,Nextjs + Antd
等方式. - Background management can be used Vue3 + Arco, React + Antd 等等的方式.
仓库地址:
1. 相关介绍
1.1 基本介绍
Elk Blog
The name is for the convenience of search engines andGithub搜索直达,At the same time form the entire animal series of open source projects.
1.2 技术栈
1.2.1 后台
技术 | 说明 | 官网 |
---|---|---|
Golang - 1.18 | 开发语言 | https://go.dev/ |
Gin - 1.8.1 | Gin Web Framework | https://gin-gonic.com/zh-cn/docs/ |
Mysql - 5.7 | 数据库 | https://www.mysql.com/cn/ |
Gorm - 1.9.16 | Golang ORM | https://gorm.io/zh_CN/docs/index.html |
Jwt | Golang jwt | https://github.com/golang-jwt/jwt |
Pongo2 - 5 | 模板引擎 | https://github.com/flosch/pongo2 |
Logrus | 日志 | https://github.com/sirupsen/logrus |
Base64Captcha | 验证码 | https://github.com/mojocn/base64Captcha |
Crypto | 密码库 | https://golang.org/x/crypto |
Ini | ini文件库 | https://github.com/go-ini/ini |
Goment | 时间处理工具 | https://github.com/nleeper/goment |
Air | 热更新工具 | https://github.com/cosmtrek/air |
1.2.1 前台
技术 | 说明 | 官网 |
---|---|---|
Vue - 2.x | 渐进式JavaScript 框架 | https://cn.vuejs.org/v2/guide/ |
Axios | 基于promise 的HTTP 库 | https://github.com/axios/axios |
Element-UI | 前端UI组件库 | https://element.eleme.io/ |
Tinymce | 可视化HTML编辑器 | https://www.tiny.cloud/ |
Fontawesome | 图标字体库 | http://www.fontawesome.com.cn/ |
1.3 开发工具
系统 | 工具 | 官网 |
---|---|---|
Goland | 开发工具 | https://www.jetbrains.com/zh-cn/go/ |
Navicat | 数据库管理工具 | https://www.navicat.com.cn/ |
Atom | 源码阅读工具 | https://atom.io/ |
Cmder | Cmd替代工具[windows] | https://cmder.net/ |
Notepad2 | Temporary single file editing[windows] | http://www.flos-freeware.ch/notepad2.html |
Chrome | 调试工具 | https://www.google.com/intl/zh-CN/chrome/ |
1.4 文件结构
The overall structure refers to the most beautiful frame in the world Laravel.
Tips:
- Air
由于goNo hot loading technology itself,So it also needs the support of a hot loading tool.Not many options,
1. Fresh
FreshSatisfy basic applications,Generates or restarts every time the file is savedWeb应用程序,It's just that this tool has not been updated for many years, so it is deprecated.
2. Air
Air的优点也比较突出:彩色日志输出,自定义构建或二进制命令,支持忽略子目录,After startup, it supports monitoring new directories, etc..
2.1 Air
存在问题
Air存在缓存问题,虽然在cmdend insideAir,But refreshing the browser program is still running,In this case, you need to manually terminate the process and restart it.
2.2 Command not found 报错
如果输入 air
报这个错,That needs to be in the systempathConfigure the project path inside,比如项目在D:\go-project
,那么在pathThere should be one in there:
- Pongo2
Due to the development of the separation of front and back side is not,So template engine plays an important role,I also looked at a lot of the initial selection.比如 goview,仿ejs的 quicktemplate,还有类ejs的 hero等等的.最后选定Pongo2The reason is powerful,上手容易,It is easy to realize the needs of sub-templates to inherit different templates.His general ideas and grammar are imitationsJinja2和Djangotemplate or basically the same.有诸如Extends、Macro、Block、Include等强大功能,如果有类 Django, Nunjucks Template syntax is basically easy to get started with,Pongo2Documentation is not so detailed,For further understanding, you need to look at the source code of the warehouse Template_tests.
问题,与VueThe boundary value of symbols.
两种解决办法:
- Vue的标签用v-html来代替,比如:
<div v-html="user.nickname"></div>
- 修改VueThe boundary of the modifier delimiters配置方式.
Goland对Pongo2Does not provide dedicated syntax highlighting support,Here is another plugin recommended, Twig,需要2步 - 安装插件
File -> Settings -> Plugins -> Twig
. - 重启开发工具
2. 如何在本地运行
以下以windows
系统举例
先从 官网下载安装最新版Go
开发包 1.18.3
,然后进行相关的配置.
2.1 环境变量的配置
在系统变量中添加 Go
Development related variables,The following variables need to be added
变量名 | 值 | 说明 |
---|---|---|
GOPATH | d:\go-project | Go The development of the language directory |
GOROOT | c:\Go | 安装 Go 安装目录 |
PATH | c:\Go\bin;d:\go-project\bin | Terminal can be run directlyGo 命令; 运行自己编译的Go 程序和Air |
GO111MODULE | on | 开启 Go.mod 功能,统一用go.mod Manage development dependencies,此功能在Go1.11 版本中添加 |
GOPROXY | https://goproxy.cn | Go 包下载代理地址 |
2.2 数据库
2.2.1 Mysql
的安装
Mysql
I used to playPHP
的套件 Phpstudy,easy to install、启动便捷、Management and worry-free features,Of course, you can also choose a separate Mysql
来安装.Due to the final release 宝塔The current default version of the control panel is 5.7
,为了避免不必要的麻烦,I currently have this version installed.
2.2.2 数据导入
然后导入 elk-blg/public/data/elk-blog.sql
文件.
Default management entry:
初始用户名:admin
,密码:admin
2.2.3 运行
Cmd
中cd到当前目录,然后直接输入命令 air
后启动项目.
2.3 Goland
的配置
2.3.1 File -> Settings -> Go 下配置
变量名 | 值 | 说明 |
---|---|---|
GOPATH | d:\go-project | Go The development of the language directory |
GOROOT | C:\Go | By default, the highest version installed on the system will be selected. |
GOPATH-Global GOPATH | d:\go-project | 全局设置 |
Go Modules | GOPROXY=https://goproxy.cn,direct | First download from the configuration address,若失败,Download from the original address 功能,统一用go.mod Manage development dependencies,此功能在Go1.11 版本中添加 |
GOPROXY | https://goproxy.cn | Go 包下载代理地址 |
2.3.2 Settings -> Project Structure
排除Exclued
.idea, bin, pkg
等目录,不进行索引,Effectively save memory resources.
2.3.3 Settings -> Appearance
使用One Dark
主题
2.4 Air
的配置
3. How do you launch
Because of my special dishes,对经典的linux
及docker
never learned,So this time the choice is 宝塔Control panel to aid deployment,The following experience is also based on the pagoda to introduce.
3.1 安装GO
- Basic step is from the firstgo官网下载
tar
包,Then upload it to the specified directory on the server/usr/local
,这样做的目的是,Can be done quickly and saves time. - Then unzip and add environment variables
tar -xzvf go1.18.2.linux-amd64.tar.gz
,There need to be pagoda terminal not modify the configuration file,i.e. without exiting the saved mode,需要sshand modify directly in text edit mode.
environment variables I added:
- Pagoda terminal input
go version
,显示:go version go1.18.2 linux/amd64
That is, it is confirmed that the installation is successful
3.2 新建网站
其实就是新建nginx配置文件,such as creating a new website milu.blog
,然后在nginx中配置goservice port entry.假如go的端口为4000
,Then add the following configuration:
3.3 准备打包go
goThe pack command only packs*.go
结尾的文件,In other words not*.go
files need to be uploaded manually.如果经历过npm run build
baptized person,那go的这个buildAt least no major problems have been encountered so far,Maybe the reason why I write less code.
3.4 上传文件
The uploaded directory is the project path defined in the environment variable,比如miluThe project directory is
Other projects are
In order to prevent other configuration files from not being found,that all relevant files are in this directory.The last published/www/wwwroot/GO/milu
The files under are organized as:
That is to say, there are so many files before,If the deployment of these files will be enough.
3.5 上传SQL
文件
The only thing this piece says is that the pagoda only supportsMysql5.7
版本,暂不支持Mysql8.0
,Manual installation is also possible,Laziness and no toss.To prevent version incompatibility,在本地开发时Mysql
就为5.7
.然后本地Navicat
导出结构和数据,在宝塔的Phpmyadmin
里边导入.
3.6 Command line terminal debugging
in the pagoda terminalcd到项目目录/www/wwwroot/GO/milu
,然后直接 ./main
,This makes it easy to view logs,You can see some detailed error messages.运行 ./main
之后,Refresh the domain name,If each BuZhengChang page will display.
3.7 PM2绑定进程
I just try to state of mindpm2来运行一下go,I didn't expect it to work..This way no other services will be installed for the time being,和NodeService a run.
4. 感谢的人
Thanks to the following people for their support and help in this project
边栏推荐
猜你喜欢
反射修改jsessionid实现Session共享
Machine Learning - Ensemble Learning
苹果Meta都在冲的Pancake技术,中国VR团队YVR竟抢先交出产品答卷
智源社区AI周刊No.92:“计算复杂度”理论奠基人Juris Hartmanis逝世;美国AI学生九年涨2倍,大学教师短缺;2022智源大会观点报告发布[附下载]
CenOS MySQL入门及安装
结合“xPlus”探讨软件架构的创新与变革
机器学习——逻辑回归
脱光衣服待着就能减肥,当真有这好事?
解决2022Visual Studio中scanf返回值被忽略问题
TiDB 6.0 Placement Rules In SQL 使用实践
随机推荐
Opencv算术操作
hdu4545 Magic String
Linux: Remember to install MySQL8 on CentOS7 (blog collection)
使用Windbg过程中两个使用细节分享
HDD杭州站•ArkUI让开发更灵活
2022 Hangzhou Electric Power Multi-School Session 6 1008.Shinobu Loves Segment Tree Regular Questions
结合“xPlus”探讨软件架构的创新与变革
张朝阳对话俞敏洪:一边是手推物理公式,一边是古诗信手拈来
Integration testing of software testing
如何修改管理工具client_encoding
Custom filters and interceptors implement ThreadLocal thread closure
智能算力的枢纽如何构建?中国云都的淮海智算中心打了个样
Android development with Kotlin programming language - basic data types
时间格式2020-01-13T16:00:00.000Z中的T和Z分别表示什么,如何处理
Android 开发用 Kotlin 编程语言三 循环控制
365天挑战LeetCode1000题——Day 050 在二叉树中增加一行 二叉树
5G NR system messages
字节一面:TCP 和 UDP 可以使用同一个端口吗?
What do T and Z in the time format 2020-01-13T16:00:00.000Z represent and how to deal with them
Discover the joy of C language