サーバーをgolang+MongoDBでフロントをVue.js
基本的な処理を色々メモ。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
package main import ( "encoding/json" "fmt" "html/template" "io/ioutil" "log" "net/http" "os" "path/filepath" "gopkg.in/mgo.v2" ) func JsonMongo1(w http.ResponseWriter, r *http.Request) { info := &mgo.DialInfo{ Addrs: []string{"localhost:27017"}, Database: "name_store", Username: "shiro", Password: "123", Source: "admin", } //通常 //session, err := mgo.Dial("localhost:27017") //認証 session, err := mgo.DialWithInfo(info) if err != nil { return } defer session.Close() c := session.DB("name_store").C("names") t := []T{} //1件取得 //if err := c.Find(bson.M{"name": "taro"}).One(&t); != nil{ // log.Print(err) //} if err := c.Find(nil).All(&t); err != nil { log.Print(err) } w.Header().Set("content-type", "application/json;charset=UTF-8") w.WriteHeader(http.StatusOK) if err := json.NewEncoder(w).Encode(t); err != nil { return } } func TemplateHtml(w http.ResponseWriter, r *http.Request) { p, err := os.Executable() if err != nil { return } t := template.Must(template.ParseFiles(filepath.FromSlash(filepath.Dir(p) + "/index.html"))) if err := t.Execute(w, nil); err != nil { return } } func TemplateText(w http.ResponseWriter, r *http.Request) { p, err := os.Executable() if err != nil { return } f, err := ioutil.ReadFile(filepath.FromSlash(filepath.Dir(p) + "/vue.html")) if err != nil { return } fmt.Fprint(w, string(f)) } func FromPost(w http.ResponseWriter, r *http.Request) { info := &mgo.DialInfo{ Addrs: []string{"localhost:27017"}, Database: "name_store", Username: "shiro", Password: "123", Source: "admin", } body, err := ioutil.ReadAll(r.Body) if err != nil { log.Print(err) } t := []T{} if err := json.Unmarshal(body, &t); err != nil { log.Print(err) } //認証 session, err := mgo.DialWithInfo(info) if err != nil { log.Print(err) } defer session.Close() c := session.DB("name_store").C("names") err = c.Insert(&t[0]) if err != nil { log.Print(err) } } func main() { http.HandleFunc("/1", RequestParams) http.HandleFunc("/2", QueryKeyValue) http.HandleFunc("/3", ResponseJson) http.HandleFunc("/4", JsonMongo1) http.HandleFunc("/5", TemplateHtml) http.HandleFunc("/6", TemplateText) http.HandleFunc("/7", FromPost) if err := http.ListenAndServe(":9000", nil); err != nil { log.Fatal(err) } } func RequestParams(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet { return } fmt.Fprintf(w, "%s\n", r.URL.Host) fmt.Fprintf(w, "%s\n", r.URL.Path) fmt.Fprintf(w, "%s\n", r.URL.RawQuery) } func QueryKeyValue(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet { return } for key, value := range r.URL.Query() { fmt.Fprintf(w, "key: %s\n", key) for i, v := range value { fmt.Fprintf(w, "value[%d]: %s\n", i, v) } } } type T struct { ID json.Number Name string } func ResponseJson(w http.ResponseWriter, r *http.Request) { t := T{ ID: "1", Name: "taro", } w.Header().Set("content-type", "application/json;charset=UTF-8") w.WriteHeader(http.StatusOK) if err := json.NewEncoder(w).Encode(t); err != nil { return } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>title</title> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script src="https://unpkg.com/axios/dist/axios.min.js"></script> </head> <body> <div id="app"> <input type="button" v-on:click="fetchData" value="Get"> <input type="button" v-on:click="insertData" value="Up"> <br> <table> <tr v-for="i in res" v-bind:id="i.ID"> <td>{{ i.ID }}</td> <td>{{ i.Name }}</td> </tr> </table> <input type="text" v-bind:value="v"> <input type="range" max="1" min="0" step="0.25" v-model="v"> </div> <script> let vm = new Vue({ el: '#app', data:{ v: 0, res: [ {"ID":1, "Name":"taro"}, {"ID":2, "Name":"jiro"} ] }, methods:{ insertData(){ console.log(vm.res); axios .post('http://localhost:9000/7',vm.res) .then(response => { console.log(response.status) }) }, fetchData(){ axios .get('http://localhost:9000/4') .then(response => { //forceUpdateでリアクティブになる //vm.res[1] = response.data; //vm.$forceUpdate(); console.log(response.status) //vm.resは配列 //レスポンスのjsonが配列ではない場合の指定の方法 //Vue.set(vm.res, 1, response.data); //配列の場合の指定の方法(vm.resが全て置き換わる) Vue.set(vm, 'res', response.data) }) } } }) </script> </body> </html> |