emptygif
Ngôn ngữ: Tiếng Việt

Tất cả ngôn ngữ và framework

Huma Go

Đoạn mã Go cho Huma, trả lời các yêu cầu tới /pixel.gif bằng GIF rỗng, được giải mã một lần khi khởi động.

Huma Go

Cài đặt
go get github.com/danielgtaylor/huma/v2
Tệp
main.go
Chạy
go run .
package main

import (
	"context"
	"encoding/base64"
	"log"
	"net/http"

	"github.com/danielgtaylor/huma/v2"
	"github.com/danielgtaylor/huma/v2/adapters/humago"
)

var gif, _ = base64.StdEncoding.DecodeString("R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==")

// A []byte Body is sent as-is; tagged fields become response headers.
type PixelOutput struct {
	ContentType  string `header:"Content-Type"`
	CacheControl string `header:"Cache-Control"`
	Body         []byte
}

func main() {
	mux := http.NewServeMux()
	api := humago.New(mux, huma.DefaultConfig("Pixel API", "1.0.0"))
	huma.Get(api, "/pixel.gif", func(ctx context.Context, _ *struct{}) (*PixelOutput, error) {
		return &PixelOutput{ContentType: "image/gif", CacheControl: "no-store", Body: gif}, nil
	})
	log.Fatal(http.ListenAndServe(":8080", mux))
}

Chúng tôi đã chạy đoạn mã này và so sánh các byte nó trả về.

Kiểm tra

curl -sI http://localhost:8080/pixel.gif

Trang chính thức: Huma

Thêm với Go