-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
58 lines (52 loc) · 1.07 KB
/
http.go
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
package main
import (
"fmt"
"strconv"
"encoding/json"
"log"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, World")
}
func ItemsRead(w http.ResponseWriter, r *http.Request) {
ids, ok := r.URL.Query()["id"]
if !ok || len(ids[0]) < 1 {
ids = nil
}
items := []map[string]interface{}{
map[string]interface{}{
"id": 1,
"name": "商品1",
"barcode": "1234",
},
map[string]interface{}{
"id": 2,
"name": "商品2",
"barcode": "1235",
},
}
if ids != nil {
res := []map[string]interface{}{}
id, _ := strconv.Atoi(ids[0])
for _, item := range items {
if id == item["id"].(int) {
res = append(res, item)
}
}
items = res
}
bytes, err := json.Marshal(items)
if err != nil {
log.Println(err)
return
}
w.Write(bytes)
}
func main() {
// http.HandleFunc("/", handler) // ハンドラーを登録してWebページを表示させる
http.Handle("/", http.FileServer(http.Dir("front")))
http.HandleFunc("/api/items", ItemsRead)
http.ListenAndServe(":8008", nil)
// fmt.Println("hello, world\n")
}