Webservices ingo

26
Web-Services In Go U1am Gandhi @U1amGandhi h1ps://in.linkedin.com/in/u1am-gandhi-0247aa21 www.synerzip.com

Transcript of Webservices ingo

Page 1: Webservices ingo

Web-ServicesInGo

U1amGandhi@U1amGandhih1ps://in.linkedin.com/in/u1am-gandhi-0247aa21www.synerzip.com

Page 2: Webservices ingo

Agenda•  REST•  GoAdvantage•  Webserviceexample•  TesMng•  SecuringusingJWT•  HosMng

Page 3: Webservices ingo

Web-service

•  TheW3CdefinesaWebservicegenerallyas:

aso%waresystemdesignedtosupportinteroperablemachine-to-machineinterac7on

overanetwork.

Page 4: Webservices ingo

RESTfulWebservice

Client

HTTPGET

/student/78/scores

{id:78,maths:89,physics:81}

Server

Page 5: Webservices ingo

REST•  IntroducedbyRoyFielding,2000•  REpresentaMonalStateTransfer– UseHTTPmethodsexplicitly.

•  POST,GET,PUT,DELETE– Bestateless.– Exposedirectorystructure-likeURIs.

•  h1p://www.myservice.org/discussion/topics/{topic}

– TransferXML,JSONorboth

Page 6: Webservices ingo

WhyREST

•  WhyREST– Minimizecouplingbetweenclientandserver– Clientbecomesresilienttoserverchanges

•  WhyNotREST– SecuredatashouldnotbesentasURI

Page 7: Webservices ingo

Statefuldesign

Client WebService

previousPage++nextPage=previousPage

returnnextPage

GET/book/next_page

JSONresponse

Page 8: Webservices ingo

Statelessdesign

Client WebService

getPage(2)

GET/book/page/2

JSONresponse

Page 9: Webservices ingo

Goadvantage

•  Deployment– Singlebinary– DependencyneedsstaMcMmemanaging

•  Performanceofgo– Built-inconcurrency

•  GarbagecollecMon

Page 10: Webservices ingo

BasicwebserviceusingFileIO

•  TheservicehandlesinventoryofIPAddresses•  Uses– net/h1pofforhandlingh1prequest

•  h1p.ListenAndServe()– Handlesconcurrentrequests– gorrilla/muxforparameterizedrouMng– osandbufioforfilehandling

•  h1ps://github.com/u1amgandhi24/ipalloc

Page 11: Webservices ingo

Let’sbuildthiswebservice….

Page 12: Webservices ingo

AddingHandlers

Page 13: Webservices ingo

HandlerforGetRequest

Page 14: Webservices ingo

HandlerforPostrequest

Page 15: Webservices ingo

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”}

Page 16: Webservices ingo

TesMng

•  testandh1ptest•  Writetestsin_test.gofiles•  funcTestMaintowritesetupandtear-downcode

•  TestcaseshouldstartwithTestprefix•  gotestrunsalltests•  gotest–coverrunsthetestandshowscoverage

Page 17: Webservices ingo

TestAnAPI

Page 18: Webservices ingo

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

Page 19: Webservices ingo

Client WebService

POST/authUser=..Password=…

HTTP200OK{JWTToken..}

GET/book/page/2Auth:JWTToken

HTTP200OK{Page2}

ValidateJWT

Page 20: Webservices ingo

GenerateJWTingo

Page 21: Webservices ingo

ParseJWTTokeningo

Page 22: Webservices ingo

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”}

Page 23: Webservices ingo

HosMng

•  Heroku– Signupandinstalltoolbelt– Creategowebapp– Godep(dependencymanager)– Pushtoheroku

•  h1ps://mmcgrana.github.io/2012/09/genng-started-with-go-on-heroku.html

Page 24: Webservices ingo

ThankYouQuesMons

Page 25: Webservices ingo

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

Page 26: Webservices ingo

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