After learning the basics of Go recently, I decided to give it a try. Because personal blogs are a common and simple example of connection, I decided to simplify the requirements further and create a simple personal blog to understand the basic use of the Go language on Web services.
Think about the functionality that needs to be implemented
A simplified version of personal blog, to meet basic needs.
- There is one that can be managed
md
File tool, the tool can be localmd
Files, that is, written blogs, can be uploaded to a server, or articles can be deleted based on their titles, etc. The tool imitateshexo
Blog system to do a command line tool, simple and direct. - Web front page: 1. There is a home page to display all articles, in this case in the form of a list. 2. For each item in the list, there is basic information about the article, such as title, upload time, word count, etc. 3. Article content page. 4. Support classification and label retrieval of corresponding articles.
- Web server: 1. Provide query of all articles according to classification
ID
Query for the article list, according to the tagID
Query for API such as article list. 2. Providemd
Files intoHTML
String function, the last interface form exposed. 3. Receive and analyze the filemd
File to store the corresponding information in the database.
Roughly the whole system consists of the above three parts.
The front and rear ends are separated
- The backend:
- There are many Web frameworks for the Go language, even with built-in ones
net/http
It is also easier to write Web services, so I chose a lightweight and simple Web frameworkgin
; - Because the business of the database is very small, there is no need to store a lot of relevant information, only need to store an article’s label, title, classification and time, so directly use lightweight
Sqlite
Database, directly todb
File storage; - The database
ORM
Tools, there are more popular domesticxorm
, directly used, the document is very comprehensive, it is not directly writtensql
That’s ok.
- There are many Web frameworks for the Go language, even with built-in ones
-
Front end:
- Nowadays the front end is more popular to build a single page application, I am also a front end programmer, take their familiar
React
Let’s build. UI
Frame, selectionGoogle
theMaterial-ui
Personally, I think it’s prettyUI
Library.
- Nowadays the front end is more popular to build a single page application, I am also a front end programmer, take their familiar
-
Command line tools:
- I did some googling and there is a library (CLI) that I can use.
Initialize the project
- First of all, we have
$GOPATH/src/github.com/[your_name]/
To create ablog
Folder, let’s just call it that. It doesn’t matter ~ -
Using the mode of separating the front and back ends, I create the following folders:
- CMD is used to develop command-line tools
- Controllers are the ones that hold the back end controllers, some operations, some functions. * Database Stores the configuration of the database and the operation of connecting to the database.
- Front_web is used for storage
React
The project. - Models store and use
xorm
Templates for database tables (some data structure structs) - Since posts is a relatively simple system, I directly put the articles uploaded by the client into this folder.
- Routers Back-end interfaces
- Static Web Static folder
- Main. go The main entrance to the entire program.
-
Create an Http service using gin:
-
Use the GET tool to install gin dependencies into local, gin documents
go get github.com/gin-gonic/gin
Copy the code -
Import gin in mian. Go, and then create the service.
package main
import (
"io"
"os"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.New()
// Set the log file
f, _ := os.Create("gin.log")
gin.DefaultWriter = io.MultiWriter(f, os.Stdout)
// Use logging middleware
r.Use(gin.Logger())
// Set the static folder
r.Static("/static", "./static")
r.Run(":8888")
}
Copy the code -
Run the main.go file to see the startup information, and then enter http://localhost:8888/ in your browser to access it, even though there is nothing there.
-
The last
The project sample has been written and posted on Github. This is just the first article to describe the functionality and how to initialize the project. Leave comments or go to my Github clone project and run through it. Continue to update the problems encountered and some solutions.