Code Monkey home page Code Monkey logo

demotritonho's Introduction

demoTritonHo

練習建構一個基本的CRUD fork TritonHo - demo

啟動

 /bin/zsh demoTritonHo/dev_env.sh

基本http要素

  • 路由
  • 驗證
  • 商業邏輯
  • 資料庫

使用套件

  • mux
  • gorm
  • jwt-go
  • pgsql
  • uuid
  • xorm

資料庫差異

使用xorm 比較接近原生ORMsql語法 在使用gorm時查詢了許多orm的寫法 為了比較原作者(xorm)gorm使用上的差異花了滿多時間比較實作差異

某些場合使用原生語法比用ORM還方便許多

再新增使用者判斷是否存在 如果使用ORM 要先判斷是否select有值 再進行新增 若使用原生語法就能query一次結束

insert into users(id, email, password_digest, first_name, last_name)
select ?, ?, ?, ?, ?
where not exists (select 1 from users where email = ?)

maybe can use gorm.firstorCreate() function to check

中介層

原作者習慣laravel方便的middleware 寫go時非常不順手,一開始容易卡住為何要這麼做,以及在寫router 時都要呼叫 http.RequestWiter http.Request 等等指數的概念 後來多方嘗試後 才知道傳入 *http.Request 是為了讓middleware判斷完應有的行為後進行 return http.code or go ahead

驗證

還沒有看懂 待捕

mux

一種路由器 可以方便存取 http 參數 進行商業邏輯的判斷

router := mux.NewRouter() // 宣告使用mux router 
router.HandleFunc("/v1/users/{userId:"+uuidRegexp+"}", middleware.Wrap(handler.UserUpdate)).Methods("PUT") // 可以存取userId 的值 也可以在上面加上簡易的驗證條件做第一部篩選
mux.Vars(*http.Request) // 可以將http宣告的變數存取

jwt

使用電腦上的私鑰 並以RS512 的方式加密

觀念技巧

能將create 或是 update 這種對於資料庫的操作寫成function 可以減少 if err!=nil 的次數 在內部回傳 db create 所需要的參數即可

demotritonho's People

Contributors

hackmd-deploy avatar ken0911208818 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.