Before we start writing the back-end functional modules, let’s first select the back-end presentation template and framework.

To prepare, select the background frame template

Because the background does not need to consider search engine inclusion and SEO function, we can choose the following schemes:

  • Traditional solutions. Such as dedecms background, WordPress background, the combination of the front and back display mode, that is, the background controller binding view interface like our blog foreground, with template rendering in the server side is not good, directly output to the user.
  • Classical front and rear end separation scheme. There are single-page and IFrame schemes in the classical back-end separation schemes. Both are available, and the popular ones are Layui’s open source Layui backend and their more business-friendly layuiAdmin backend. The backend uses jQuery ajax to interact with the backend, and the front end uses static HTML + JS template engine to render the page.
  • Leading-edge vUE and React dynamic binding solutions. Due to the use of more avant-garde technology, this kind of scheme can be very convenient to achieve the back-end operation of the front and back end separation. There are also a large number of excellent front and back end framework components, such as Ant-Design, Element and so on. However, since using such solutions requires additional learning of vUE, React and other knowledge, golang beginners will be significantly more burdened if they have not been exposed to or used in the past.

To speed up the learning process, we chose layuiAdmin to implement golang’s blog background. Because it uses jQuery, most users will have used it at some point or another, making it easier to use. Note: layuiAdmin is a commercial backend, which means it has to be paid. I have purchased the paid version and use the LPPL license. Therefore, it can be used in open source projects. If you are using them for commercial purposes, go to Layui.com and purchase their license.

Build the layuiAdmin skeleton

We chose layuiAdmin as our background. Therefore, before we start, we should first build a scaffold, that is, to realize the code supported at the bottom, so as to provide convenient operations for the subsequent module writing more conveniently.

Let’s define the background directory and create a Manage directory under the root directory to use as the background directory. The layuiAdmin core files are:

manage/index.html
manage/src/index.js
manage/src/config.js
manage/src/lib/admin.js
manage/src/lib/view.js
manage/src/controller/common.js
manage/src/controller/console.js
manage/src/style/admin.css
manage/src/views/index.html
manage/src/views/layout.html
manage/src/tips/404.html
manage/src/tips/error.html
Copy the code

This part of the code on GitHub, related to copyright, here do not post their source code, but the source code has followed the Goblog open source project attached to the project, if you want to check the source code, please check GitHub.

The skeleton above is arranged according to the implementation of layuiAdmin. Since layuiAdmin is not directly applicable to our blog project, the above file has made some changes on the basis of layuiAdmin, and the request method has also changed. The main changes are in manage/index.html, manage/ SRC /index.js, manage/ SRC /config.js, and manage/ SRC /lib/view.js, so if you buy layuiAdmin, please don’t replace it directly. Otherwise, the background cannot be opened properly.

Major changes to the layuiAdmin section

  • To modify theindex.htmlConfiguration, addedbaseApiParameters:
<! DOCTYPEhtml>
<html>
<head>
  <meta charset="utf-8">
  <title>Background management system</title>
  <meta name="renderer" content="webkit">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, viewport-fit=cover" />
  <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1"/>
  <meta name="applicable-device" content="mobile">
  <link id="layuicss-layui" rel="stylesheet" href="https://www.layuicdn.com/layui/css/layui.css" media="all">
</head>
<body>
  <div id="LAY_app"></div>
  <script src="https://www.layuicdn.com/layui/layui.js"></script>
  <script>
  layui.config({
    baseApi: location.pathname + '/'.base: location.pathname + '/src/'.version: new Date().getTime()
    / / version: '1.0.1'
  }).use('index');
  </script>
</body>
</html>
Copy the code
  • Modified manage/ SRC /index.js to remove the authorization part. Since we will be using sessions to manage authorization, there is no need to pass tokens in the URL.

  • Modified manage/ SRC /config.js to add baseApi calls.

  • Modify manage/ SRC /lib/view.js to set the baseApi parameter to global add, meaning that baseApi will be added automatically to all requests. Change the parameters submitted by POST to JSON. Since formData is used by default to submit forms, Golang will encounter various problems when receiving two-dimensional arrays and objects. In addition, golang needs to set each field of the form submitted by formData, otherwise it cannot be read into struct normally. Therefore, we should use JSON submission to prevent this kind of problem.

if(e.url.indexOf('http')! = =0) {
    if(e.url.indexOf('/') = = =0) {
        e.url = e.url.substring(1); } e.url = r.baseApi + e.url; }...if (e.method == 'post' || e.type == 'post') {
    e.contentType = 'application/json; charset=utf-8';
    e.data = JSON.stringify(e.data);
}
Copy the code

This is the first step in our implementation of the backend, the scaffolding is complete, and then we can write back-end code as we please.

Tutorial use case source code

The complete project sample code is hosted on GitHub at github.com/fesiong/gob… You can view the complete tutorial project source code, it is recommended to view the tutorial at the same time, carefully control the source code, can effectively improve the code speed and deepen the understanding of the blog project. It is recommended to fork a copy directly to make changes on it. Welcome to Star.