Need to do anything good plan, then we in the development blog system, also need to do the planning, how to set up the directory structure, how to understand the flow of the project, when we understand the execution of the application process, then the next the design becomes relatively easy to code
Assuming that the file system specified gopath ordinary directory name, of course, we can easily set up a directory name, then its path into GOPATH. GOPATH described earlier can be multiple directories: set the environment variable in the window system ; in linux/MacOS system as long as the input terminal command export gopath =/home/astaxie/gopath
, but must ensure that gopath the code below three directories pkg directory, bin, src. New Project source code in src directory, now tentatively our blog directory called beeblog, the following is in the window environment variables and directory structure screenshot:
Figure 13.1 GOPATH setting environment variables
Figure 13.2 working directory in $ gopath/src under
Blog system is based on the model - view - controller of this design pattern. MVC is a logic of the application layer and the presentation layer separation is structured. In practice, due to the presentation layer separate from the Go out, so it allows your page includes only a small script.
- Models(Model) represents the data structure. Generally speaking, the model class will contain remove, insert, update database information, etc. These functions.
- View(View) is displayed to the user's information structure and style. A view is usually a web page, but in Go, a view can also be a page fragment, such as page header, footer. It can also be an RSS page, or any other type of " page ", Go template package has been implemented to achieve a good part of the View layer of functionality.
- Controller(Controller) is a model, view, and anything else necessary for processing the HTTP request intermediary between resources and generate web pages.
The following figure shows the framework of the project design is how the data flow throughout the system:
Figure 13.3 the frame data stream
- Main.go as an application portal, running blog initialize some basic resources needed, configuration information, listen port.
- Check the HTTP request routing function, based on the URL and method to determine who( control layer ) to process the request forwarding resources.
- If the cache file exists, it will bypass the normal process execution, is sent directly to the browser.
- Safety Testing: The application before the call controller, HTTP requests, and any user submitted data will be filtered.
- controller loading model, core libraries, auxiliary functions, as well as any treatment other resources required for a particular request, the controller is primarily responsible for handling business logic.
- Output view layer rendering good to be sent to the Web browser content. If on the cache, the cache is first view, the routine for future requests.
According to the above application process design, blog design the directory structure is as follows:
|——main.go import documents
|——conf configuration files and processing module
|——controllers controller entry
|——models database processing module
|——utils useful function library
|——static static file directory
|——views view gallery
In order to achieve a quick blog to build, based on the above process design intends to develop a minimization framework, which includes routing capabilities, support for REST controllers, automated template rendering, log system, configuration management, and so on.
This section describes the blog system to the directory from the setup GOPATH establish such basic information, but also a brief introduction of the framework structure using the MVC pattern, blog system data flow execution flow, and finally through these processes designed blog system directory structure, thus we basically completed a framework to build the next few sections we will achieve individually.
- Directory
- Previous: build blogsystem
- Next: custom router design