文章目錄
- 前言
- 先上圖
- 技術支撐
- 用戶 IP 地址獲取
- 天氣預報 API
- 開始搭建
- beego 環境搭建
- 網站專案撰寫
- 部署
- 完工
前言
前段時間在家搭建了一臺服務器《云服務器續費太貴,直接在家搭一臺,再也不受約束了》,有小伙伴留言想看后續服務部署,今天就從0-1部署一個一直使用的天氣預報網站,這個網站已經持續運行了一年多了,穩如老gou,大家可以使用一下,
先上圖

主要功能:
- 根據訪問者所在城市自動查詢當地天氣
- 查詢指定城市的天氣
- 未來七天的溫度走勢圖
- 天氣情況每半小時左右更新一次
技術支撐
用戶 IP 地址獲取

訪問網址 http://pv.sohu.com/cityjson 即可獲得訪問者的公網 IP、所在城市、所在城市代碼,這里我們會用到的資訊是用戶的公網 IP,
天氣預報 API
這里我們用到的天氣 API 服務網站是 https://tianqiapi.com/ 大家可以免費注冊使用,
開始搭建
作業系統:Centos 8.3
beego 環境搭建
網站使用的是基于 Go 語言的 beego 環境
- 安裝 Git
yum install git - 安裝 go 語言環境
- 下載安裝包
https://golang.org/dl/ - 解壓
tar -C /usr/local -xzf go1.16.5.linux-amd64.tar.gz - 添加環境變數
export PATH=$PATH:/usr/local/go/bin
- 安裝 beego
go get github.com/astaxie/beego
如果出現下載超時的情況,可以設定代理
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.io,direct - 安裝 bee 工具
go get github.com/beego/bee
執行后將在$GOPATH/bin目錄下安裝二進制檔案bee,接著我們需要為此目錄添加環境變數
export PATH=$GOPATH/bin
至此,beego 環境就搭建完成了,我們可以測驗一下
# cd ~/go/src/
[root@Box src]# bee new hello
2021/06/11 16:50:34 INFO ? 0001 generate new project support go modules.
2021/06/11 16:50:34 INFO ? 0002 Creating application...
create /root/go/src/hello/go.mod
create /root/go/src/hello/
create /root/go/src/hello/conf/
create /root/go/src/hello/controllers/
create /root/go/src/hello/models/
create /root/go/src/hello/routers/
create /root/go/src/hello/tests/
create /root/go/src/hello/static/
create /root/go/src/hello/static/js/
create /root/go/src/hello/static/css/
create /root/go/src/hello/static/img/
create /root/go/src/hello/views/
create /root/go/src/hello/conf/app.conf
create /root/go/src/hello/controllers/default.go
create /root/go/src/hello/views/index.tpl
create /root/go/src/hello/routers/router.go
create /root/go/src/hello/tests/default_test.go
create /root/go/src/hello/main.go
2021/06/11 16:50:34 SUCCESS ? 0003 New application successfully created!
[root@Box src]# cd hello/
[root@Box hello]# bee run
______
| ___ \
| |_/ / ___ ___
| ___ \ / _ \ / _ \
| |_/ /| __/| __/
\____/ \___| \___| v1.12.0
2021/06/11 16:50:41 INFO ? 0001 Using 'hello' as 'appname'
2021/06/11 16:50:41 INFO ? 0002 Initializing watcher...
go: github.com/astaxie/beego@v1.12.1: missing go.sum entry; to add it:
go mod download github.com/astaxie/beego
2021/06/11 16:50:41 ERROR ? 0003 Failed to build the application: go: github.com/astaxie/beego@v1.12.1: missing go.sum entry; to add it:
go mod download github.com/astaxie/beego
^C
[root@Box hello]# go mod tidy
go: finding module for package github.com/shiena/ansicolor
go: found github.com/shiena/ansicolor in github.com/shiena/ansicolor v0.0.0-20200904210342-c7312218db18
[root@Box hello]# bee run
______
| ___ \
| |_/ / ___ ___
| ___ \ / _ \ / _ \
| |_/ /| __/| __/
\____/ \___| \___| v1.12.0
2021/06/11 16:52:37 INFO ? 0001 Using 'hello' as 'appname'
2021/06/11 16:52:37 INFO ? 0002 Initializing watcher...
hello/controllers
hello/routers
hello
2021/06/11 16:52:40 SUCCESS ? 0003 Built Successfully!
2021/06/11 16:52:40 INFO ? 0004 Restarting 'hello'...
2021/06/11 16:52:40 SUCCESS ? 0005 './hello' is running...
2021/06/11 16:52:40.991 [I] [asm_amd64.s:1371] http server Running on http://:8080

網站專案撰寫
下面是專案目錄
# tree
.
├── city.json
├── conf
│ └── app.conf
├── controllers
│ ├── default.go
│ └── tianqiapi.go
├── go.mod
├── go.sum
├── go_web
├── main.go
├── routers
│ └── router.go
├── static
│ ├── css
│ │ ├── mystyle.css
│ │ └── style.css
│ ├── html
│ │ └── weather.html
│ ├── img
│ │ └── cucumber
│ │ ├── bingbao.png
│ │ ├── lei.png
│ │ ├── qing.png
│ │ ├── shachen.png
│ │ ├── wu.png
│ │ ├── xue.png
│ │ ├── yin.png
│ │ ├── yun.png
│ │ └── yu.png
│ └── js
│ ├── ajax.js
│ ├── lineChart.js
│ ├── method.js
│ ├── mycity.js
│ ├── mymethod.js
│ └── reload.min.js
├── tests
│ └── default_test.go
└── views
└── index.html
11 directories, 29 files
由于專案本身還是有些復雜的,這里介紹起來太占篇幅了,我把原始碼上傳到了 CodeChina https://codechina.csdn.net/lyndon_li/go_web,有需要的伙伴可以下載下來跑一跑,有任何問題都可以留言探討,
注意:
tianqiapi.go 里面需要用到個人的注冊資訊:appid 和 appsecret,需要大家到 https://tianqiapi.com/ 注冊,然后填入代碼即可

func getWeather(dataType string, value string) string {
params := url.Values{}
Url, _ := url.Parse("https://www.tianqiapi.com/api/")
params.Set("appid", "xxx")
params.Set("appsecret", "xxx")
params.Set("version", "v1")
if dataType == "cityid" {
params.Set("cityid", value)
} else if dataType == "ip" {
params.Set("ip", value)
}
//如果引數中有中文引數,這個方法會進行URLEncode
Url.RawQuery = params.Encode()
urlPath := Url.String()
fmt.Println(urlPath) //等同于https://www.xxx.com?age=23&name=zhaofan
resp, _ := http.Get(urlPath)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
// fmt.Println(string(body))
v, _ := UnescapeUnicode(body)
// fmt.Println(string(v))
return string(v)
}
部署
使用 bee pack 命令將專案進行打包,這使得我們可以在本地開發,完成后打包部署到服務器上,不用擔心環境和依賴等問題,非常方便,
部署后可以直接已二進制的方式運行
# ./go_web
2021/06/12 01:45:50.004 [I] [asm_amd64.s:1357] http server Running on http://:8345
手動查詢一下別的城市的天氣

完工
至此,天氣預報網站已搭建完成,期待小伙伴們都能用上自己搭建天氣預報網站!
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/287193.html
標籤:其他
