Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

feat: use httpclient #184

Merged
merged 3 commits into from
Sep 9, 2022
Merged
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
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,6 @@ github.com/spf13/viper v1.13.0 h1:BWSJ/M+f+3nmdz9bxB+bWX28kkALN2ok11D0rSo8EJU=
github.com/spf13/viper v1.13.0/go.mod h1:Icm2xNL3/8uyh/wFuB1jI7TiTNKp8632Nwegu+zgdYw=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0 h1:M2gUjqZET1qApGOWNSnZ49BAIMX4F/1plDv3+l31EJ4=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand Down
17 changes: 13 additions & 4 deletions internal/bot/bot.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package bot

import (
"fmt"
"time"

"github.com/indes/flowerss-bot/internal/bot/handler"
"github.com/indes/flowerss-bot/internal/bot/middleware"
"github.com/indes/flowerss-bot/internal/config"
"github.com/indes/flowerss-bot/internal/util"
"github.com/indes/flowerss-bot/pkg/client"

"go.uber.org/zap"
tb "gopkg.in/telebot.v3"
Expand All @@ -26,14 +27,22 @@ func init() {
"token", config.BotToken,
"endpoint", config.TelegramEndpoint,
)
// create bot

clientOpts := []client.HttpClientOption{
client.WithTimeout(10 * time.Second),
}
if config.Socks5 != "" {
clientOpts = append(clientOpts, client.WithProxyURL(fmt.Sprintf("socks5://%s", config.Socks5)))
}
httpClient := client.NewHttpClient(clientOpts...)

var err error
B, err = tb.NewBot(
tb.Settings{
URL: config.TelegramEndpoint,
Token: config.BotToken,
Poller: &tb.LongPoller{Timeout: 10 * time.Second},
Client: util.HttpClient,
Client: httpClient.Client(),
Verbose: true,
},
)
Expand All @@ -48,7 +57,7 @@ func init() {
}
}

//Start bot
// Start bot
func Start() {
if config.RunMode == config.TestMode {
return
Expand Down
20 changes: 19 additions & 1 deletion internal/model/model.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,41 @@
package model

import (
"fmt"
"time"

"github.com/indes/flowerss-bot/internal/config"
"github.com/indes/flowerss-bot/internal/log"
"github.com/indes/flowerss-bot/pkg/client"

"github.com/jinzhu/gorm"
"go.uber.org/zap"
"moul.io/zapgorm"
)

var db *gorm.DB
var httpClient *client.HttpClient // TODO: 将网络拉取逻辑从 model 包移除

// InitDB init db object
func InitDB() {
connectDB()
configDB()
updateTable()
initHttpClient()
}

func initHttpClient() {
clientOpts := []client.HttpClientOption{
client.WithTimeout(10 * time.Second),
}
if config.Socks5 != "" {
clientOpts = append(clientOpts, client.WithProxyURL(fmt.Sprintf("socks5://%s", config.Socks5)))
}

if config.UserAgent != "" {
clientOpts = append(clientOpts, client.WithUserAgent(config.UserAgent))
}
httpClient = client.NewHttpClient(clientOpts...)
}

func configDB() {
Expand Down Expand Up @@ -66,7 +84,7 @@ func createOrUpdateTable(model interface{}) {
}
}

//EditTime timestamp
// EditTime timestamp
type EditTime struct {
CreatedAt time.Time
UpdatedAt time.Time
Expand Down
16 changes: 1 addition & 15 deletions internal/model/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"unicode"

"github.com/indes/flowerss-bot/internal/config"
"github.com/indes/flowerss-bot/internal/util"

"github.com/SlyMarbo/rss"
"github.com/jinzhu/gorm"
Expand Down Expand Up @@ -46,27 +45,14 @@ func GetSourceByUrl(url string) (*Source, error) {
}

func fetchFunc(url string) (resp *http.Response, err error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
zap.S().Fatal(err)
}

if config.UserAgent != "" {
req.Header.Set("User-Agent", config.UserAgent)
} else {
req.Header.Set("User-Agent", "flowerss/2.0")
}

resp, err = util.HttpClient.Do(req)

resp, err = httpClient.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()

var data []byte
if data, err = ioutil.ReadAll(resp.Body); err != nil {

return nil, err
}

Expand Down
32 changes: 0 additions & 32 deletions internal/util/client.go

This file was deleted.

5 changes: 0 additions & 5 deletions internal/util/util.go

This file was deleted.

3 changes: 0 additions & 3 deletions pkg/client/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ func WithProxyURL(url string) HttpClientOption {
}
}

type Http interface {
}

type HttpClient struct {
client *http.Client
userAgent string
Expand Down