{"id":2366,"date":"2015-03-13T03:14:57","date_gmt":"2015-03-13T03:14:57","guid":{"rendered":"http:\/\/41j.com\/blog\/?p=2366"},"modified":"2015-03-14T04:18:10","modified_gmt":"2015-03-14T04:18:10","slug":"serving-server-generated-pngs-over-http-in-golang","status":"publish","type":"post","link":"https:\/\/41j.com\/blog\/2015\/03\/serving-server-generated-pngs-over-http-in-golang\/","title":{"rendered":"Serving server generated PNGs over HTTP in golang"},"content":{"rendered":"<p>Building on <a href=\"http:\/\/41j.com\/blog\/2015\/03\/creating-a-png-image-in-golang\/\">the PNG generation code<\/a>. We can easily expand this to serve the content over HTTP.<\/p>\n<p>The following example will serve content from the filesystem for everything but &#8220;\/images&#8221;. This path will generate and send a random 100&#215;100 PNG to the client.<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\npackage main\r\n\r\nimport (\r\n    &quot;github.com\/daaku\/go.httpgzip&quot;\r\n    &quot;net\/http&quot;\r\n    &quot;image&quot;\r\n    &quot;image\/png&quot;\r\n    &quot;image\/color&quot;\r\n    &quot;math\/rand&quot;\r\n)\r\n\r\nfunc handler(w http.ResponseWriter, r *http.Request) {\r\n  myimage := image.NewRGBA(image.Rectangle{image.Point{0,0},image.Point{100,100}})\r\n\r\n  \/\/ This loop just fills the image with random data\r\n  for x := 0; x &lt; 100; x++ {\r\n    for y := 0; y &lt; 100; y++ {\r\n      c := color.RGBA{uint8(rand.Intn(255)),uint8(rand.Intn(255)),uint8(rand.Intn(255)),255}\r\n      myimage.Set(x,y,c)\r\n    }\r\n  }\r\n\r\n  png.Encode(w, myimage)\r\n}\r\n\r\nfunc main() {\r\n  http.HandleFunc(&quot;\/images&quot;, handler)  \r\n  http.Handle(&quot;\/&quot;, httpgzip.NewHandler(http.FileServer(http.Dir(&quot;.&quot;))))\r\n\r\n  http.ListenAndServe(&quot;:8080&quot;, nil)\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Building on the PNG generation code. We can easily expand this to serve the content over HTTP. The following example will serve content from the filesystem for everything but &#8220;\/images&#8221;. This path will generate and send a random 100&#215;100 PNG to the client. package main import ( &quot;github.com\/daaku\/go.httpgzip&quot; &quot;net\/http&quot; &quot;image&quot; &quot;image\/png&quot; &quot;image\/color&quot; &quot;math\/rand&quot; ) func [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-2366","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1RRoU-Ca","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/2366","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/comments?post=2366"}],"version-history":[{"count":3,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/2366\/revisions"}],"predecessor-version":[{"id":2376,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/2366\/revisions\/2376"}],"wp:attachment":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/media?parent=2366"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/categories?post=2366"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/tags?post=2366"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}