{"id":2446,"date":"2018-09-27T12:19:32","date_gmt":"2018-09-27T03:19:32","guid":{"rendered":"http:\/\/okamurax.com\/?p=2446"},"modified":"2020-05-15T02:10:29","modified_gmt":"2020-05-14T17:10:29","slug":"golangmongodbvue-js","status":"publish","type":"post","link":"https:\/\/appbay.org\/?p=2446","title":{"rendered":"golang+MongoDB+Vue.js \u8272\u3005"},"content":{"rendered":"<p>\u30b5\u30fc\u30d0\u30fc\u3092golang+MongoDB\u3067\u30d5\u30ed\u30f3\u30c8\u3092Vue.js<br \/>\n\u57fa\u672c\u7684\u306a\u51e6\u7406\u3092\u8272\u3005\u30e1\u30e2\u3002<\/p>\n<pre class=\"lang:go decode:true \">package main\r\n\r\nimport (\r\n\t\"encoding\/json\"\r\n\t\"fmt\"\r\n\t\"html\/template\"\r\n\t\"io\/ioutil\"\r\n\t\"log\"\r\n\t\"net\/http\"\r\n\t\"os\"\r\n\t\"path\/filepath\"\r\n\r\n\t\"gopkg.in\/mgo.v2\"\r\n)\r\n\r\nfunc JsonMongo1(w http.ResponseWriter, r *http.Request) {\r\n\r\n\tinfo := &amp;mgo.DialInfo{\r\n\t\tAddrs:    []string{\"localhost:27017\"},\r\n\t\tDatabase: \"name_store\",\r\n\t\tUsername: \"shiro\",\r\n\t\tPassword: \"123\",\r\n\t\tSource:   \"admin\",\r\n\t}\r\n\r\n\t\/\/\u901a\u5e38\r\n\t\/\/session, err := mgo.Dial(\"localhost:27017\")\r\n\r\n\t\/\/\u8a8d\u8a3c\r\n\tsession, err := mgo.DialWithInfo(info)\r\n\tif err != nil {\r\n\t\treturn\r\n\t}\r\n\tdefer session.Close()\r\n\tc := session.DB(\"name_store\").C(\"names\")\r\n\r\n\tt := []T{}\r\n\r\n\t\/\/1\u4ef6\u53d6\u5f97\r\n\r\n\t\/\/if err := c.Find(bson.M{\"name\": \"taro\"}).One(&amp;t); != nil{\r\n\t\/\/\tlog.Print(err)\r\n\t\/\/}\r\n\r\n\tif err := c.Find(nil).All(&amp;t); err != nil {\r\n\t\tlog.Print(err)\r\n\t}\r\n\r\n\tw.Header().Set(\"content-type\", \"application\/json;charset=UTF-8\")\r\n\tw.WriteHeader(http.StatusOK)\r\n\r\n\tif err := json.NewEncoder(w).Encode(t); err != nil {\r\n\t\treturn\r\n\t}\r\n\r\n}\r\n\r\nfunc TemplateHtml(w http.ResponseWriter, r *http.Request) {\r\n\r\n\tp, err := os.Executable()\r\n\tif err != nil {\r\n\t\treturn\r\n\t}\r\n\r\n\tt := template.Must(template.ParseFiles(filepath.FromSlash(filepath.Dir(p) + \"\/index.html\")))\r\n\r\n\tif err := t.Execute(w, nil); err != nil {\r\n\t\treturn\r\n\t}\r\n\r\n}\r\n\r\nfunc TemplateText(w http.ResponseWriter, r *http.Request) {\r\n\r\n\tp, err := os.Executable()\r\n\tif err != nil {\r\n\t\treturn\r\n\t}\r\n\r\n\tf, err := ioutil.ReadFile(filepath.FromSlash(filepath.Dir(p) + \"\/vue.html\"))\r\n\tif err != nil {\r\n\t\treturn\r\n\t}\r\n\r\n\tfmt.Fprint(w, string(f))\r\n\r\n}\r\n\r\nfunc FromPost(w http.ResponseWriter, r *http.Request) {\r\n\r\n\tinfo := &amp;mgo.DialInfo{\r\n\t\tAddrs:    []string{\"localhost:27017\"},\r\n\t\tDatabase: \"name_store\",\r\n\t\tUsername: \"shiro\",\r\n\t\tPassword: \"123\",\r\n\t\tSource:   \"admin\",\r\n\t}\r\n\r\n\tbody, err := ioutil.ReadAll(r.Body)\r\n\tif err != nil {\r\n\t\tlog.Print(err)\r\n\t}\r\n\r\n\tt := []T{}\r\n\tif err := json.Unmarshal(body, &amp;t); err != nil {\r\n\t\tlog.Print(err)\r\n\t}\r\n\r\n\t\/\/\u8a8d\u8a3c\r\n\tsession, err := mgo.DialWithInfo(info)\r\n\tif err != nil {\r\n\t\tlog.Print(err)\r\n\t}\r\n\tdefer session.Close()\r\n\tc := session.DB(\"name_store\").C(\"names\")\r\n\r\n\terr = c.Insert(&amp;t[0])\r\n\tif err != nil {\r\n\t\tlog.Print(err)\r\n\t}\r\n\r\n}\r\n\r\nfunc main() {\r\n\r\n\thttp.HandleFunc(\"\/1\", RequestParams)\r\n\thttp.HandleFunc(\"\/2\", QueryKeyValue)\r\n\thttp.HandleFunc(\"\/3\", ResponseJson)\r\n\thttp.HandleFunc(\"\/4\", JsonMongo1)\r\n\thttp.HandleFunc(\"\/5\", TemplateHtml)\r\n\thttp.HandleFunc(\"\/6\", TemplateText)\r\n\thttp.HandleFunc(\"\/7\", FromPost)\r\n\r\n\tif err := http.ListenAndServe(\":9000\", nil); err != nil {\r\n\t\tlog.Fatal(err)\r\n\t}\r\n\r\n}\r\n\r\nfunc RequestParams(w http.ResponseWriter, r *http.Request) {\r\n\r\n\tif r.Method != http.MethodGet {\r\n\t\treturn\r\n\t}\r\n\r\n\tfmt.Fprintf(w, \"%s\\n\", r.URL.Host)\r\n\tfmt.Fprintf(w, \"%s\\n\", r.URL.Path)\r\n\tfmt.Fprintf(w, \"%s\\n\", r.URL.RawQuery)\r\n}\r\n\r\nfunc QueryKeyValue(w http.ResponseWriter, r *http.Request) {\r\n\r\n\tif r.Method != http.MethodGet {\r\n\t\treturn\r\n\t}\r\n\r\n\tfor key, value := range r.URL.Query() {\r\n\t\tfmt.Fprintf(w, \"key: %s\\n\", key)\r\n\t\tfor i, v := range value {\r\n\t\t\tfmt.Fprintf(w, \"value[%d]: %s\\n\", i, v)\r\n\t\t}\r\n\t}\r\n}\r\n\r\ntype T struct {\r\n\tID   json.Number\r\n\tName string\r\n}\r\n\r\nfunc ResponseJson(w http.ResponseWriter, r *http.Request) {\r\n\r\n\tt := T{\r\n\t\tID:   \"1\",\r\n\t\tName: \"taro\",\r\n\t}\r\n\r\n\tw.Header().Set(\"content-type\", \"application\/json;charset=UTF-8\")\r\n\tw.WriteHeader(http.StatusOK)\r\n\r\n\tif err := json.NewEncoder(w).Encode(t); err != nil {\r\n\t\treturn\r\n\t}\r\n\r\n}\r\n<\/pre>\n<pre class=\"lang:js decode:true \">&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n    &lt;meta charset=\"UTF-8\" \/&gt;\r\n    &lt;title&gt;title&lt;\/title&gt;\r\n    &lt;script src=\"https:\/\/cdn.jsdelivr.net\/npm\/vue\/dist\/vue.js\"&gt;&lt;\/script&gt;\r\n    &lt;script src=\"https:\/\/unpkg.com\/axios\/dist\/axios.min.js\"&gt;&lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n\r\n&lt;div id=\"app\"&gt;\r\n\r\n&lt;input type=\"button\" v-on:click=\"fetchData\" value=\"Get\"&gt;\r\n&lt;input type=\"button\" v-on:click=\"insertData\" value=\"Up\"&gt;\r\n\r\n&lt;br&gt;\r\n\r\n&lt;table&gt;\r\n        &lt;tr v-for=\"i in res\" v-bind:id=\"i.ID\"&gt;\r\n            &lt;td&gt;{{ i.ID }}&lt;\/td&gt;\r\n            &lt;td&gt;{{ i.Name }}&lt;\/td&gt;\r\n        &lt;\/tr&gt;\r\n&lt;\/table&gt;\r\n\r\n&lt;input type=\"text\" v-bind:value=\"v\"&gt;\r\n&lt;input type=\"range\" max=\"1\" min=\"0\" step=\"0.25\" v-model=\"v\"&gt;\r\n\r\n\r\n\r\n&lt;\/div&gt;\r\n\r\n&lt;script&gt;\r\n\r\nlet vm = new Vue({\r\n    el: '#app',\r\n    data:{\r\n        v: 0,\r\n        res: [\r\n            {\"ID\":1, \"Name\":\"taro\"},\r\n            {\"ID\":2, \"Name\":\"jiro\"}\r\n        ]\r\n    },\r\n    methods:{\r\n        insertData(){\r\n            console.log(vm.res);\r\n            axios\r\n            .post('http:\/\/localhost:9000\/7',vm.res)\r\n            .then(response =&gt; {\r\n                console.log(response.status) \r\n            })\r\n        },\r\n        fetchData(){\r\n            axios\r\n            .get('http:\/\/localhost:9000\/4')\r\n            .then(response =&gt; {\r\n\r\n                \/\/forceUpdate\u3067\u30ea\u30a2\u30af\u30c6\u30a3\u30d6\u306b\u306a\u308b\r\n                \/\/vm.res[1] = response.data;\r\n                \/\/vm.$forceUpdate();\r\n\r\n                console.log(response.status) \r\n\r\n                \/\/vm.res\u306f\u914d\u5217\r\n\r\n                \/\/\u30ec\u30b9\u30dd\u30f3\u30b9\u306ejson\u304c\u914d\u5217\u3067\u306f\u306a\u3044\u5834\u5408\u306e\u6307\u5b9a\u306e\u65b9\u6cd5\r\n                \/\/Vue.set(vm.res, 1, response.data);\r\n\r\n                \/\/\u914d\u5217\u306e\u5834\u5408\u306e\u6307\u5b9a\u306e\u65b9\u6cd5(vm.res\u304c\u5168\u3066\u7f6e\u304d\u63db\u308f\u308b)\r\n                Vue.set(vm, 'res', response.data)\r\n                \r\n            })\r\n        }\r\n    } \r\n})\r\n\r\n&lt;\/script&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u30b5\u30fc\u30d0\u30fc\u3092golang+MongoDB\u3067\u30d5\u30ed\u30f3\u30c8\u3092Vue.js \u57fa\u672c\u7684\u306a\u51e6\u7406\u3092\u8272\u3005\u30e1\u30e2\u3002 package main import ( &#8220;encoding\/json&#8221; &#8220;fmt&#8221; &#8220;html\/template&#8221; &#8220;io\/i &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/appbay.org\/?p=2446\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;golang+MongoDB+Vue.js \u8272\u3005&#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":[148,4],"class_list":["post-2446","post","type-post","status-publish","format-standard","hentry","category-1","tag-golang","tag-javascript-typescript"],"_links":{"self":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/2446","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=2446"}],"version-history":[{"count":5,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/2446\/revisions"}],"predecessor-version":[{"id":3566,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/2446\/revisions\/3566"}],"wp:attachment":[{"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2446"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2446"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2446"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}