編寫GO的WEB開發框架(二): 我指望怎麼用框架來開發個人應用?

根椐常見的PHP web開發過程,如下是我所指望的使用框架進行開發的過程:

初始化項目目錄結構和配置文件 (可以使用腳本進行初始化)

編寫入口文件

package main
import "./controllers"
func main() {
	app := App.New("conf.ini") //引入配置文件
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		app.Dispatch() //須要傳遞一些參數
	})
}

編寫controller

package controllers
import "models"

type Control{
	*App //匿名組合框架對象,以便直接使用框架的方法
}

//實現做用於全部controller的前置攔置,好比進行權限檢查
func (this *Control) PreControl{
}

//具體的每個controller方法(對應到Path)
func (this *Control) Action1(){
	//調用Valid進行參數檢查
	
	//調用models進行業務邏輯處理
	User:= models.NewUser()
	//若是業務邏輯是對單表簡單的增刪改查,可不編寫service層,直接用框架提供的dao方法
	User:= this.NewMySQLDao("user")

	//可根椐須要進行輸出,好比輸出格式化的json
	this.RespJson(User.Lst())	
	//或使用模板輸出
	this.Render("a.tpl",User.Lst())
}

若是有須要,編寫models,實現service層

package models

type M struct{ //組合框架提供的dao對象方法
	 *MySQLDao
	 *MemcacheDao
}
func NewUser() *M{
	mysql:= NewMySQLDao()
	return &M{mysql}
}
//實現service方法
func (this *M)GetCache(){
	//從緩存讀,若有,直接返回
	//緩存沒有,從數據庫讀,並放回緩存
}

若有須要,編寫模板

相關文章
相關標籤/搜索