Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
/.idea
/.idea
*/*/Gopkg.lock
*/*/_dev
vendor
*/*/go.sum
2 changes: 0 additions & 2 deletions Config/mysql.toml

This file was deleted.

43 changes: 43 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
.PHONY: dev release push publish


PWD := $(shell pwd)
USER := $(shell id -nu)

DEV_NAME = $(USER)-$(PWD)

all: dev

dev:
cd docker/dev && docker-compose -p "$(DEV_NAME)" down && docker-compose -p "$(DEV_NAME)" up --force-recreate

dep:
docker run --rm \
-v $(PWD)/src/app:/go/src/app \
-v /tmp/golang-dep:/go/pkg/dep \
-w /go/src/app \
-ti docker.io/library/golang:1.13.0-buster \
sh -c 'curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh && dep ensure -v'

gomod:
docker run --rm \
-v $(PWD)/src/app:/go/src/app \
-v /tmp/golang-mod:/go/pkg/mod \
-w /go/src/app \
-e GOPROXY=https://goproxy.cn \
-ti docker.io/library/golang:1.13.0-buster \
go mod vendor

build-server:
docker run --rm \
-v $(PWD)/src/app:/go/src/app \
-w /go/src/app \
-ti docker.io/library/golang:1.13.0-buster \
go build -o ./App/Server ./App/Server.go

build-gethot:
docker run --rm \
-v $(PWD)/src/app:/go/src/app \
-w /go/src/app \
-ti docker.io/library/golang:1.13.0-buster \
go build -o ./App/GetHot ./App/GetHot.go
86 changes: 40 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,59 +6,53 @@
**今日热榜是一个获取各大热门网站热门头条的聚合网站,使用Go语言编写,多协程异步快速抓取信息,预览:[https://www.printf520.com/hot.html][热榜]**
![DeepinScrot-4337.png](https://i.loli.net/2019/08/05/PjX2nqWAgM5xsL4.png)

### 安装教程
### 开发教程

1. 编译

```
cd {root_path} # 项目根目录
go build -o ./App/GetHot App/GetHot.go
go build -o ./App/Server App/Server.go
```

2. 创建数据库,如 `news`,执行database.sql创建表,更改配置文件`Config/mysql.toml`

3. 编辑文件 `Html/js/blog/globalConfig.js`

```
const ServerIp = 'http://{your_domain}:9090' // 替换成服务器域名
```

4. 部署定时任务/App/GetHot.go爬虫程序,且以守护进程的方式执行Server.go

```
crontab -e # 添加一行 0 */1 * * * {root_path}/App/GetHot
nohup {root_path}/App/Server &
```

5. 测试

- 打开`http://{yourdomain}:9090/` 即可访问今日热榜
1. 将 `/your/path/to/TopList/src` 添加到 $GOPATH,(如果用IDE开发,需要代码提示的话)
2. `cd {root_path}` # 项目根目录
3. `cp docker/dev/default.env docker/dev/.env` # 复制docker-compose环境文件
4. `make gomod` 或 `make dep` # 安装依赖包, `make gomod`是使用 `gomodule`, `make dep`是使用 `dep` 工具
5. `make dev` # 启动。会根据database.sql自动创建数据库,同时使用 [AIR](https://github.com/cosmtrek/air), 不需要重启即可热重载
6. 等待服务启动完毕,打开`http://{yourdomain}:{DEV_PORT}/` 即可访问今日热榜

### 编译教程
```
cd {root_path} # 项目根目录
make build-server
make build-gethot
```

### 目录说明

