I launched a small blog some time ago. Today, I have a little time to summarize http://www.milu.blog . Don't miss it if you pass by. Golang
Learn while learning, this seemingly uncomplicated little thing has been working for more than 2 months. In the early stage of technology selection, as a rookie with a little understanding of the front-end, I made most of the discards in the front-end. Instead of choosing the popular technology stack, I chose the most primitive technology stack. The purpose of doing this is that on the one hand, I hope that I can focus my main energy on the development of Golang
, and try not to indulge in the details of the front page while completing the small goals of the stage. When there are many problems, it can be quickly repaired and updated quickly, so that users and small partners who feedback problems can respond in time. Even with such a goal, I finally tried three sets of different style templates to display at the front desk, and it took a lot of time. Interested friends can try it. Of course, these are all perfectly implemented with the cooperation of the template engine Pongo2
. It is not necessary to restart the background service to directly update a single page. If you switch templates, you need to restart the service.
The original intention of this project is to consolidate the mastery of the basic knowledge of Golang
through a basic code exercise, and at the same time, it can also connect the other knowledge learned, and can quickly develop and complete in the simplest development method. Can be published online. Since the focus is on Golang
, the front page is rendered in a simple server-side way, no npm, webpack, no installation dependencies, no compression, rough and simple. On the basis of Layuimini , the background management page adopts iframe+vue
mixed development method. Originally, the multi-label function and basic layout framework achieved through iframe
did not change, and it was directly introduced in the business page vue
, element
. The advantage is that it saves the steps of installing dependency packages and packaging before going online. The disadvantage is that it cannot support complex business. The optional technology stacks are:
- The front page can use
Nuxt3 + ElementPlus
,Nextjs + Antd
and so on. - Background management can use Vue3 + Arco , React + Antd and so on.
Warehouse Address:
1. Related introduction
1.1 Basic introduction
-
麋鹿博客
The name is for the convenience of search engine and Github search directly, and at the same time constitutes the entire animal series of open source projects.
1.2 Technology stack
1.2.1 Background
technology | illustrate | Official website |
---|---|---|
Golang-1.18 | Development language | https://go.dev/ |
Gin-1.8.1 | Gin Web Framework | https://gin-gonic.com/zh-cn/docs/ |
Mysql-5.7 | database | 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 | template engine | https://github.com/flosch/pongo2 |
Logrus | log | https://github.com/sirupsen/logrus |
Base64Captcha | verification code | https://github.com/mojocn/base64Captcha |
Crypto | password vault | https://golang.org/x/crypto |
Ini | ini file library | https://github.com/go-ini/ini |
Goment | time processing tool | https://github.com/nleeper/goment |
Air | Hot update tool | https://github.com/cosmtrek/air |
1.2.1 Front Desk
technology | illustrate | Official website |
---|---|---|
Vue-2.x | Progressive JavaScript Framework | https://cn.vuejs.org/v2/guide/ |
Axios | Promise-based HTTP library | https://github.com/axios/axios |
Element-UI | Front-end UI component library | https://element.eleme.io/ |
Tinymce | Visual HTML editor | https://www.tiny.cloud/ |
Fontawesome | Icon font library | http://www.fontawesome.com.cn/ |
1.3 Development tools
system | tool | Official website |
---|---|---|
Goland | development tools | https://www.jetbrains.com/zh-cn/go/ |
Navicat | Database management tools | https://www.navicat.com.cn/ |
Atom | Source code reading tool | https://atom.io/ |
Cmder | Cmd Alternative Tool [windows] | https://cmder.net/ |
Notepad2 | Temporary single file editing [windows] | http://www.flos-freeware.ch/notepad2.html |
Chrome | debugging tools | https://www.google.com/intl/en-US/chrome/ |
1.4 File Structure
The overall structure refers to Laravel , the most beautiful framework in the world.
├─app // 核心代码
│ ├─controller // 控制层
│ │ ├─admin
│ │ └─home
│ ├─database // 数据库链接
│ ├─model // 模型层
│ └─service // 操作数据层
├─config // 配置文件
├─pkg // 所有工具文件
│ ├─e // 报错
│ ├─hash // 验证码
│ ├─response // 返回封装
│ └─utils // 工具库
├─public // 所有静态资源
│ ├─admin
│ ├─common
│ ├─data
│ ├─green
│ ├─home
│ └─uploads
├─routers // 路由文件
└─views // 所有静态资源
├─admin
├─green // 绿色主题模板
├─default // 默认模板
└─home // 普通模板
Tips:
Air
Since go itself does not have hot loading technology, it also needs the support of a hot loading tool. Not much to choose from.1. Fresh
Fresh satisfies the basic application, and every time the file is saved, the web application is generated or restarted, but this tool has not been updated for many years, so it is deprecated.
2. Air
The advantages of Air are also more prominent: color log output, custom build or binary commands, support for ignoring subdirectories, support for monitoring new directories after startup, and more.
2.1
Air
There is a problemAir has a cache problem. Although Air is terminated in cmd, the refresh browser program is still running. At this time, you need to manually end the process and restart.
// 查找 PID,9888为端口号 netstat -ano | findstr 9888 // 杀死进程,14172 查到的pid taskkill /pid 14172 /f
2.2 Command not found error
If you enter
air
and report this error, you need to configure the project path in the system path. For example, if the project is inD:\go-project
, then there should be one in the path:D:\go-project\bin
- Pongo2
Because it is a development method that the front and back ends are not separated, the template engine plays an important role, and the initial selection has also been seen a lot. For example, goview , ejs-like quicktemplate , and ejs-like hero , etc. Finally, the reason for choosing Pongo2 is that it is powerful, easy to use, and can easily implement the needs of sub-templates to inherit different templates. His general ideas and syntax are imitated or basically the same as Jinja2 and Django templates. There are powerful functions such as Extends, Macro, Block, Include, etc. If there is a Django -like, Nunjucks template syntax is basically not difficult to get started, and the Pongo2 documentation is not so detailed. For further understanding, you need to look at the repository source code Template_tests .
The problem is that it conflicts with Vue's value boundary symbol.
Two solutions:
- Vue tags are replaced with v-html, for example:
<div v-html="user.nickname"></div>
- Modify the configuration of Vue's boundary modifier delimiters .
Goland does not provide special syntax highlighting support for Pongo2, which recommends another plugin, Twig , which requires 2 steps - Install the plugin
File -> Settings -> Plugins -> Twig
. - restart dev tools
2. How to run locally
The following is an example of windows
system, first download and install the latest version Go
development package 1.18.3
from the official website , and then configure it.
2.1 Configuration of environment variables
Add Go
development-related variables to the system variables, you need to add the following variables
variable name | value | illustrate |
---|---|---|
GOPATH | d:\go-project | Go language development directory |
GOROOT | c:\Go | installation Go installation directory |
PATH | c:\Go\bin;d:\go-project\bin | The terminal can directly run the Go command; run the self-compiled Go program and Air |
GO111MODULE | on | Enable Go.mod function, unified use go.mod manage development dependencies, this function is added in Go1.11 version |
GOPROXY | https://goproxy.cn | Go Package download proxy address |
2.2 Database
2.2.1 Mysql
Mysql
is the Phpstudy suite I used to play PHP
. It is easy to install, easy to start, and easy to manage. Of course, you can also choose a separate Mysql
to install . Since the final release of the Pagoda control panel is currently the default version 5.7
, in order to avoid unnecessary trouble, I currently install this version.
2.2.2 Data Import
Then import elk-blg/public/data/elk-blog.sql
file.
2.2.3 Running
Cmd
cd to the current directory, and then directly enter the command air
to start the project.
$ cd D:\go-project\src\elk-blog
$ air
2.3 Goland
2.3.1 Configuration under File -> Settings -> Go
variable name | value | illustrate |
---|---|---|
GOPATH | d:\go-project | Go language development directory |
GOROOT | C:\Go | By default, the highest version installed on the system will be selected. |
GOPATH - Global GOPATH | d:\go-project | Global Settings |
Go Modules | GOPROXY= https://goproxy.cn ,direct | First download from the configuration address, if it fails, switch to the download function from the original address, and use go.mod to manage the development dependency package. This function is added in the Go1.11 version |
GOPROXY | https://goproxy.cn | Go Package download proxy address |
2.3.2 Settings -> Project Structure
Exclude Exclued
.idea, bin, pkg
and other directories without indexing, which effectively saves memory resources.
2.3.3 Settings -> Appearance
Use One Dark
theme
2.4 Configuration of Air
3. How to publish online
Due to my special dishes, I haven't learned much about the classics linux
and docker
, so this time I chose the pagoda control panel to assist in the deployment. The following experience is also introduced based on the pagoda.
3.1 Install GO
Pagoda Linux Panel - Install Golang Environment
- The basic steps are to first download the
tar
package from the go official website, and then upload it to the specified directory on the server/usr/local
The purpose of this is to quickly complete and save time. Then decompress and add environment variables
tar -xzvf go1.18.2.linux-amd64.tar.gz
, here it needs to be explained that the terminal of the pagoda cannot modify the configuration file, that is, it does not exit the save mode, and needs ssh and modification directly in the text editing mode.
Environment variables I added:export GOROOT=/usr/local/go # 设置为go安装的路径 export GOPATH=/www/wwwroot/GO #项目路径 export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
- Enter
go version
in the pagoda terminal, and display:go version go1.18.2 linux/amd64
that is, it is confirmed that the installation is successful
3.2 Create a new website
In fact, it is to create a new nginx configuration file, such as a new website milu.blog
, and then configure the go service port entry in nginx. If the port of go is 4000
, add the following configuration:
server{
location / {
proxy_pass http://127.0.0.1:4000;
}
}
3.3 Prepare to package go
The go package command only packages files ending with *.go
, in other words, files other than *.go
need to be uploaded manually. If you have experienced npm run build
baptism, then this build of go has not encountered major problems at least so far, perhaps the reason why I wrote less code.
set CGO_ENABLED=0
set GOARCH=amd64
set GOOS=linux
go build main.go
3.4 Uploading files
The uploaded directory is the project path defined in the environment variable. For example, the directory of the milu project is
/www/wwwroot/GO/milu
Other projects are
/www/wwwroot/GO/other
In order to prevent other configuration files from not being found, all related files are in this directory. The file organization under the last published /www/wwwroot/GO/milu
is:
├─config // 配置文件
├─public // 静态资源
├─views // 模板文件
├─main // 打完包二进制文件
That is to say, there are so many files in the front, and it is enough to deploy these files.
3.5 Upload SQL
file
The only thing in this piece is that the pagoda only supports the Mysql5.7
version, and does not support the Mysql8.0
a609dd742b08ae3a213adc3fcf248261--- version. It is estimated that manual installation can also be done, and there is no need to be lazy. To prevent version incompatibility, when developing locally Mysql
is 5.7
. Then locally Navicat
export the structure and data, and import it in Phpmyadmin
.
3.6 Command line terminal debugging
In the pagoda terminal, cd to the project directory /www/wwwroot/GO/milu
, and then directly ./main
, so that you can easily view the log and see some detailed error messages. After running ./main
, you can refresh the domain name, and if each step is normal, the page will be displayed.
3.7 PM2 binding process
I just tried to use pm2 to run go, but I didn't expect it to work. In this way, no other services will be installed for the time being, and it will run together with the Node service.
4. Someone to thank
This project thanks the following people for their support and help
- Shanghai - Time Shilao give me more help in the field of
Mysql
- Nanjing-Wike
Wike
is a seniorGo
developer - Shanghai-Sam
Sam
Good atGin
,Vue
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。