4.6 KiB
13.1 Project planning
Anything you intend to do well must first be planned well. In our case, our intention is to develop a blogging system, so the first step we should take is to design the flow of the application in its entirety. When we have a clear understanding of the our application's process of execution, the subsequent design and coding steps become much easier.
GOPATH and project settings
Let's proceed by assuming that our GOPATH points to a folder with with an ordinary directory name (if not, we can easily set up a suitable directory and set its path as the GOPATH). As we've describe earlier, a GOPATH can contain more than one directory: in Windows, we can set this as an environment variable; in linux/OSX systems, GOPATH can be set using export, i.e: export gopath=/path/to/your/directory, as long as the directory which GOPATH points to contains the three sub-directories: pkg, bin and src. Below, we've placed the source code of our new project in the src directory with the tentative name beelog. Here are some screenshots of the Windows environment variables as well as of the directory structure.
Figure 13.1 Setting the GOPATH environment variable
Figure 13.2 The working directory under $gopath/src
Application flowchart
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.
Directory structure
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
Frame design
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.
Summary
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.
Links
- Directory
- Previous section: Build a web framework
- Next section: Customized routers


