Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Web-Services	In	Go	
U1am	Gandhi	
@U1amGandhi	
h1ps://in.linkedin.com/in/u1am-gandhi-0247aa21	
www.synerzip.com
Agenda	
•  REST	
•  Go	Advantage	
•  Web	service	example	
•  TesMng	
•  Securing	using	JWT	
•  HosMng
Web-service	
•  The	W3C	defines	a	Web	service	generally	as:	
													a	so%ware	system	designed	to	support	
interoperab...
RESTful	Webservice	
Client	
		
HTTP	
GET	
/student/78/scores	
{	id:	78,	maths	:	89,	physics:81	}	
Server
REST	
•  Introduced	by	Roy	Fielding,	2000	
•  REpresentaMonal	State	Transfer	
– Use	HTTP	methods	explicitly.	
•  POST,	GET...
Why	REST	
•  Why	REST	
– Minimize	coupling	between	client	and	server	
– Client	becomes	resilient	to	server	changes	
•  Why...
Stateful	design	
Client	
Web	
Service	
previousPage++	
nextPage	=	previousPage	
return	nextPage	
	
GET	/book/next_page		
J...
Stateless	design	
Client	
Web	
Service	
getPage(2)	
	
GET	/book/page/2		
JSON	response
Go	advantage	
•  Deployment		
– Single	binary	
– Dependency	needs	staMc	Mme	managing	
•  Performance	of	go	
– Built-in	con...
Basic	web	service	using	FileIO	
•  The	service	handles	inventory	of	IPAddresses		
•  Uses	
– net/h1p	of	for	handling	h1p	r...
Let’s	build	this	webservice….
Adding	Handlers
Handler	for	Get	Request
Handler	for	Post	request
IPAlloc	In	AcMon	
•  ipalloc	
•  ipalloc	service	listening	on	8123	….	
•  curl	–XPOST	–d	
’{“Name”:”dev8”,”IPAddress”:”1.2...
TesMng	
•  test	and	h1ptest	
•  Write	tests	in	_test.go	files	
•  func	TestMain	to	write	setup	and	tear-down	
code	
•  Test...
Test	An	API
Securing	with	JWT.io	
•  Json	web	tokens	
–  JSON	Web	Token	(JWT)	is	an	open	standard	(RFC	7519)	
that	defines	a	compact	an...
Client	 Web	Service	
POST	/auth	
User=..		Password=…	
HTTP	200	OK	
{		JWT	Token..	}	
GET	/book/page/2	
Auth:	JWT	Token	
HT...
Generate	JWT	in	go
Parse	JWT	Token	in	go
JWT	In	AcMon	
•  jwtdemo	
•  jwtdemo	service	listening	on	3080	….	
•  curl	–XPOST	–H	“AuthenMcaMon	:	
U3luZXJ6aXAxOlBhc3N3...
HosMng	
•  Heroku	
– Signup	and	install	toolbelt	
– Create	go	webapp		
– Godep	(	dependency	manager	)	
– Push	to	heroku	
•...
Thank	You	
QuesMons
References	
•  h1p://thenewstack.io/make-a-resxul-json-api-go/	
•  h1p://www.ibm.com/developerworks/library/ws-
resxul/	
•...
References	
•  h1ps://golang.org/pkg/encoding/base64/
#example_Encoding_DecodeString	
•  h1p://stackoverflow.com/quesMons/
...
Upcoming SlideShare
Loading in …5
×

Webservices ingo

556 views

Published on

Restful Webservices in Go

Published in: Internet
  • Be the first to comment

  • Be the first to like this

Webservices ingo

  1. 1. Web-Services In Go U1am Gandhi @U1amGandhi h1ps://in.linkedin.com/in/u1am-gandhi-0247aa21 www.synerzip.com
  2. 2. Agenda •  REST •  Go Advantage •  Web service example •  TesMng •  Securing using JWT •  HosMng
  3. 3. Web-service •  The W3C defines a Web service generally as: a so%ware system designed to support interoperable machine-to-machine interac7on over a network.
  4. 4. RESTful Webservice Client HTTP GET /student/78/scores { id: 78, maths : 89, physics:81 } Server
  5. 5. REST •  Introduced by Roy Fielding, 2000 •  REpresentaMonal State Transfer – Use HTTP methods explicitly. •  POST, GET, PUT, DELETE – Be stateless. – Expose directory structure-like URIs. •  h1p://www.myservice.org/discussion/topics/{topic} – Transfer XML, JSON or both
  6. 6. Why REST •  Why REST – Minimize coupling between client and server – Client becomes resilient to server changes •  Why Not REST – Secure data should not be sent as URI
  7. 7. Stateful design Client Web Service previousPage++ nextPage = previousPage return nextPage GET /book/next_page JSON response
  8. 8. Stateless design Client Web Service getPage(2) GET /book/page/2 JSON response
  9. 9. Go advantage •  Deployment – Single binary – Dependency needs staMc Mme managing •  Performance of go – Built-in concurrency •  Garbage collecMon
  10. 10. Basic web service using FileIO •  The service handles inventory of IPAddresses •  Uses – net/h1p of for handling h1p request •  h1p.ListenAndServe() – Handles concurrent requests – gorrilla/mux for parameterized rouMng – os and bufio for file handling •  h1ps://github.com/u1amgandhi24/ipalloc
  11. 11. Let’s build this webservice….
  12. 12. Adding Handlers
  13. 13. Handler for Get Request
  14. 14. Handler for Post request
  15. 15. IPAlloc In AcMon •  ipalloc •  ipalloc service listening on 8123 …. •  curl –XPOST –d ’{“Name”:”dev8”,”IPAddress”:”1.2.3.12”}’ h1p://localhost:8123/ipalloc •  curl –G h1p://localhost:8123/ipalloc/1.2.3.12 {“Name”:”device12”,”IPAddress”:”1.2.3.12”}
  16. 16. TesMng •  test and h1ptest •  Write tests in _test.go files •  func TestMain to write setup and tear-down code •  Test case should start with Test prefix •  go test runs all tests •  go test –cover runs the test and shows coverage
  17. 17. Test An API
  18. 18. Securing with JWT.io •  Json web tokens –  JSON Web Token (JWT) is an open standard (RFC 7519) that defines a compact and self-contained way for securely transminng informaMon between parMes as a JSON object. •  Library support –  C, Go, Swip, Scala, .NET, Java, Python, Node.js …... •  Structure of JWT –  base64 encoded header –  base64 encoded payload –  signature of the dot separated header and payload. •  h1ps://github.com/u1amgandhi24/jwtdemo
  19. 19. Client Web Service POST /auth User=.. Password=… HTTP 200 OK { JWT Token.. } GET /book/page/2 Auth: JWT Token HTTP 200 OK { Page 2 } Validate JWT
  20. 20. Generate JWT in go
  21. 21. Parse JWT Token in go
  22. 22. JWT In AcMon •  jwtdemo •  jwtdemo service listening on 3080 …. •  curl –XPOST –H “AuthenMcaMon : U3luZXJ6aXAxOlBhc3N3b3JkMQ==” h1ps://localhost:3080/ authenMcate •  "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0NTU2MTQzOT Z9.pzRgQSRHwSfUymQPsG-LVaH_2n10g3wxpJltYuiUco0" •  curl –H “AuthenMcaMon : eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0NTU2MTQzOTZ 9.pzRgQSRHwSfUymQPsG-LVaH_2n10g3wxpJltYuiUco0” h1p:// localhost:3080/book/page/2 {"PageNum":2, "content":"This a sample page”}
  23. 23. HosMng •  Heroku – Signup and install toolbelt – Create go webapp – Godep ( dependency manager ) – Push to heroku •  h1ps://mmcgrana.github.io/2012/09/genng- started-with-go-on-heroku.html
  24. 24. Thank You QuesMons
  25. 25. 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
  26. 26. 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

×