Golang六款优秀Web框架对比
Golangæ¯ä¸é¨å¿«éå¢é¿çè¯è¨ï¼ä¸ä¸ºæ建ç®åãå¿«éä¸å¯é ç软件è设计ãå®æä¾çnet/httpåºå¯¹äºHTTPåè®®å®ç°é常好ï¼åºäºæ¤åæé æ¡æ¶ä¼æ´å®¹æï¼å æ¤çæä¸åºç°äºå¾å¤æ¡æ¶ãæ¬æå°ä»æµè¡åº¦ã社åºæ¯æåå建åè½çè§åº¦å¯¹å款ç¥åGoè¯è¨Webæ¡æ¶å对æ¯ã
å款Webæ¡æ¶
Beego é¢åGoç¼ç¨è¯è¨çå¼æºé«æ§è½webæ¡æ¶
- https://github.com/astaxie/beego
- https://beego.me
Buffalo 使ç¨Goè¯è¨å¿«éæ建Webåºç¨
- https://github.com/gobuffalo/buffalo
- https://gobuffalo.io
Echo é«æ§è½ãæç®Goè¯è¨Webæ¡æ¶
- https://github.com/labstack/echo
- https://echo.labstack.com
Gin Goè¯è¨ç¼åçHTTP Webæ¡æ¶ï¼å®ä»¥æ´å¥½çæ§è½å®ç°äºç±»ä¼¼MartiniçAPIï¼æ§è½æ´å¥½
- https://github.com/gin-gonic/gin
- https://gin-gonic.github.io/gin
Iris å¨å®å®æå¿«çGoè¯è¨Webæ¡æ¶ï¼å®å¤MVCæ¯æï¼æ¥æ±æªæ¥
- https://github.com/kataras/iris
- https://iris-go.com
Revel Goè¯è¨çé«æãå¨æ Webæ¡æ¶
- https://github.com/revel/revel
- https://revel.github.io
æµè¡åº¦
å¦ä¹ æ²çº¿
astaxie å kataras åå«ä¸º Beego å Iris æ´çäºè®¸å¤ä¾åä¾å¼åèåèå¦ä¹
æ ¸å¿åè½
å 个ç¥åçGoè¯è¨Webæ¡æ¶(EchoãGinåBuffalo)ç±äºæ²¡æå®å¤æ¯æææåè½ï¼å¹¶ä¸è½ç®æ¯çæ£æä¹ä¸çWebæ¡æ¶ï¼ä½å¤§é¨ågo社åºè®¤ä¸ºå®ä»¬æ¯çï¼å æ¤ï¼æå¿è¦å°è¿å 个æ¡æ¶ä¹åå¨è¡¨æ ¼ä¸å¯ä»¥åIrisãBeegoãRevelåæ¯è¾ã以ä¸è¿äºæ¡æ¶ï¼é¤äºBeegoåRevelä¹å¤ï¼é½å¯ä»¥ééä»»ænet/httpä¸é´ä»¶ï¼å¶ä¸ä¸é¨åæ¡æ¶å¯ä»¥è½»æ¾å°åééï¼å¦å¤ä¸äºå¯è½å°±éè¦é¢å¤çåªå
è¯æ±è§£æ
è·¯ç±: å½ååæ°åéé符 æ¯æ注åå¨æè·¯å¾
å½åè·¯å¾åæ°ç¤ºä¾
- // è·¯å¾åæ° username åå¼åå«æ¯ me,speedwheel
- "/user/{username}" å¹éå° "/user/me", "/user/speedwheel" çç
éé符示ä¾
- // è·¯å¾åæ° path 对åºçåå«æ¯ /user/some/path/here å /user/this/is/a/dynamic/multi/level/path
- "/user/{path *wildcard}" å¹éå°
- "/user/some/path/here",
- "/user/this/is/a/dynamic/multi/level/path" çç
Irisä¹æ¯æä¸ä¸ªå«microsçåè½ï¼å®å¯ä»¥è¢«è¡¨ç¤ºä¸º /user/{username: string} æ /user/{username: int min(1)}
è·¯ç±: æ£åè¡¨è¾¾å¼ æ¯æè¿æ»¤å¨æè·¯å¾
- "/user/{id ^[0-9]$}" å¹éå° "/user/42" ä½ä¸ä¼å¹é "/user/somestring"
è·¯ç±: åç» éè¿å±ç¨é»è¾æä¸é´ä»¶æ¥å¤çæå±ååç¼çè·¯å¾ç»
- myGroup := Group("/user", userAuthenticationMiddleware)
- myGroup.Handle("GET", "/", userHandler)
- myGroup.Handle("GET", "/profile", userProfileHandler)
- myGroup.Handle("GET", "/signup", getUserSignupForm)
- /user
- /user/profile
- /user/signup
å¯ä»¥ä»åç»ä¸åå建ååç»
- myGroup.Group("/messages", optionalUserMessagesMiddleware)
- myGroup.Handle("GET', "/{id}", getMessageByID)
- /user/messages/{id}
è·¯ç±: 以ä¸ææè§åç¸ç»åèä¸äº§çå²çª è¿æ¯ä¸ä¸ªé«çº§ä¸æç¨çåè½ï¼ç®åå¨Goè¯è¨æ¡æ¶æ¹é¢åªæIrisè½æ¯æè¿ä¸åè½ãè¿æå³çç±»ä¼¼å¦ /{path wildcard}, /user/{username}å/user/static以å/user/{path wildcard}çè·¯å¾é½å¯ä»¥å¨åä¸ä¸ªè·¯ç±ä¸éè¿éæè·¯å¾/user/staticæéé符/user/{path *wildcard}æ¥æ£ç¡®å¹é
è·¯ç±: èªå®ä¹HTTPå¼å¸¸ æå¯ä»¥èªè¡å¤ç请æ±é误çæåµãHTTPçé误ç¶æç >=400ï¼ä¾å¦ï¼è¯·æ±çèµæºä¸åå¨ NotFound 404
- OnErrorCode(404, myNotFoundHandler)
ä¸è¿°å¤§å¤æ°Webæ¡æ¶åªæ¯æ404,405å500é误ç¶æçå¤çï¼Irisè½å¤æ¯æä»»æé误ï¼BeegoåRevelä¹è½æ¯æå®å¤çHTTPé误ç¶æç
100%å¼å®¹net/httpå è¿æå³ç
- è¿äºæ¡æ¶è½å¤ç´æ¥è®¿é® *http.Request å http.ResponseWriter çä¸ä¸æä¿¡æ¯
- ä¸ç§å°net/http handler转æ¢ä¸ºç¹å®æ¡æ¶ç±»åhandlerçæ¹æ³
ä¸é´ä»¶çæç³»ç» æ¡æ¶ä¼ä¸ºä½ æä¾ä¸ä¸ªå®æ´çå¼ææ¥å®ä¹æµç¨ãå¨å±ãå个æä¸ç»è·¯ç±ï¼èä¸éè¦èªå·±ç¨ä¸åçä¸é´ä»¶æ¥å°è£æ¯ä¸é¨åçhandlers
ç±»Sinatraé£æ ¼API å¯ä»¥å¨è¿è¡æ¶ä¸æ³¨å¥ä»£ç æ¥å¤çç¹å®ç HTTP æ¹æ³(以åè·¯å¾åæ°)
- .Get or GET("/path", gethandler)
- .Post or POST("/path", postHandler)
- .Put or PUT("/path", putHandler) and etc.
æå¡å¨ç¨åº: èªå¨å¯ç¨HTTPS æ¡æ¶çæå¡å¨æ¯æ注ååèªå¨æ´æ°SSLè¯ä¹¦æ¥ç®¡çæ°ä¼ å¥çSSL/TLSè¿æ¥(https)
æå¡å¨ç¨åº: ä¼éå³é å½æä¸CTRL+Cå³éç»ç«¯åºç¨ç¨åºæ¶ï¼æå¡å¨å°çå¾(ç¹å®çè¶æ¶æ¶é´)å¶ä»çè¿æ¥å®æç¸å³ä»»å¡æ触åä¸ä¸ªèªå®ä¹äºä»¶æ¥åæ¸çå·¥ä½(æ¯å¦: å³éæ°æ®åº)ï¼æåä¼éçç»æ¢æå¡
æå¡å¨ç¨åº: å¤éçå¬ æ¡æ¶çæå¡å¨æ¯æèªå®ä¹çnet.Listeneræ使ç¨å¤ä¸ªhttpæå¡å¨åå°å为webåºç¨ç¨åºæä¾æå¡
å®å¨æ¯æHTTP/2 æ¡æ¶å¯ä»¥å¾å¥½çå¤çhttps请æ±çhttp/2åè®®ï¼å¹¶æ¯ææå¡å¨pushåè½ååå å¯ä»¥ç´æ¥å¨Webåºç¨ä¸æ³¨å¥åååçè·¯å¾
- è¾å©åè½ æå³çè¿ä¸ªåè½å¹¶ä¸è¢«è¿ä¸ªæ¡æ¶åçæ¯æï¼ä½æ¯ä½ ä»æ§å¯ä»¥éè¿å¯ç¨å¤ä¸ª http æå¡å¨æ¥å®ç°ã缺ç¹å¨äºï¼ä¸»ç¨åºååååç¨åºä¹é´å¹¶ä¸æ¯è¿éçï¼é»è®¤æåµä¸ï¼å®ä»¬ä¸è½å±äº«é»è¾
ä¼è¯(Sessions) æ¯æHTTP Sessionsï¼ä¸å¯ä»¥å¨èªå®ä¹çhandlersä¸ä½¿ç¨sessions
- ä¸äº Web æ¡æ¶æ¯æåå°æ°æ®åºæ¥å¨å sessionsï¼ä»¥ä¾¿å¨æå¡å¨éå¯ä¹åä»æ§è½è·å¾æä¹ç sessions
- Buffalo ä½¿ç¨ gorilla ç sessions åºï¼å®æ¯å¶ä»æ¡æ¶çå®ç°ç¥å¾®æ¢äºä¸ç¹
- func setValue(context http_context){
- s := Sessions.New(http_context)
- s.Set("key", "my value")
- }
- func getValue(context http_context){
- s := Sessions.New(http_context)
- myValue := s.Get("key")
- }
- func logoutHandler(context http_context){
- Sessions.Destroy(http_context)
- }
Websockets æ¯æwebsocketéä¿¡åè®®ï¼ä¸åæ¡æ¶æä¸åçå®ç°æ¹å¼ï¼å¶ä¸Iriså®ç°äºwebsocketæå¤åè½å¹¶æä¾äºç¸å¯¹æ´å®¹æ使ç¨çAPI
ç¨åºååµå¯¹è§å¾(模ç)çæ¯æ é常æåµä¸ï¼ä½ å¿é¡»æ ¹æ® Web åºç¨çå¯æ§è¡æ件ä¸ä¸å¯¹åºå°è½¬æ¢æ¨¡çæ件ãååµå°åºç¨ä¸æå³çè¿ä¸ªæ¡æ¶éæäº go-bindata ï¼å æ¤å¨æç»çå¯æ§è¡æ件ä¸å¯ä»¥ä»¥ []byte çå½¢å¼å°æ¨¡çåå«è¿æ¥
è§å¾å¼æ æ¡æ¶æ¯æ模çå è½½ãèªå®ä¹åå建模çåè½ï¼èçå¼åæ¶é´
è§å¾å¼æ: STD æ¡æ¶æ¯æéè¿æ åç html/template 解æå¨å 载模ç
è§å¾å¼æ: Pug æ¡æ¶æ¯æéè¿ Pug 解æå¨å 载模ç
è§å¾å¼æ: Django æ¡æ¶æ¯æéè¿ Django 解æå¨å 载模ç
è§å¾å¼æ: Handlebars æ¡æ¶æ¯æéè¿ Handlebars 解æå¨å 载模ç
è§å¾å¼æ: Amber æ¡æ¶æ¯æéè¿ Amber 解æå¨å 载模ç
渲æ: Markdown, JSON, JSONP, XML... æ¡æ¶æä¾ä¸ä¸ªç®åçæ¹æ³æ¥åéåèªå®ä¹åç§å容类åçååº
MVC Model-view-controller(MVC)模åæ¯ä¸ç§ç¨äºå¨è®¡ç®æºä¸å®ç°ç¨æ·çé¢ç软件æ¶æ模å¼ï¼å®å°ä¸ä¸ªåºç¨ç¨åºå为äºç¸å³èçä¸é¨åãè¿æ ·åçç®çæ¯ä¸ºäºï¼å°ä¿¡æ¯çåé¨å¤çé»è¾ãä¿¡æ¯åç°ç»ç¨æ·ä»¥åä»ç¨æ·è·åä¿¡æ¯ä¸èå离ãMVC 设计模å¼å°è¿ä¸ä¸ªç»ä»¶è§£è¦åï¼ä»èå®ç°é«æç代ç å¤ç¨å并è¡å¼å
- Iris æ¯æå®å¤ç MVC åè½, å¯ä»¥å¨è¿è¡æ¶ä¸æ³¨å¥
- Beego ä»æ¯ææ¹æ³åæ°æ®æ¨¡åçå¹éï¼å¯ä»¥å¨è¿è¡æ¶ä¸æ³¨å¥
- Revel æ¯ææ¹æ³ï¼è·¯å¾åæ°æ®æ¨¡åçå¹éï¼åªå¯ä»¥éè¿çæå¨æ³¨å¥(çæå¨æ¯å¦å¤ä¸ä¸ªä¸åç软件ç¨äºæå»ºä½ ç Web åºç¨)
ç¼å Web ç¼åæ¯ä¸ç§ç¨äºä¸´æ¶åå¨(ç¼å)ç½é¡µææ¡£ï¼å¦ HTML 页é¢åå¾åï¼æ¥åç¼æå¡å¨å»¶æ¶ãä¸ä¸ª Web ç¼åç³»ç»ç¼åç½é¡µææ¡£ï¼ä½¿å¾åç»ç请æ±å¦æ满足ç¹å®æ¡ä»¶å°±å¯ä»¥ç´æ¥å¾å°ç¼åçææ¡£ãWeb ç¼åç³»ç»æ¢å¯ä»¥æ设å¤ï¼ä¹å¯ä»¥æ软件ç¨åº
æ件æå¡å¨ å¯ä»¥æ³¨åä¸ä¸ª(ç©çç)ç®å½å°ä¸ä¸ªè·¯å¾ï¼ä½¿å¾è¿ä¸ªè·¯å¾ä¸çæ件å¯ä»¥èªå¨å°æä¾ç»å®¢æ·ç«¯
æ件æå¡å¨: ååµå¥åºç¨ é常æåµä¸ï¼å¿é¡»å°ææçéææ件(æ¯å¦assets: CSS, JavaScript æ件ç)ä¸åºç¨ç¨åºçå¯æ§è¡æ件ä¸èµ·ä¼ è¾ãæ¯ææ¤é¡¹åè½çæ¡æ¶ä¸ºä½ æä¾äºå¨åºç¨ä¸ï¼ä»¥ []byte çå½¢å¼ï¼ååµææè¿äºæ°æ®çæºä¼ãç±äºæå¡å¨å¯ä»¥ç´æ¥ä½¿ç¨è¿äºæ°æ®èæ éå¨ç©çä½ç½®æ¥æ¾æ件ï¼å®ä»¬çååºé度ä¹å°æ´å¿«
ååºå¨åéåå¯ä»¥å¨æ´ä¸ªçå½å¨æä¸ä¿®æ¹å¤æ¬¡ å½æ¡æ¶æ¯ææ¤åè½æ¶ï¼å¯ä»¥å¨è¿åç»å®¢æ·ç«¯ä¹åæ£ç´¢ãéç½®æä¿®æ¹ç¶æç ãbodyåheadersãé»è®¤æåµä¸ï¼å¨åºäº net/http ç Web æ¡æ¶ä¸è¿æ¯ä¸å¯è½çï¼å 为æ£æåç¶æç ä¸ç»åå®å°±ä¸è½è¢«æ£ç´¢æä¿®æ¹ãç®ååªæ Iris éè¿ http_context ä¸å建ççååºåå¥å¨(response writer)æ¯æè¿ä¸ªåè½
Gzip å¨ä¸ä¸ªè·¯ç±çhandlerä¸ï¼å¹¶ä¸å¯ä»¥æ¹åååºåå¥å¨(response writer)æ¥åéä¸ä¸ªç¨ gzip å缩çååºæ¶ï¼æ¡æ¶ä¼è´è´£ååºç头é¨ãå¦æåçä»»ä½é误ï¼æ¡æ¶åºè¯¥æååºé置为æ£å¸¸ï¼æ¡æ¶ä¹åºè¯¥è½å¤æ£æ¥å®¢æ·ç«¯æ¯å¦æ¯æ gzip å缩gzip æ¯ç¨äºå缩å解å缩çæä»¶æ ¼å¼å软件ç¨åº
æµè¯æ¡æ¶ å¯ä»¥ä½¿ç¨æ¡æ¶ç¹å®çåºï¼æ¥å¸®å©ä½ è½»æ¾å°ç¼åæ´å¥½çæµè¯ä»£ç æ¥æµè¯ä½ ç HTTPï¼å¦ä¸ç¤ºä¾(ç®ååªæIrisæ¯æ该åè½)
- func TestAPI(t *testing.T) {
- app := myIrisApp()
- tt := httptest.New(t, app)
- tt.GET("/admin").WithBasicAuth("name", "pass").Expect().
- Status(httptest.StatusOK).Body().Equal("welcome")
- }
myIrisAppè¿åèæçWebåºç¨ï¼å®æä¸ä¸ªé对/adminè·¯å¾çGETæ¹æ³ï¼ååºæ¬ç身份éªè¯é»è¾ä¿æ¤ãä¸é¢çç®åæµè¯ç¨ä¾ï¼ç¨nameåpasséè¿èº«ä»½éªè¯å¹¶è®¿é®GET /adminï¼æ£æ¥å®çååºç¶ææ¯å¦ä¸ºStatus OKåååºä½æ¯å¦ä¸ºwelcome
TypeScript转è¯å¨
å¨çº¿ç¼è¾å¨
æ¥å¿ç³»ç» èªå®ä¹æ¥å¿ç³»ç»éè¿æä¾æç¨çåè½ï¼å¦æ¥å¿è¾åºãæ ¼å¼åãæ¥å¿çº§å«å离åä¸åçæ¥å¿è®°å½å端çï¼æ¥æ©å±åçæ¥å¿å
ç»´æ¤åèªå¨æ´æ° 以éä¾µå¥çæ¹å¼éç¥æ¡æ¶çç¨æ·å³æ¶æ´æ°