demo
It is a tutorial project to let people understand the backend framework step-by-step.
From phase1 to phase11, you will see know a CRUD endpoint is refactored from 250 lines to only 80 lines. Also, you will learn modern coding skills, such as struct tag(~=Java annotation), reflection, middleware.
這是一個教學用專案,讓你一步一步理解backend framework的基本結構。 從第一版到第十一版,你將會看到一個250行的CRUD endpoint,被改寫成只有80行。 而且,你將會學習到現在編程技巧,例如:struct tag(大約等同Java annotation),reflection和middleware。
Contents
Branch | Description |
---|---|
phase1 | Demonstrate the codes written by novice developer. No attention to code reusability decreases maintainability and productivity. |
phase2 | Introduce of mux library. The routing information is centralized in main.go. |
phase3 | Remove hardcoded database username and password in source code. Introduces dependency injection of database object. |
phase4 | Introduce ORM. |
phase5 | Introduce input binder. Also, demonstrates the power(and pain) of reflection. |
phase6 | Introduce input validator. |
phase7 | Introduce middleware to handle http output. |
phase8 | Introduce database transaction manager. Remove of global database object in handler. |
phase9 | Add user model into the system. Introduce jwt authorization in middleware. |
phase10 | Wrap up repetitive code in CRUD handler |
phase11 | Introduce 3rd party middleware. Make the system become production-ready. |
分支 | 說明 |
---|---|
phase1 | 一個菜鳥所寫的程式碼。沒有著重程式重用性,讓程式變得難以維護,也降低人們的生產力。 |
phase2 | 引入mux程式庫。Routing資料現在全集中在main.go。 |
phase3 | 刪掉在程式碼中hardcoded的資料庫用戶名字和密碼。引入資料庫物件的dependency injection。 |
phase4 | 引入ORM. |
phase5 | 引入輸入處理器,也說明reflection的強大(和痛苦)。 |
phase6 | 引入輸入檢查器。 |
phase7 | 引入中間件來負責HTTP輸出。 |
phase8 | 引入資料庫交易管理者。刪掉在handler的全域資料庫物件。 |
phase9 | 加入用戶物件到系統。在中間件層加入jwt身份驗證 |
phase10 | 把重複性程式碼變成獨立程序 |
phase11 | 引入第三方套件,讓系統質素到達能上線級別。 |
Getting Started
Mercurial and Git
Some libraries require installation of Mercurial and Git. Please install these yourselves.
If you are using ubuntu, you may run the following command:
(Remarks: Never ask me anything about Windows environment)
sudo apt-get install mercurial git
部份程式庫需要使用Mercurial和Git,請自行安裝:
如果你使用ubuntu,你可以執行以下指令:
(請勿問我任何Windows上操作問題)
sudo apt-get install mercurial git
Go 1.5+
The Go packages in ubuntu is outdated. Thus I suggest you downloading the latest Go Compiler yourselves.
In following step, the Go compiler will be stored in your home directory.
- Extract the go1.X-linux-amd64.tar.gz file into ~/go-compiler:
mkdir ~/go-compiler && tar -xvf go1.6.linux-amd64.tar.gz -C ~/go-compiler --strip-components 1 - Create the a folder to store all go source code:
mkdir ~/go - Open ~/.bashrc add following lines in the bottom
export PATH=$PATH:[home_directory]/go-compiler/bin
export GOROOT=[home_directory]/go-compiler
export GOPATH=[home_directory]/go - restart your terminal
- Run: go version
If your compiler installed correctly, you will see something like "go version go1.6 linux/amd64" - cd ~/go/src && git clone https://github.com/TritonHo/demo.git
在ubuntu中的Go package是老舊的,我建議你下載最新Go 編譯器
- 把下載到的go1.X-linux-amd64.tar.gz解壓縮到 ~/go-compiler:
mkdir ~/go-compiler && tar -xvf go1.6.linux-amd64.tar.gz -C ~/go-compiler --strip-components 1 - 建立資料夾,用來存放所有的原始碼:
mkdir ~/go - 打開 ~/.bashrc,把以下內容加到最底:
export PATH=$PATH:[home_directory]/go-compiler/bin
export GOROOT=[home_directory]/go-compiler
export GOPATH=[home_directory]/go - 重開你的terminal
- 執行: go version
如果你的Go編譯器正確設定,你應該會看到"go version go1.6 linux/amd64" - cd ~/go/src && git clone https://github.com/TritonHo/demo.git
PostgreSQL 9.3+
If you are using ubuntu, I would suggest you simply run: "sudo apt-get install postgresql"
The default installation is enough for development purpose.
The ~/go/src/demo/schema folder contains readme.txt. It will teach you how to create the objects in database.
如果你正在使用ubuntu,我建議你直接執行"sudo apt-get install postgresql"
標準安裝在開發環境下足夠使用了。
資料夾 ~/go/src/demo/schema內的readme.txt,會教你怎樣一步一步地建立資料庫內所需物件
Need Help?
If you have a question or feature request, ask me in facebook. GitHub will be used exclusively for bug reports and pull requests.
I also provide backend courses. Please contract me if you want to pay for more knowledge.
如果你有疑問或請求,從facebook找我. GitHub只用作錯誤回報和pull request.
我有提供後端教學課程。如果你願意付費來換取更多知識,歡迎找我。
##Donate
If you think this tutorial is really helpful, I suggest you donate your 2 hours salary to Free Software foundation or Open Culture foundation