卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章35035本站已运行393

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

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)
}

卓越飞翔博客
上一篇: 如何使用atlas在go中连接mongodb?
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