{"id":2417,"date":"2018-09-05T23:44:36","date_gmt":"2018-09-05T14:44:36","guid":{"rendered":"http:\/\/okamurax.com\/?p=2417"},"modified":"2020-05-04T23:53:33","modified_gmt":"2020-05-04T14:53:33","slug":"nginx%e3%81%a8golang%e3%81%a7api%e3%82%b5%e3%83%bc%e3%83%90%e3%83%bc","status":"publish","type":"post","link":"https:\/\/appbay.org\/?p=2417","title":{"rendered":"Nginx\u3068golang\u3067API\u30b5\u30fc\u30d0\u30fc"},"content":{"rendered":"<p>golang\u3082Nginx\u3082\u59cb\u3081\u305f\u3070\u304b\u308a\u306a\u306e\u3067\u3001\u4ed6\u306e\u30d6\u30ed\u30b0\u306e\u8a18\u4e8b\u3092\u53c2\u8003\u306b\u3057\u3064\u3064\u8a66\u9a13\u4e2d<\/p>\n<p>Nginx\u306econf\u306blocation\u8ffd\u52a0<\/p>\n<pre class=\"lang:default decode:true \">location \/app1 {\r\n    fastcgi_pass 127.0.0.1:8000;\r\n    include fastcgi_params;\r\n}<\/pre>\n<p>golang<\/p>\n<pre class=\"lang:go decode:true \">package main\r\n\r\nimport(\r\n    \"net\"\r\n    \"net\/http\"\r\n    \"net\/http\/fcgi\"\r\n    \"text\/template\"\r\n)\r\n\r\ntype Html struct{\r\n    Title string\r\n    Get string\r\n}\r\n\r\nfunc handler(w http.ResponseWriter, r *http.Request){\r\n\r\n    tmp := r.FormValue(\"word\")\r\n\r\n    t, err := template.ParseFiles(\"base.html\")\r\n    if err != nil{\r\n        return\r\n    }\r\n    err = t.Execute(w, &amp;Html{\r\n        Title: \"boom\",\r\n        Get: tmp,\r\n    })\r\n\r\n}\r\n\r\nfunc main(){\r\n\r\n    l, err := net.Listen(\"tcp\", \"127.0.0.1:8000\")\r\n    if err != nil{\r\n        return\r\n    }\r\n\r\n    http.HandleFunc(\"\/\", handler)\r\n    fcgi.Serve(l, nil)\r\n\r\n}<\/pre>\n<p>\u3061\u3087\u3063\u3068\u9055\u3046\u66f8\u304d\u65b9<\/p>\n<pre class=\"lang:default decode:true \">upstream app2 {\r\n  server 127.0.0.1:8000;\r\n}\r\n\r\n...\r\n\r\nlocation \/app2 {\r\n  proxy_pass http:\/\/app2\/;\r\n}<\/pre>\n<pre class=\"lang:go decode:true\">package main\r\n\r\nimport(\r\n  \"net\/http\"\r\n)\r\n\r\nfunc main(){\r\n\r\n\/\/NewServeMux.Handle(\u30d1\u30bf\u30fc\u30f3, Handler)\u3068\u306a\u3063\u3066\u3044\u308b\r\n\/\/ServeMux\u3068\u306f\u30d1\u30b9\u3054\u3068\u306e\u51e6\u7406\u3092\u767b\u9332\u3059\u308b\u69cb\u9020\u4f53\r\n\/\/HandlerFunc(\u30d1\u30bf\u30fc\u30f3\u3001\u30cf\u30f3\u30c9\u30e9)\u304c\u7528\u610f\u3055\u308c\u3066\u3044\u308b\r\n\r\n  m := http.NewServeMux()\r\n  h := new(MyHandler)\r\n  m.Handle(\"\/\", h)\r\n\r\n  \/\/Server\u306fHTTP\u30b5\u30fc\u30d0\u30fc\u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u6307\u5b9a\u3067\u304d\u308b\r\n\r\n  s := http.Server{\r\n    Addr: \":8000\",\r\n    Handler: m,\r\n  }\r\n\r\n  s.ListenAndServe()\r\n\r\n}\r\n\r\n\/\/MyHandler\u306fServeHTTP\u3092\u5b9f\u88c5\u3057\u3066\u3044\u308b\r\n\/\/ServeHTTP\u306fHandler Interface\u306b\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u308b\r\n\r\ntype MyHandler struct{\r\n\r\n}\r\n\r\nfunc (h *MyHandler) ServeHTTP (w http.ResponseWriter, r *http.Request){\r\n  w.Write([]byte(\"app2!!\"))\r\n}<\/pre>\n<h2>\u30b5\u30fc\u30d0\u30fc\u306e\u958b\u59cb<\/h2>\n<p><span style=\"color: #0000ff;\">func http.ListenAndServe(addr string, handler Handler) err<\/span><br \/>\n<span style=\"color: #0000ff;\">http.ListenAndServe(&#8220;:81&#8221;, nil)<\/span><\/p>\n<p>\u7b2c1\u5f15\u6570\u306bTCP\u30a2\u30c9\u30ec\u30b9<br \/>\n\u7b2c2\u5f15\u6570\u306bHandler\u3092\u5f15\u6570\u306b\u53d7\u3051\u53d6\u308a\u30b5\u30fc\u30d0\u30fc\u3092\u8d77\u52d5\u3059\u308b\u3002<\/p>\n<p>Handler\u3068\u3044\u3046\u306e\u306f\u3001http.Handler\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u3092\u5b9f\u88c5\u3057\u3066\u3044\u308c\u3070\u3044\u3044\u3002<span style=\"color: #0000ff;\"><br \/>\n<\/span><\/p>\n<p>\u3061\u306a\u307f\u306b\u3001\u5185\u90e8\u3067http.server.ListenAndServe\u3092\u547c\u3093\u3067\u3044\u308b\u3002http.server.ListenAndServe\u306f\u5185\u90e8\u3067http.Server.Serve\u3092\u547c\u3093\u3067\u3044\u308b\u3002http.Server.Serve\u306ffor\u30eb\u30fc\u30d7\u3067\u30ea\u30af\u30a8\u30b9\u30c8\u3054\u3068\u306bgoroutine\u3092\u7acb\u3061\u4e0a\u3052\u308b\u3002<\/p>\n<h2>http.Handle(r)\u3068\u306f<\/h2>\n<p><span style=\"color: #0000ff;\"><span style=\"color: #333333;\">ServeHTTP\u30e1\u30bd\u30c3\u30c9\u3092\u6301\u3064\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u3002<\/span><br \/>\n<span style=\"color: #333333;\">ServeHTTP\u306f\u30ea\u30af\u30a8\u30b9\u30c8\u3068\u30ec\u30b9\u30dd\u30f3\u30b9\u304c\u8a18\u8ff0\u3055\u308c\u308b\u3002<\/span><br \/>\ntype Handler interface { ServeHTTP(ResponseWriter, *Request)\u00a0}<\/span><\/p>\n<h2><span style=\"color: #333333;\">http.Handle\u3068\u306f<\/span><\/h2>\n<p><span style=\"color: #0000ff;\">func Handle(pattern string, handler Handler)<\/span><br \/>\nURL\u3068http.Hander\u3092\u5b9f\u88c5\u3057\u305f\u69cb\u9020\u4f53\u3092\u53d7\u3051\u53d6\u308a\u3001\u30eb\u30fc\u30bf\u306b\u767b\u9332\u3002<\/p>\n<h2><span style=\"color: #333333;\">http.HandleFunc\u3068\u306f<\/span><\/h2>\n<p><span style=\"color: #0000ff;\">func HandleFunc(pattern string, handler func(ResponseWriter, *Request))<br \/>\n<\/span>URL\u3068http.Hander\u3092\u53d7\u3051\u53d6\u308a\u3001\u30eb\u30fc\u30bf\u306b\u767b\u9332\u3002<\/p>\n<p>Handle\u3060\u3068ServeHTTP\u3092\u5b9a\u7fa9\u3057Handler\u3092\u5b9f\u88c5\u3057\u305f\u69cb\u9020\u4f53\u3092\u6e21\u3059\u5fc5\u8981\u304c\u3042\u308b\u304c\u3001HandleFunc\u306e\u5834\u5408Func\u306e\u307e\u307e\u53d7\u3051\u53d6\u308c\u308b\u3002<\/p>\n<h2>http.Handle\u3068HandleFunc\u306b\u3064\u3044\u3066<\/h2>\n<p>http\u5185\u3067DefaultServeMux(ServeMux)\u304c\u5b9a\u7fa9\u3055\u308c\u3066\u304a\u308a\u3001DefaultServeMux.Handler\u3001DefaultServeMux.HandleFunc\u304c\u547c\u3070\u308c\u3066\u3044\u308b\u3002<\/p>\n<h2><span style=\"color: #333333;\">http.ServerMux\u3068\u306f<\/span><\/h2>\n<p><span style=\"color: #333333;\">ServerMux\u306fURL\u3068handler\u3092\u767b\u9332\u3057\u3066\u304a\u304f\u69cb\u9020\u4f53\u3002<\/span><br \/>\n<span style=\"color: #333333;\">ServeMux\u81ea\u4f53\u3001ServeHTTP\u30e1\u30bd\u30c3\u30c9\u3092\u5b9f\u88c5\u3057\u3066\u3044\u308b\u2192Handler<br \/>\n<\/span><span style=\"color: #0000ff;\">mux := http.NewServeMux()<\/span><br \/>\n<span style=\"color: #0000ff;\">mux.Handle(&#8220;\/echo&#8221;, echoHandler)<\/span><\/p>\n<h2><span style=\"color: #333333;\">http.Handle(r)Func\u3068\u306f<\/span><\/h2>\n<p><span style=\"color: #0000ff;\">type HandlerFunc func(ResponseWriter, *Request)<br \/>\n<\/span>func(ResponseWriter, *Request)\u306e\u5225\u540d\u3067ServerHTTP\u304c\u5b9a\u7fa9(\u5b9f\u88c5)\u3055\u308c\u3066\u3044\u308b\u3002<\/p>\n<pre class=\"lang:go decode:true\">func main() {\r\nm := http.NewServeMux()\r\nm.Handle(\"\/test\", http.HandlerFunc(f))\r\n}\r\n\r\nfunc f(w http.ResponseWriter, r *http.Request) {\r\nw.WriteHeader(http.StatusOK)\r\nfmt.Fprintf(w, \"Hello World\")\r\n}<\/pre>\n<h2>Handle(r)Func\u3068HandleFunc\u306b\u3064\u3044\u3066<\/h2>\n<p>\u305d\u3063\u304f\u308a\u3067\u6df7\u4e71\u3059\u308b\u304c\u3001HandleFunc\u306fURL\u3068Handler\u3092ServerMux\u306b\u767b\u9332\u3059\u308b\u3002Handle(r)Func\u306e\u65b9\u306f\u3001func(ResponseWriter, *Request)\u306e\u5225\u540d\u3067http.Handler\u3068\u3057\u3066\u4f7f\u3048\u308b\u3002<\/p>\n<h2><span style=\"color: #333333;\">http.ServeMux.HandleFunc\u3068\u306f<\/span><\/h2>\n<p>URL\u3068func(ResponseWriter, *Request)\u3092\u53d7\u3051\u53d6\u308a\u30eb\u30fc\u30bf\u306b\u767b\u9332\u3057\u3066\u3044\u308b<\/p>\n<p>\u5185\u90e8\u3067func(ResponseWriter, *Request)\u3092Handle(r)Func\u306b\u30ad\u30e3\u30b9\u30c8\u3057\u3066\u3044\u308bHandle\u306b\u767b\u9332\u3057\u3066\u3044\u308b\u3002<\/p>\n<pre class=\"lang:go decode:true\">m := http.NewServeMux()\r\nm.HandleFunc(\"\/test\", func(w http.ResponseWriter, r *http.Request) {\r\nw.WriteHeader(http.StatusOK)\r\nfmt.Fprintf(w, \"Hello World\")\r\n})<\/pre>\n<h2>http.ServerMux.Handle\u3068\u306f<\/h2>\n<p>ServeMux\u306bURL\u3068Handler\u3092\u767b\u9332\u3059\u308b\u3002<\/p>\n<pre class=\"lang:go decode:true\">func main() {\r\nm := http.NewServeMux()\r\nm.Handle(\"\/test\", handler)\r\n}\r\n\r\ntype handler struct{}\r\n\r\nfunc (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {\r\nw.WriteHeader(http.StatusOK)\r\nfmt.Fprintf(w, \"Hello World\")\r\n}<\/pre>\n<h2>\u51fa\u529b\u95a2\u4fc2<\/h2>\n<p><span style=\"color: #0000ff;\">Print<\/span><br \/>\n\u6a19\u6e96\u51fa\u529b\u3078<\/p>\n<p><span style=\"color: #0000ff;\">Fprint<\/span><br \/>\n\u51fa\u529b\u5148\u3092\u6307\u5b9a<\/p>\n<p><span style=\"color: #0000ff;\">Sprint<\/span><br \/>\n\u6587\u5b57\u5217\u3067\u8fd4\u3059\u3002<\/p>\n<p>\u63a5\u5c3e\u306ef\u306f\u30d5\u30a9\u30fc\u30de\u30c3\u30c8<\/p>\n","protected":false},"excerpt":{"rendered":"<p>golang\u3082Nginx\u3082\u59cb\u3081\u305f\u3070\u304b\u308a\u306a\u306e\u3067\u3001\u4ed6\u306e\u30d6\u30ed\u30b0\u306e\u8a18\u4e8b\u3092\u53c2\u8003\u306b\u3057\u3064\u3064\u8a66\u9a13\u4e2d Nginx\u306econf\u306blocation\u8ffd\u52a0 location \/app1 { fastcgi_pass 127.0.0.1:8000;  &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/appbay.org\/?p=2417\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;Nginx\u3068golang\u3067API\u30b5\u30fc\u30d0\u30fc&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[42],"class_list":["post-2417","post","type-post","status-publish","format-standard","hentry","category-1","tag-apache"],"_links":{"self":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/2417","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2417"}],"version-history":[{"count":9,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/2417\/revisions"}],"predecessor-version":[{"id":2508,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/2417\/revisions\/2508"}],"wp:attachment":[{"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2417"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2417"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2417"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}