emptygif
언어: 한국어

모든 언어 및 프레임워크

Huma Go

/pixel.gif 요청에 빈 GIF로 응답하는 Huma용 Go 스니펫입니다. GIF는 시작할 때 한 번만 디코딩합니다.

Huma Go

설치
go get github.com/danielgtaylor/huma/v2
파일
main.go
실행
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))
}

이 스니펫은 실행해 보고, 반환된 바이트를 비교했습니다.

확인

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

공식 사이트: Huma

Go의 다른 예제