-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpFunctions.go
More file actions
94 lines (76 loc) · 1.98 KB
/
httpFunctions.go
File metadata and controls
94 lines (76 loc) · 1.98 KB
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
package main
import (
"encoding/json"
"net/http"
"strconv"
"example.com/gcf/db"
)
func InitRouter() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
weatherData(w, r)
})
}
func weatherData(w http.ResponseWriter, r *http.Request) {
queryNX := r.URL.Query().Get("nx")
queryNY := r.URL.Query().Get("ny")
var message []string
var errCount int
nx, err := strconv.Atoi(queryNX)
if err != nil {
errCount++
message = append(message, "nx is not a number")
}
ny, err := strconv.Atoi(queryNY)
if err != nil {
errCount++
message = append(message, "ny is not a number")
}
if errCount > 0 {
type ErrorResponse struct {
Errors []string `json:"errors"`
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
err = json.NewEncoder(w).Encode(ErrorResponse{Errors: message})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte("server error"))
}
return
}
data, success := db.GetData(nx, ny)
if !success {
http.Error(w, "데이터 찾지 못하거나 존재하지 않음", http.StatusBadRequest)
return
}
response := dataProcess(data)
w.Header().Set("Content-Type", "application/json")
encodeErr := json.NewEncoder(w).Encode(response)
if encodeErr != nil {
http.Error(w, "서버에서 데이터 변환 실패.", http.StatusInternalServerError)
}
}
type resData struct {
Name string `json:"Name"`
FcstDate string `json:"FcstDate"`
AvgTempera float64 `json:"AvgTempera"`
Wash string `json:"Wash"`
Sky int `json:"Sky"`
Wind float64 `json:"Wind"`
}
// dataProcess 필요한 데이터만 가공하여 반환합니다.
func dataProcess(data []*db.Weather) []resData {
var res []resData
for _, doc := range data {
slice := resData{
Name: doc.Name,
FcstDate: doc.FcstDate,
AvgTempera: doc.AvgTempera,
Wash: doc.Wash,
Sky: doc.Sky,
Wind: doc.Wind,
}
res = append(res, slice)
}
return res
}