```
TopList/
├── App
│   ├── GetHot.go 爬虫程序需要Cron定时任务执行
│   └── Server.go Server程序需要守护进程的方式执行
├── Common
│   ├── Db.go DB组件
│   └── Message.go
├── Config
│   ├── MySql.go mysql配置读取组件
│   └── mysql.toml mysql配置文件需要手动配置
├── Cron
│   ├── GetHot.sh 爬虫Cron程序可以是每小时执行一次
│   └── README.md
├── database.sql 数据库建表文件
├── Html
│   ├── css
│   ├── hot.html 前端热榜展示网页
│   └── js
│  
└── README.md
├── docker
│   └── dev
│ ├── air # air 的可执行文件
│ ├── air-gethot.conf # GetHot 的 air 热加载配置
│ ├── air-server.conf # Server 的 air 热加载配置
│ ├── default.env # docker-compose 默认的环境变量
│ └── docker-compose.yaml # docker-compose 配置文件
└── src
└── app
├── App
│   ├── GetHot.go 爬虫程序需要Cron定时任务执行
│   └── Server.go Server程序需要守护进程的方式执行
├── Common
│   ├── Db.go DB组件
│   ├── Message.go
│  └── database.sql 数据库建表文件
├── Config
│   ├── MySql.go mysql配置读取组件
│   └── mysql.toml mysql配置文件需要手动配置
├── Cron
│   ├── GetHot.sh 爬虫Cron程序可以是每小时执行一次
│   └── README.md
├── Html
│   ├── css
│   ├── hot.html 前端热榜展示网页
│   └── js
└── README.md
```

### API说明
Expand Down
5 changes: 5 additions & 0 deletions docker/dev/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DEV_USER=h1z3y3
DEV_PORT=9090
MYSQL_DATABASE=toplist
MYSQL_ROOT_PASSWORD=a1b2c3d4e
MYSQL_ROOT_HOST=%
Binary file added docker/dev/air
Binary file not shown.
24 changes: 24 additions & 0 deletions docker/dev/air-gethot.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# conf for [Air](https://github.com/cosmtrek/air) in TOML format

# working directory
root = "." # . or absolute path, please note that the directories following must be under root
watch_dir = "" # optional! if no watch_dir, use root
tmp_dir = "_dev/"

[build]
bin = "./_dev/run-build" #这里写执行时需要的参数
cmd = "go build -o ./_dev/run-build ./App/GetHot.go"
log = "server-errors.log" # this log file places in your tmp_dir
include_ext = ["go", "tpl", "tmpl", "json", "toml"]
exclude_dir = ["assets", "tmp", "vendor", "frontend/node_modules"]
delay = 800 # ms

[log]
time = true

[color]
main = "magenta"
watcher = "cyan"
build = "yellow"
runner = "green"
app = "white"
24 changes: 24 additions & 0 deletions docker/dev/air-server.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# conf for [Air](https://github.com/cosmtrek/air) in TOML format

# working directory
root = "." # . or absolute path, please note that the directories following must be under root
watch_dir = "" # optional! if no watch_dir, use root
tmp_dir = "_dev/"

[build]
bin = "./_dev/run-build" #这里写执行时需要的参数
cmd = "go build -o ./_dev/run-build ./App/Server.go"
log = "server-errors.log" # this log file places in your tmp_dir
include_ext = ["go", "tpl", "tmpl", "json", "toml"]
exclude_dir = ["assets", "tmp", "vendor", "frontend/node_modules"]
delay = 800 # ms

[log]
time = true

