-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
179 lines (132 loc) · 4.37 KB
/
main.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
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
package main
import (
"encoding/json"
"github.com/14km/9th-depromeet-conf/api"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"net/http"
)
type response struct {
Documents []document `json:"documents"`
}
type document struct {
AddressName string `json:"address_name"`
AddressType string `json:"address_type"`
RoadAddress string `json:"road_address"`
X string `json:"x"`
Y string `json:"y"`
}
type responseDto struct {
Weather WeatherDto `json:"weather"`
AirPollution AirPollutionDto `json:"airPollution"`
}
type responseWeekDto struct {
Weather WeatherWeekDto `json:"weather"`
AirPollution AirPollutionDto `json:"airPollution"`
}
type WeatherDto struct {
List interface{} `json:"list"`
City interface{} `json:"city"`
}
type WeatherWeekDto struct {
Lat interface{} `json:"lat"`
Lon interface{} `json:"lon"`
Timezone interface{} `json:"timezone"`
Current interface{} `json:"current"`
Daily interface{} `json:"daily"`
}
type AirPollutionDto struct {
List interface{} `json:"list"`
}
func main() {
// Echo instance
e := echo.New()
e.Use(middleware.CORS())
// Routes
e.GET("/ping", hello)
e.GET("/kakao", kakao)
e.GET("/weather-map", weather)
e.GET("/weather", addressAfterWeather)
e.GET("/weather-air", weatherAndAir)
e.GET("/weather-week-air", weatherForWeekAndAir)
e.Logger.Fatal(e.Start(":1323"))
}
// Handler
func hello(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!!!! V3")
}
func addressAfterWeather(c echo.Context) error {
address := c.QueryParam("address")
api.SettingByKakao("https://dapi.kakao.com", "1234")
api.SettingByWeatherMap("https://api.openweathermap.org", "1234")
var info response
kakaoApi, _ := api.GetAddressContentsByKakaoApi(address)
if err := json.Unmarshal(kakaoApi, &info); err != nil {
panic(err)
}
lat := info.Documents[0].Y
lon := info.Documents[0].X
weather, _ := api.GetWeatherByWeatherMapApi(lat, lon)
airPollution, _ := api.GetAirPollutionByWeatherMapApi(lat, lon)
var weatherDto WeatherDto
var airPollutionDto AirPollutionDto
_ = json.Unmarshal(weather, &weatherDto)
_ = json.Unmarshal(airPollution, &airPollutionDto)
u := &responseDto{
Weather: weatherDto,
AirPollution: airPollutionDto,
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
c.Response().WriteHeader(http.StatusOK)
return json.NewEncoder(c.Response()).Encode(u)
}
func weatherAndAir(c echo.Context) error {
lat := c.QueryParam("lat")
lon := c.QueryParam("lon")
api.SettingByWeatherMap("https://api.openweathermap.org", "1234")
weather, _ := api.GetWeatherByWeatherMapApi(lat, lon)
airPollution, _ := api.GetAirPollutionByWeatherMapApi(lat, lon)
var weatherDto WeatherDto
var airPollutionDto AirPollutionDto
_ = json.Unmarshal(weather, &weatherDto)
_ = json.Unmarshal(airPollution, &airPollutionDto)
u := &responseDto{
Weather: weatherDto,
AirPollution: airPollutionDto,
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
c.Response().WriteHeader(http.StatusOK)
return json.NewEncoder(c.Response()).Encode(u)
}
func weatherForWeekAndAir(c echo.Context) error {
lat := c.QueryParam("lat")
lon := c.QueryParam("lon")
api.SettingByWeatherMap("https://api.openweathermap.org", "1234")
weather, _ := api.GetWeatherForWeekByWeatherMapApi(lat, lon)
airPollution, _ := api.GetAirPollutionByWeatherMapApi(lat, lon)
var weatherDto WeatherWeekDto
var airPollutionDto AirPollutionDto
_ = json.Unmarshal(weather, &weatherDto)
_ = json.Unmarshal(airPollution, &airPollutionDto)
u := &responseWeekDto{
Weather: weatherDto,
AirPollution: airPollutionDto,
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
c.Response().WriteHeader(http.StatusOK)
return json.NewEncoder(c.Response()).Encode(u)
}
func kakao(c echo.Context) error {
address := c.QueryParam("address")
api.SettingByKakao("https://dapi.kakao.com", "1234")
kakaoApi, _ := api.GetAddressContentsByKakaoApi(address)
return c.String(http.StatusOK, string(kakaoApi))
}
func weather(c echo.Context) error {
//lat string, lon string
lat := c.QueryParam("lat")
lon := c.QueryParam("lon")
api.SettingByWeatherMap("https://api.openweathermap.org", "1234")
weather, _ := api.GetWeatherByWeatherMapApi(lat, lon)
return c.String(http.StatusOK, string(weather))
}