webservices ingo
TRANSCRIPT
Web-ServicesInGo
U1amGandhi@U1amGandhih1ps://in.linkedin.com/in/u1am-gandhi-0247aa21www.synerzip.com
Agenda• REST• GoAdvantage• Webserviceexample• TesMng• SecuringusingJWT• HosMng
Web-service
• TheW3CdefinesaWebservicegenerallyas:
aso%waresystemdesignedtosupportinteroperablemachine-to-machineinterac7on
overanetwork.
RESTfulWebservice
Client
HTTPGET
/student/78/scores
{id:78,maths:89,physics:81}
Server
REST• IntroducedbyRoyFielding,2000• REpresentaMonalStateTransfer– UseHTTPmethodsexplicitly.
• POST,GET,PUT,DELETE– Bestateless.– Exposedirectorystructure-likeURIs.
• h1p://www.myservice.org/discussion/topics/{topic}
– TransferXML,JSONorboth
WhyREST
• WhyREST– Minimizecouplingbetweenclientandserver– Clientbecomesresilienttoserverchanges
• WhyNotREST– SecuredatashouldnotbesentasURI
Statefuldesign
Client WebService
previousPage++nextPage=previousPage
returnnextPage
GET/book/next_page
JSONresponse
Statelessdesign
Client WebService
getPage(2)
GET/book/page/2
JSONresponse
Goadvantage
• Deployment– Singlebinary– DependencyneedsstaMcMmemanaging
• Performanceofgo– Built-inconcurrency
• GarbagecollecMon
BasicwebserviceusingFileIO
• TheservicehandlesinventoryofIPAddresses• Uses– net/h1pofforhandlingh1prequest
• h1p.ListenAndServe()– Handlesconcurrentrequests– gorrilla/muxforparameterizedrouMng– osandbufioforfilehandling
• h1ps://github.com/u1amgandhi24/ipalloc
Let’sbuildthiswebservice….
AddingHandlers
HandlerforGetRequest
HandlerforPostrequest
IPAllocInAcMon
• ipalloc• ipallocservicelisteningon8123….• curl–XPOST–d’{“Name”:”dev8”,”IPAddress”:”1.2.3.12”}’h1p://localhost:8123/ipalloc
• curl–Gh1p://localhost:8123/ipalloc/1.2.3.12{“Name”:”device12”,”IPAddress”:”1.2.3.12”}
TesMng
• testandh1ptest• Writetestsin_test.gofiles• funcTestMaintowritesetupandtear-downcode
• TestcaseshouldstartwithTestprefix• gotestrunsalltests• gotest–coverrunsthetestandshowscoverage
TestAnAPI
SecuringwithJWT.io• Jsonwebtokens– JSONWebToken(JWT)isanopenstandard(RFC7519)thatdefinesacompactandself-containedwayforsecurelytransminnginformaMonbetweenparMesasaJSONobject.
• Librarysupport– C,Go,Swip,Scala,.NET,Java,Python,Node.js…...
• StructureofJWT– base64encodedheader– base64encodedpayload– signatureofthedotseparatedheaderandpayload.
• h1ps://github.com/u1amgandhi24/jwtdemo
Client WebService
POST/authUser=..Password=…
HTTP200OK{JWTToken..}
GET/book/page/2Auth:JWTToken
HTTP200OK{Page2}
ValidateJWT
GenerateJWTingo
ParseJWTTokeningo
JWTInAcMon• jwtdemo• jwtdemoservicelisteningon3080….• curl–XPOST–H“AuthenMcaMon:
U3luZXJ6aXAxOlBhc3N3b3JkMQ==”h1ps://localhost:3080/authenMcate
• "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0NTU2MTQzOTZ9.pzRgQSRHwSfUymQPsG-LVaH_2n10g3wxpJltYuiUco0"
• curl–H“AuthenMcaMon:eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0NTU2MTQzOTZ9.pzRgQSRHwSfUymQPsG-LVaH_2n10g3wxpJltYuiUco0”h1p://localhost:3080/book/page/2
{"PageNum":2,"content":"Thisasamplepage”}
HosMng
• Heroku– Signupandinstalltoolbelt– Creategowebapp– Godep(dependencymanager)– Pushtoheroku
• h1ps://mmcgrana.github.io/2012/09/genng-started-with-go-on-heroku.html
ThankYouQuesMons
References
• h1p://thenewstack.io/make-a-resxul-json-api-go/• h1p://www.ibm.com/developerworks/library/ws-resxul/
• h1ps://scotch.io/tutorials/the-ins-and-outs-of-token-based-authenMcaMon
• h1p://jwt.io/• h1p://dghubble.com/blog/posts/json-web-tokens-and-go/
• h1p://www.tutorialized.com/tutorial/RESTful-Web-services:-The-basics/40001
References
• h1ps://golang.org/pkg/encoding/base64/#example_Encoding_DecodeString
• h1p://stackoverflow.com/quesMons/1368014/why-do-we-need-resxul-web-services
• h1p://di-side.com/di-side/services/web-soluMons/rest-webservice-symfony/
• h1p://spf13.com/post/soap-vs-rest