首頁  >  文章  >  後端開發  >  GoLang 有條件地提供 .js.gz(如果存在),否則提供 .js

GoLang 有條件地提供 .js.gz(如果存在),否則提供 .js

WBOY
WBOY轉載
2024-02-09 20:36:09328瀏覽

GoLang 有条件地提供 .js.gz(如果存在),否则提供 .js

php小編蘋果在介紹GoLang時指出,GoLang有一個有趣且實用的特性,即在提供JavaScript檔案時,它會有條件地提供.js.gz檔案(如果存在),否則才會提供.js檔。這個特性可以有效地減少檔案的大小,提高網頁載入速度,為使用者帶來更好的體驗。這種智慧的文件選擇機制讓GoLang在Web開發中更有效率、更有彈性。無論是前端開發還是後端開發,GoLang都是一個值得探索的優秀程式語言。

問題內容

我的背景主要是反應/前端,但我的任務是提高我們的React Web 應用程式的效能,該應用程式使用GoLang 提供服務,並使用適用於Go 的aws sdk 從S3 中提取檔案。 我已將 Webpack 設定為執行其任務並盡可能使用其最佳化功能,包括使用其壓縮插件與部署到 S3 的捆綁包中的 .js 檔案一起建立 gzip 壓縮的 .js.gz 檔案。

我的問題是,Go 和aws sdk 中是否有一種方法,當它從s3 存儲桶中獲取文件時,首先確定該文件的gzip 壓縮形式是否存在,然後獲取該文件,如果不存在則獲取常規文件?這是解決這個問題的最佳方法嗎?我知道 Go 中有一個用於運行時壓縮的庫,但提前完成壓縮似乎更有效。

Go 伺服器部分非常小,具有獲取儲存桶檔案的功能,該功能基本上會建立一個s3 用戶端,然後使用該客戶端上的getObject 方法來取得該儲存桶的內容,然後使用http 的.write方法.ResponseWriter 包含這些內容的正文。

解決方法

是的,可以直接發送壓縮版本。

這是一個例子:

package main

import (
    "fmt"
    "net/http"
    "strings"

    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/aws/session"
    "github.com/aws/aws-sdk-go/service/s3"
)

func getFileFromS3(bucket, key string, w http.ResponseWriter, r *http.Request) error {
    sess, err := session.NewSession(&aws.Config{
        Region: aws.String("your-region"),
        // Add other necessary configurations
    })
    if err != nil {
        return err
    }

    client := s3.New(sess)

    // Check if gzipped version exists
    gzKey := key + ".gz"
    _, err = client.HeadObject(&s3.HeadObjectInput{
        Bucket: aws.String(bucket),
        Key:    aws.String(gzKey),
    })
    if err == nil {
        // Gzipped version exists, fetch and serve directly
        obj, err := client.GetObject(&s3.GetObjectInput{
            Bucket: aws.String(bucket),
            Key:    aws.String(gzKey),
        })
        if err != nil {
            return err
        }
        defer obj.Body.Close()

        // Set appropriate headers
        w.Header().Set("Content-Encoding", "gzip")
        w.Header().Set("Content-Type", "application/javascript") // Set the appropriate content type

        // Copy the gzipped content directly to the response
        _, err = fmt.Fprint(w, obj.Body)
        return err
    }

    // Gzipped version doesn't exist, fetch the regular version
    obj, err := client.GetObject(&s3.GetObjectInput{
        Bucket: aws.String(bucket),
        Key:    aws.String(key),
    })
    if err != nil {
        return err
    }
    defer obj.Body.Close()

    // Set appropriate headers
    w.Header().Set("Content-Type", "application/javascript") // Set the appropriate content type

    // Copy the regular content directly to the response
    _, err = fmt.Fprint(w, obj.Body)
    return err
}

func handler(w http.ResponseWriter, r *http.Request) {
    // Extract the file key from the request URL or any other way you have it
    fileKey := "your-file-key"

    // Set appropriate cache headers, handle CORS, etc.

    // Fetch the file from S3
    err := getFileFromS3("your-s3-bucket", fileKey, w, r)
    if err != nil {
        // Handle error, e.g., return a 404 or 500 response
        http.Error(w, "Internal Server Error", http.StatusInternalServerError)
        return
    }
}

func main() {
    http.HandleFunc("/your-endpoint", handler)
    http.ListenAndServe(":8080", nil)
}

以上是GoLang 有條件地提供 .js.gz(如果存在),否則提供 .js的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除