golang web實戰(beego,mvc)
想寫個小網站,聽說MVC過時了,流行MVVM,但是看了一下gin+vue+axios方式,發現還有一堆知識點要掌握,尤其是不喜歡nodejs和javascript方式的寫法。算了,還是用beego來寫吧。
1、首先參考https://beego.me
2、 獲取Beego 和 Bee 的開發工具
$ go get -u github.com/astaxie/beego $ go get -u github.com/beego/bee
我是用gopm get代替go get ,一開始有-u參數,但很長時間沒反應。去掉-u立即下載成功。
由於我的$gopath設置的是:E:\goapp\,所以將從github下載的bee包復制到這個目錄:E:\goapp\src\github.com\beego\bee
但這個目錄下沒有bee.exe工具。於是在這個目錄下執行 go install,提示以下錯誤:
E:\goapp\src\github.com\beego\bee>go install main.go:21:2: cannot find package "github.com/beego/bee/cmd" in any of: D:\Go\src\github.com\beego\bee\cmd (from $GOROOT) C:\Users\Administrator\go\src\github.com\beego\bee\cmd (from $GOPATH)
很明顯,搜索的路徑沒有包括我所設置的的$gopath,也就是E:\goapp\ 用go env 查看一下,gopath仍為:C:\Users\Administrator\go
在windows系統環境變量一看,原來是用戶的gopath環境變量%USERPROFILE%\go覆蓋了我設的系統的gopath環境變量E:\goapp\,於是將用戶環境變量設為E:\goapp\。 重新打開cmd,到bee目錄下執行go install 。然後可以在E:\goapp\bin下找到bee.exe了
3、開始寫網站,打開cmd ,轉到E:\goapp\src\ 目錄下,執行
bee new beego1
在E:\goapp\src\beego1下已生成了MVC網站,cmd中定位到E:\goapp\src\beego1目錄下,執行
bee run
然後可以用瀏覽器打開http://localhost:8080/ 進行訪問(若彈出防火墻提示,允許與否沒關系,因為本機localhost不通過防火墻?)
4、用liteide打開beego1目錄下的main.go,將所有內容用以下代碼替換
package main import ( _ "beego1/routers" "github.com/astaxie/beego" ) type MainController struct { beego.Controller } func (this *MainController) Get() { //this.TplName = "home/index.tpl" this.Ctx.WriteString("hello world") } func main() { beego.BConfig.WebConfig.StaticDir["/static"] = "static" beego.Router("/", &MainController{}) beego.Run() }
將routers.go的內容改為
package routers import ( "beego1/controllers" "github.com/astaxie/beego" ) func init() { //beego.Router("/", &controllers.MainController{}) beego.AutoRouter(&controllers.MainController{}) }
Ctrl-S保存,刷新http://localhost:8080/,頁面內容變成hello world ;而訪問http://localhost:8080/main/get時則顯示beego歡迎頁,AutoRouter起了作用。 (同時bee工具熱編譯,無需重新編譯執行。如需停止服務就用Ctrl-C),註意,在main包和controllers包各有一個MainController,互不影響。
5、上面的main包添加了靜態文件支持。即,在/main.go文件中beego.Run()之前加入了 : beego.BConfig.WebConfig.StaticDir["/static"] = "static" 則beego1\static目錄下的文檔可以直接通過瀏覽器訪問。
6、不喜歡8080端口,直接打開E:\goapp\src\beego1\conf\app.conf修改即可;也可以用beego.BConfig.Listen.HTTPPort = 8081(更多設置可參考:https://blog.csdn.net/qq_33610643/article/details/53511058)
7、顯示url參數。參數保存在 this.Ctx.Input.Params
當中,如:
/object/blog/2013/09/12 調用 ObjectController 中的 Blog 方法,參數如下:map[0:2013 1:09 2:12]
首先修改controllers目錄下的default.go如下
package controllers import ( "encoding/json" "github.com/astaxie/beego" ) type MainController struct { beego.Controller } func (c *MainController) Get() { c.Data["Website"] = "beego.me" c.Data["Email"] = "[email protected]" c.TplName = "index.tpl" } func (c *MainController) Params() { j, _ := json.Marshal(c.Ctx.Input.Params()) v := string(j) c.Ctx.WriteString(v) }
訪問http://localhost:8080/main/params/a/b/c
顯示:{"0":"a","1":"b","2":"c",":splat":"a/b/c"}
8、上傳文件。
首先在beego1文件夾下新建upload文件夾
在controllers目錄下的default.go中增加以下代碼
func (c *MainController) UpFile() { v := `<form id="fform" method="POST" enctype="multipart/form-data" action="/main/upfile1"> <input id="myfile" name="myfile" type="file" /> <input type="submit" value="保存" /> </form>` c.Ctx.Output.Header("Content-Type", "text/html; charset=utf-8") c.Ctx.WriteString(v) } func (c *MainController) UpFile1() { _, h, _ := c.GetFile("myfile") //獲取上傳的文件 //defer f.Close() //關閉上傳的文件,不然的話會出現臨時文件不能清除的情況 if h == nil { c.Ctx.WriteString("未選擇文件~!!!!!!!") c.StopRun() } b := path.Base(h.Filename) err := c.SaveToFile("myfile", "upload/"+b) if err != nil { c.Ctx.WriteString(fmt.Sprintf("%v", err)) } c.Ctx.WriteString("上傳成功~!!!!!!!") }
相應import "fmt" 和 "path",然後訪問:http://localhost:8080/main/upfile 上傳文件
9、數據庫支持。
參考:https://www.cnblogs.com/shanyou/p/3256906.html,決定使用postgresql數據庫
從官網https://www.postgresql.org下載,這裏有個可參考的安裝教程https://www.cnblogs.com/LLLONG/p/3164508.html 以及這裏https://blog.csdn.net/hanyoud/article/details/83294612
我在內網有個服務器是2008 sp2,在上面試試最新的11.1版本(由於官網很慢,改用迅雷大法下載),按向導安裝,先是自動安裝了VC++2013 VC++2017可再發行包,選擇安裝到E:盤,設置用戶postgres的密碼為1234 ,端口為默認5432,其他的一路next.
=============下面走了彎路,可以跳過===========
安裝很順利。但多出了個類似全家桶的stack builder,試了試安裝不上,可能還是天朝網絡影響,算了。
安裝完不會用,在目錄中找到pgadmin也用不了,度娘出來這個https://www.jb51.net/article/43061.htm,先看一下任務管理器中有了pg_ctl.exe,然後發現和別人的不一樣,於是手工到E:\Program Files\PostgreSQL\11\data目錄下找到了pg_hba.conf,參考:https://www.jb51.net/article/137062.htm,在pg_hba.conf配置文件最後加上以下內容(不太明白24或32的意思)
host all all 127.0.0.1/32 trust host all all 192.168.3.0/24 md5
然後看postgresql.conf中,默認就是listen_addresses = ‘*‘,不是listen_addresses=
‘localhost‘,就不改了。
到控制面板-服務中重啟一下postgresql-x64-11,再找到E:\Program Files\PostgreSQL\11\bin\psql.exe執行後提示Administrtor密碼,試了1234不行。參照https://blog.csdn.net/vivasoft/article/details/8248715 執行
initdb -D "E:\Program Files\PostgreSQL\11\data" --locale=C --encodeing=WIN1252 --username=postgres --pwprompt=1234 pg_ctl -D "E:\Program Files\PostgreSQL\11\data" start
,包括按提示修改一些錯誤,仍不行。運行psql,甚至提示Administrator不存在。還不如剛才了。
=============上面走了彎路,可以跳過===========
卸掉重新安裝一次,安裝時只選擇server和command line tool,locale選擇C,安裝完成。
發現E:\Program Files\PostgreSQL\11\scripts有個runsql.bat,運行之,輸入前面設置密碼1234,終於有個像樣的命令行入口了。
參考:https://blog.csdn.net/linuxchyu/article/details/16984517 裝完刪除掉data文件夾,命令行重新生成正宗的中文數據庫:
initdb -D data --locale=chinese-simplified_china.936 -E UTF-8
參考https://blog.csdn.net/little_rabbit_baby/article/details/54928940,輸入\l 命令查看所有數據庫,輸入\conninfo顯示連接信息,\q退出。因為需要在局域網內我的電腦(我的電腦ip是192.168.3.3,而安裝了pg的2008 sp2的ip是192.168.0.238)上訪問,參照前面的內容的配置server上的pg_hba.conf,加上
host all all 192.168.3.0/24 md5
再重啟服務:
pg_ctl -D "E:\Program Files\PostgreSQL\11\data" restart
根據runpsql.bat內容,知遠程連接命令為(-d表示數據庫名,-U不寫的話默認為Administraotr):
psql -h 192.168.0.238 -p 5432 -d postgres -U postgres
除了locale ,還要註意server_encoding和 client_encoding參考:https://www.cnblogs.com/winkey4986/p/6279243.html 雖然PG支持客戶端和服務器端的編碼自動轉換,但是還需要遵從一個原則:本地環境的編碼和客戶端編碼需一致。
在dos環境中輸入:chcp 或在psql中輸入:\! chcp
顯示:活動代碼頁: 936 ----936為簡體中文,GBK;
在psql中輸入: show client_encoding;(註意分號)我的電腦顯示GBK
輸入: show server_encoding;(註意分號)我的電腦顯示UTF8
可以用 set client_encoding to ‘utf8‘; 修改編碼
總結(轉來的):①直接在psql執行insert或者select的時候,設置client_encoding=gbk(默認),不亂碼;(上面例子證明了)
②使用“\i sql文件.sql”(sql文件是utf8編碼)命令的時候,如果sql文件中有中文,一定要先行執行set client_encoding=utf8;(設置此之後,按照上面說的,客戶端不轉換,直接把接收的字符作為utf8編碼傳給服務器端,而文件本身就是utf8,所以不亂碼;同理如果sql文件是ansi編碼即gbk編碼的話,確保client_encoding為gbk;總之,sql文件與client_encoding編碼一致),才不亂碼。
10、用beego連接pg,參考:https://www.cnblogs.com/hezhixiong/p/4617951.html
在$gopath\src下新建目錄postgresql ,其中main.go如下
package main import ( "fmt" "postgresql/models" "github.com/astaxie/beego" "github.com/astaxie/beego/orm" //_ "postgresql/routers" _ "github.com/lib/pq" ) func init() { // PostgreSQL 配置 //orm.RegisterDriver("postgres", orm.DR_Postgres) // 註冊驅動,// < 1.6 orm.RegisterDriver("postgres", orm.DRPostgres) //參考:https://beego.me/docs/mvc/model/orm.md#registerdriver orm.RegisterDataBase("default", "postgres", "user=postgres password=1234 dbname=postgres host=192.168.0.238 port=5432 sslmode=disable") /** * MySQL 配置 * 註冊驅動 * orm.RegisterDriver("mysql", orm.DR_MySQL) * mysql用戶:root ,root的秘密:tom , 數據庫名稱:test , 數據庫別名:default * orm.RegisterDataBase("default", "mysql", "root:tom@/test?charset=utf8") */ /** * Sqlite 配置 * 註冊驅動 * orm.RegisterDriver("sqlite", orm.DR_Sqlite) * 數據庫存放位置:./datas/test.db , 數據庫別名:default * orm.RegisterDataBase("default", "sqlite3", "./datas/test.db") */ // 自動建表 orm.RunSyncdb("default", false, true) } func main() { orm.Debug = true o := orm.NewOrm() o.Using("default") stu := new(models.Student) stu.Name = "tom" stu.Age = 25 fmt.Println(o.Insert(stu)) beego.Run() }
在postgresql目錄中建目錄models,其中,models.go如下:
package models import ( "github.com/astaxie/beego/orm" ) type Student struct { Id int64 Name string Age int } func init() { orm.RegisterModel(new(Student)) }
在liteide中Ctrl-B成功。
在psql終端中輸入 help 顯示幫助,其中sql 和psql有不同的幫助命令
輸入 \l 顯示有一個名為postgres 的database
輸入 \c 顯示You are now connected to database "postgres" as user "postgres".
輸入 \d 顯示 Did not find any relations.
什麽意思?沒有表? 繼續輸入以下命令:
create database testdb owner postgres; \c testdb create schema testdb \c create table t(id int,info text); \d
終於顯示有一個名為t的table了。
修改main.go,將dbname的值由postgres改為testdb,再Ctrl-B,這才想到忘記運行編譯出來的程序了,尷尬!運行後,在psql中 用 \d命令,看到生成student表了。
刪除表可用:drop table student;(註意分號;)
用 select * from student; 查詢student表中的數據。
終於OK了!
等等,還沒測試中文。刪除 student表,將main.go中的 tom 改為 湯姆。重新編譯運行,嗯,沒有亂碼。
golang web實戰(beego,mvc)