-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
147 lines (133 loc) · 4.01 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
package main
import (
"encoding/json"
"fmt"
"html/template"
"image"
"image/draw"
"image/png"
"log"
"net/http"
"os"
"path"
"websoft.club/weather/model"
"websoft.club/weather/util"
)
// 城市码
// 来源:https://apip.weatherdt.com/float/static/js/city.js
// 欢迎页面
func welcome(resp http.ResponseWriter, reqs *http.Request) {
basePath := path.Base(reqs.URL.Path)
if basePath == "." || basePath == "/" {
citycode := reqs.URL.Query().Get("code")
// 没有城市码则读取IP地址对应的地址
if citycode == "" {
citycode = "101200805" // 默认为监利
ipaddr := util.ClientIP(reqs)
datas, err := util.GetCityByIP(ipaddr)
if err == nil {
// 由IP地址对应的城市名,找到对应的天气城市码
if country, ok := datas["country_name"]; ok && country == "中国" {
cityname := datas["city_name"]
if citys := util.SearchCity(cityname); citys != nil && len(citys) > 0 {
citycode = citys[0].Code
}
}
}
}
// 由城市码查询对应的城市天气
result, err := util.GetCNWeather(citycode)
if err == nil && result != nil {
templates := template.Must(template.ParseFiles("templates/index.html"))
templates.Execute(resp, result)
return
}
templates := template.Must(template.ParseFiles("templates/error.html"))
templates.Execute(resp, err)
return
}
resp.WriteHeader(404)
fmt.Fprintf(resp, "<h1>404 Not Found!<h1>")
}
// 天气图标
func weatherIcon(resp http.ResponseWriter, reqs *http.Request) {
resp.Header().Set("Content-Type", "image/png")
size := reqs.URL.Query().Get("size") // 图标大小及模式
wcode := reqs.URL.Query().Get("wcode") // 天气代码
if wcode == "" {
wcode = "n99"
}
wiconData := util.GetWeatherIcon(size, wcode)
bgfile, _ := os.Open(wiconData.FatherIconsLocation)
defer bgfile.Close()
bgimg, _ := png.Decode(bgfile)
weatherIcon := image.NewRGBA(image.Rect(0, 0, wiconData.Width, wiconData.Height))
draw.Draw(weatherIcon, bgimg.Bounds().Add(wiconData.Position), bgimg, bgimg.Bounds().Min, draw.Src)
png.Encode(resp, weatherIcon)
}
// 城市天气结果JSON对象
type weatherResult struct {
Code int `json:"code"`
Message string `json:"message"`
Weather *model.Weather `json:"weather,omitempty"`
}
// 根据城市码获取天气信息
func weather(resp http.ResponseWriter, reqs *http.Request) {
resp.Header().Set("Content-Type", "application/json;charset=UTF-8")
datas := weatherResult{
Code: 500,
Message: "获取失败",
Weather: nil,
}
citycode := path.Base(reqs.URL.Path)
if citycode != "/" && citycode != "." && citycode != "weather" {
result, err := util.GetCNWeather(citycode)
if err != nil {
datas.Message = err.Error()
} else {
datas.Code = 200
datas.Message = "获取成功"
datas.Weather = result
}
} else {
datas.Message = "请指定有效的城市码!"
}
jsonStr, _ := json.Marshal(datas)
fmt.Fprintln(resp, string(jsonStr))
}
// 搜索城市结果JSON对象
type cityResult struct {
Code int `json:"code"`
Message string `json:"message"`
Citys []*util.SearchCityName `json:"citys,omitempty"`
}
// 查找对应的城市编码
func searchCity(resp http.ResponseWriter, reqs *http.Request) {
resp.Header().Set("Content-Type", "application/json;charset=UTF-8")
datas := cityResult{
Code: 500,
Message: "未找到结果",
Citys: nil,
}
name := reqs.URL.Query().Get("name")
if name != "" {
if citys := util.SearchCity(name); citys != nil && len(citys) > 0 {
datas.Code = 200
datas.Message = "找到结果"
datas.Citys = citys
}
}
jsonStr, _ := json.Marshal(datas)
fmt.Fprintln(resp, string(jsonStr))
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", welcome)
// 静态文件处理
files := http.FileServer(http.Dir("public"))
mux.Handle("/resources/", http.StripPrefix("/resources/", files))
mux.HandleFunc("/icon.png", weatherIcon)
mux.HandleFunc("/weather/", weather)
mux.HandleFunc("/city/search", searchCity)
log.Fatal(http.ListenAndServe("127.0.0.1:9001", mux))
}