[color]
main = "magenta"
watcher = "cyan"
build = "yellow"
runner = "green"
app = "white"
5 changes: 5 additions & 0 deletions docker/dev/default.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DEV_USER=h1z3y3
DEV_PORT=9090
MYSQL_DATABASE=toplist
MYSQL_ROOT_PASSWORD=a1b2c3d4e
MYSQL_ROOT_HOST=%
66 changes: 66 additions & 0 deletions docker/dev/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
version: '2.0'
services:
server:
image: docker.io/library/golang:1.13.0-buster
container_name: toplist-server-${DEV_PORT}
working_dir: /go/src/app
command:
- sh
- -c
- |
#使用air做动态代码检查,代码变更后自动重新build并运行
#!!!禁止用于生产环境
#curl -fLo /usr/local/bin/air https://raw.githubusercontent.com/cosmtrek/air/master/bin/linux/air
echo "Asia/Shanghai" >> /etc/timezone
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
chmod +x /usr/local/bin/air
cd /go/src/app
air -c /etc/air/air.conf
volumes:
- ../../src:/go/src/
#用于开发环境动态加载代码变更
- ./air-server.conf:/etc/air/air.conf
- ./air:/usr/local/bin/air
links:
- mysql:mysql
ports:
- "${DEV_PORT}:9090"
gethot:
image: docker.io/library/golang:1.13.0-buster
container_name: toplist-gethot-crontab
working_dir: /go/src/app
command:
- sh
- -c
- |
#使用air做动态代码检查,代码变更后自动重新build并运行
#!!!禁止用于生产环境
#curl -fLo /usr/local/bin/air https://raw.githubusercontent.com/cosmtrek/air/master/bin/linux/air
echo "Asia/Shanghai" >> /etc/timezone
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
chmod +x /usr/local/bin/air
cd /go/src/app
air -c /etc/air/air.conf
volumes:
- ../../src:/go/src/
#用于开发环境动态加载代码变更
- ./air-gethot.conf:/etc/air/air.conf
- ./air:/usr/local/bin/air
links:
- mysql:mysql
mysql:
image: docker.io/library/mysql:8
container_name: toplist-mysql-3306
environment:
MYSQL_DATABASE: "${MYSQL_DATABASE}"
MYSQL_ROOT_PASSWORD: "${MYSQL_ROOT_PASSWORD}"
MYSQL_ROOT_HOST: "${MYSQL_ROOT_HOST}"
volumes:
- ./_dev/mysql:/var/lib/mysql:z
- ../../src/app/Common/database.sql:/docker-entrypoint-initdb.d/database.sql
expose:
- "3306"
networks:
default:
external:
name: ${DEV_USER}
2 changes: 1 addition & 1 deletion App/GetHot.go → src/app/App/GetHot.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
"../Common"
"app/Common"
"bytes"
"encoding/json"
"fmt"
Expand Down
13 changes: 8 additions & 5 deletions App/Server.go → src/app/App/Server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"regexp"
"text/template"

"../Common"
"../Config"
"app/Common"
"app/Config"
)

func GetTypeInfo(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -61,20 +61,23 @@ func main() {
http.HandleFunc("/GetConfig", GetConfig) // 设置访问的路由

// 静态资源
http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("../Html/css/"))))
http.Handle("/js/", http.StripPrefix("/js/", http.FileServer(http.Dir("../Html/js/"))))
http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("Html/css/"))))
http.Handle("/js/", http.StripPrefix("/js/", http.FileServer(http.Dir("Html/js/"))))

// 首页
http.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
t, err := template.ParseFiles("../Html/hot.html")
t, err := template.ParseFiles("Html/hot.html")
if err != nil {
log.Println("err")
}
t.Execute(res, nil)
})

fmt.Println("Server Start At :9090")

err := http.ListenAndServe(":9090", nil) // 设置监听的端口
if err != nil {
log.Fatal("ListenAndServe: ", err)
}

}
2 changes: 1 addition & 1 deletion Common/Db.go → src/app/Common/Db.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package Common

import (
"../Config"
"app/Config"
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
Expand Down
File renamed without changes.
4 changes: 4 additions & 0 deletions Common/database.sql → src/app/Common/database.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
-- Table structure for table `hotData2`
--

CREATE DATABASE IF NOT EXISTS `toplist` CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;

USE `toplist`;

DROP TABLE IF EXISTS `hotData2`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions src/app/Config/mysql.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Source = "root:a1b2c3d4e@tcp(mysql:3306)/toplist?charset=utf8mb4"
Driver = "mysql"
File renamed without changes.
File renamed without changes.
26 changes: 26 additions & 0 deletions src/app/Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 12 additions & 0 deletions src/app/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module app

go 1.13

require (
github.com/BurntSushi/toml v0.3.1
github.com/PuerkitoBio/goquery v1.5.0
github.com/bitly/go-simplejson v0.5.0
github.com/go-sql-driver/mysql v1.4.1
golang.org/x/text v0.3.2
google.golang.org/appengine v1.6.3 // indirect
)