Merging other languages

This commit is contained in:
James Miranda
2016-09-23 18:01:10 -03:00
parent 380a8ee74c
commit de3c5bdaa4
490 changed files with 24539 additions and 24588 deletions

View File

@@ -1,53 +1,62 @@
# 13.1 项目规划
做任何事情都需要做好规划,那么我们在开发博客系统之前,同样需要做好项目的规划,如何设置目录结构,如何理解整个项目的流程图,当我们理解了应用的执行过程,那么接下来的设计编码就会变得相对容易了
## gopath以及项目设置
假设指定gopath是文件系统的普通目录名当然我们可以随便设置一个目录名然后将其路径存入GOPATH。前面介绍过GOPATH可以是多个目录在window系统设置环境变量在linux/MacOS系统只要输入终端命令`export gopath=/home/astaxie/gopath`但是必须保证gopath这个代码目录下面有三个目录pkg、bin、src。新建项目的源码放在src目录下面现在暂定我们的博客目录叫做beeblog下面是在window下的环境变量和目录结构的截图
![](images/13.1.gopath.png?raw=true)
图13.1 环境变量GOPATH设置
![](images/13.1.gopath2.png?raw=true)
图13.2 工作目录在$gopath/src下
## 应用程序流程图
博客系统是基于模型-视图-控制器这一设计模式的。MVC是一种将应用程序的逻辑层和表现层进行分离的结构方式。在实践中由于表现层从Go中分离了出来所以它允许你的网页中只包含很少的脚本。
- 模型 (Model) 代表数据结构。通常来说,模型类将包含取出、插入、更新数据库资料等这些功能。
- 视图 (View) 是展示给用户的信息的结构及样式。一个视图通常是一个网页但是在Go中一个视图也可以是一个页面片段如页头、页尾。它还可以是一个 RSS 页面或其它类型的“页面”Go实现的template包已经很好的实现了View层中的部分功能。
- 控制器 (Controller) 是模型、视图以及其他任何处理HTTP请求所必须的资源之间的中介并生成网页。
下图显示了项目设计中框架的数据流是如何贯穿整个系统:
![](images/13.1.flow.png?raw=true)
图13.3 框架的数据流
1. main.go作为应用入口初始化一些运行博客所需要的基本资源配置信息监听端口。
2. 路由功能检查HTTP请求根据URL以及method来确定谁(控制层)来处理请求的转发资源。
3. 如果缓存文件存在,它将绕过通常的流程执行,被直接发送给浏览器。
4. 安全检测应用程序控制器调用之前HTTP请求和任一用户提交的数据将被过滤。
5. 控制器装载模型、核心库、辅助函数,以及任何处理特定请求所需的其它资源,控制器主要负责处理业务逻辑。
6. 输出视图层中渲染好的即将发送到Web浏览器中的内容。如果开启缓存视图首先被缓存将用于以后的常规请求。
## 目录结构
根据上面的应用程序流程设计,博客的目录结构设计如下:
|——main.go 入口文件
|——conf 配置文件和处理模块
|——controllers 控制器入口
|——models 数据库处理模块
|——utils 辅助函数库
|——static 静态文件目录
|——views 视图库
## 框架设计
为了实现博客的快速搭建打算基于上面的流程设计开发一个最小化的框架框架包括路由功能、支持REST的控制器、自动化的模板渲染日志系统、配置管理等。
## 总结
本小节介绍了博客系统从设置GOPATH到目录建立这样的基础信息也简单介绍了框架结构采用的MVC模式博客系统中数据流的执行流程最后通过这些流程设计了博客系统的目录结构至此我们基本完成一个框架的搭建接下来的几个小节我们将会逐个实现。
## links
* [目录](<preface.md>)
* 上一章: [构建博客系统](<13.0.md>)
* 下一节: [自定义路由器设计](<13.2.md>)
# 13.1 Project program
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
## GOPATH and project settings
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:
![](images/13.1.gopath.png?raw=true)
Figure 13.1 GOPATH setting environment variables
![](images/13.1.gopath2.png?raw=true)
Figure 13.2 working directory in $ gopath/src under
## 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:
![](images/13.1.flow.png?raw=true)
Figure 13.3 the frame data stream
1. Main.go as an application portal, running blog initialize some basic resources needed, configuration information, listen port.
2. Check the HTTP request routing function, based on the URL and method to determine who( control layer ) to process the request forwarding resources.
3. If the cache file exists, it will bypass the normal process execution, is sent directly to the browser.
4. Safety Testing: The application before the call controller, HTTP requests, and any user submitted data will be filtered.
5. 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.
6. 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](preface.md)
- Previous section: [Build a web framework](13.0.md)
- Next section: [Customized routers](13.2.md)