Skip to content

Commit

Permalink
httpClientを共通のインスタンスとして使えるようにした
Browse files Browse the repository at this point in the history
  • Loading branch information
studiokaiji committed Oct 25, 2023
1 parent a75c31e commit 158609c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
10 changes: 4 additions & 6 deletions hostr/cmd/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ func convertLinks(priKey, pubKey, basePath string, replaceable bool, indexHtmlId
// jsファイルを解析する
if strings.HasSuffix(basePath, ".js") {
jsContent := string(bytesContent)

}

// Tagsを追加
Expand Down Expand Up @@ -179,7 +178,7 @@ func convertLinks(priKey, pubKey, basePath string, replaceable bool, indexHtmlId
} else if slices.Contains(availableMediaHtmlTags, n.Data) {
// 内部mediaファイルを対象にUpload Requestを作成
for i, a := range n.Attr {
if (a.Key == "href" || a.Key == "src" || a.Key == "data") && !isExternalURL(a.Val) && isValidBasicFileType(a.Val) {
if (a.Key == "href" || a.Key == "src" || a.Key == "data") && !isExternalURL(a.Val) && isValidMediaFileType(a.Val) {
filePath := filepath.Join(basePath, a.Val)

// アップロードのためのHTTPリクエストを取得
Expand All @@ -189,12 +188,11 @@ func convertLinks(priKey, pubKey, basePath string, replaceable bool, indexHtmlId
}

// アップロード処理を代入
uploadFunc := func() (*MediaResult, error) {
// リクエストを送信
client := &http.Client{}
uploadFunc := func(client *http.Client) (*MediaResult, error) {
response, err := client.Do(request)
// リクエストを送信
if err != nil {
fmt.Errorf("Error sending request: %w", err)
return nil, fmt.Errorf("Error sending request: %w", err)
}
defer response.Body.Close()

Expand Down
10 changes: 6 additions & 4 deletions hostr/cmd/deploy/media.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ type MediaResult struct {
tags []string
}

var mediaUploadRequestQueue []func() (*MediaResult, error)
var mediaUploadRequestQueue []func(*http.Client) (*MediaResult, error)

func addNostrEventQueue(event *nostr.Event) {
nostrEventsQueue = append(nostrEventsQueue, event)
}

func addMediaUploadRequestFuncQueue(reqFunc func() (*MediaResult, error)) {
func addMediaUploadRequestFuncQueue(reqFunc func(client *http.Client) (*MediaResult, error)) {
mediaUploadRequestQueue = append(mediaUploadRequestQueue, reqFunc)
}

Expand All @@ -106,11 +106,13 @@ func uploadMediaFilesFromQueue() {

var mutex sync.Mutex

client := &http.Client{}

// アップロードを並列処理
for _, reqFunc := range mediaUploadRequestQueue {
wg.Add(1)
go func(reqFun func() (*MediaResult, error)) {
_, err := reqFun()
go func(reqFun func(*http.Client) (*MediaResult, error)) {
_, err := reqFun(client)
if err != nil {
fmt.Println(err)
return
Expand Down

0 comments on commit 158609c

Please sign in to comment.