http

package module
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 28, 2026 License: MIT Imports: 10 Imported by: 0

README

HTTP Source · HTTP 脚本来源

Go Version

基于 HTTP(S) 协议的远程脚本来源,支持 CRC32 校验和热更新检测

中文 · English · 日本語


概述

HTTP Source 通过 HTTP(S) 协议从 Web 服务器、CDN、REST API 或任何 HTTP 可访问资源获取脚本。通过比对响应体的 CRC32 校验和检测热更新。

特性
特性 说明
底层库 net/http(标准库)
热更新 CRC32 校验和比对轮询
自定义请求头 支持 WithHeader 设置认证等
Base URL WithBaseURL + key 拼接完整 URL
Key 前缀 支持 WithPrefix 路径前缀
接口 实现 source.ReadWatcher

安装

go get github.com/tx7do/go-scripts/source/http

配置选项

选项 默认值 说明
WithBaseURL(url) 必填 基础 URL,key 追加到其后
WithPrefix(prefix) Key 前缀(自动去除前导 /
WithTimeout(d) 30s HTTP 客户端超时
WithHeader(k, v) 自定义请求头(可多次调用)
WithHTTPClient(c) 默认 Client 注入自定义 *http.Client

快速开始

package main

import (
    "context"
    "fmt"
    httpSrc "github.com/tx7do/go-scripts/source/http"
)

func main() {
    ctx := context.Background()

    src, err := httpSrc.New(ctx,
        httpSrc.WithBaseURL("https://api.example.com/scripts/"),
        httpSrc.WithHeader("Authorization", "Bearer token123"),
        httpSrc.WithTimeout(10*time.Second),
    )
    if err != nil { panic(err) }
    defer src.Close()

    // 加载脚本(URL: https://api.example.com/scripts/hello.lua)
    code, err := src.Load(ctx, "hello.lua")
    if err != nil { panic(err) }
    fmt.Println(code)
}

测试

cd source/http && go test -v ./...

相关文档

License

MIT License

Documentation

Overview

Package http provides a source.Reader implementation that fetches scripts via HTTP(S). It is suitable for retrieving scripts from web servers, CDN endpoints, REST APIs, or any HTTP-accessible resource.

Construction:

src, err := http.New(ctx,
    http.WithBaseURL("https://api.example.com/scripts/"),
    http.WithTimeout(10*time.Second),
    http.WithHeader("Authorization", "Bearer token"),
)

Hot-reload detection compares the response body's checksum (CRC32) against the value recorded by the most recent Load.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("http source: not found")

ErrNotFound is returned (wrapped) by Load when the HTTP endpoint responds with a 404 status code. Detect with errors.Is(err, ErrNotFound) or the convenience helper IsNotFound.

Functions

func IsNotFound

func IsNotFound(err error) bool

IsNotFound reports whether err represents a 404 response. Equivalent to errors.Is(err, ErrNotFound).

Types

type Option

type Option func(*configOptions)

Option configures a Reader. Pass to New.

func WithBaseURL

func WithBaseURL(baseURL string) Option

WithBaseURL sets the base URL that keys are resolved against. The key is appended to the base URL to form the full request URL.

Example: WithBaseURL("https://api.example.com/scripts/") + key "main.lua"

-> GET https://api.example.com/scripts/main.lua

func WithHTTPClient

func WithHTTPClient(c *http.Client) Option

WithHTTPClient injects a custom *http.Client. When set, WithTimeout is ignored (the caller controls the client's timeout).

func WithHeader

func WithHeader(key, value string) Option

WithHeader adds or overrides an HTTP header sent with every request. Multiple calls accumulate headers.

func WithPrefix

func WithPrefix(prefix string) Option

WithPrefix sets a key prefix that is transparently prepended to every key before it is resolved against the base URL. Useful when all scripts share a common path segment.

Leading slashes are stripped; no trailing slash normalization is applied (the base URL should handle that).

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout sets the HTTP client timeout (default 30s).

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

Reader fetches scripts via HTTP.

All exported methods are safe for concurrent use. Reader implements the source.ReadWatcher interface.

func New

func New(_ context.Context, opts ...Option) (*Reader, error)

New creates an HTTP-backed Reader. WithBaseURL is required; all other settings are optional.

func (*Reader) Close

func (r *Reader) Close() error

Close releases the underlying HTTP client. The default http.Client has no resources to release, so this is a no-op; the method exists to satisfy the source.Reader interface.

func (*Reader) Load

func (r *Reader) Load(ctx context.Context, key string) (string, error)

Load fetches the URL via HTTP GET and returns the response body as a string. Context cancellation propagates to the underlying request.

A 404 response is reported as a wrapped ErrNotFound. Non-2xx responses (other than 404) are reported as errors.

func (*Reader) Watch

func (r *Reader) Watch(ctx context.Context, key string) (<-chan struct{}, error)

Watch returns a channel that signals when the content at the URL identified by `key` changes. It polls the URL every 5 seconds, computes the CRC32 checksum of the response body, and sends a signal when the checksum differs from the one recorded during the last Load.

The returned channel is closed when the context is cancelled. Callers should re-Load the script after receiving from the channel.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL