Documentation
¶
Index ¶
- Constants
- func Decode(buf []byte, h http.Header) ([]byte, error)
- func DirFS(dir string) fs.FS
- func EncodeContent(h http.Header, content []byte) (buff []byte, encoding string, err error)
- func FileName(uri any) string
- func Mount(fs embed.FS)
- func ParseMap(buf []byte) (map[string]string, error)
- func ReadAll(body io.Reader, h http.Header) ([]byte, error)
- func ReadFile(uri any) ([]byte, error)
- func ReadFileWithEncoding(uri string, h http.Header) ([]byte, error)
- func ReadMap(uri any) (map[string]string, error)
- func ReadMapT[T any](uri any) (t T, err error)
- func ValidateUri(uri string) error
- func WriteMap(m map[string]string) string
- func ZipFile(uri string) error
- type EncodingReader
- type EncodingWriter
Examples ¶
Constants ¶
View Source
const ( AcceptEncoding = "Accept-Encoding" AcceptEncodingValue = "gzip, deflate, br" ContentEncoding = "Content-Encoding" GzipEncoding = "gzip" BrotliEncoding = "br" DeflateEncoding = "deflate" CompressEncoding = "compress" NoneEncoding = "none" ApplicationGzip = "application/x-gzip" ApplicationBrotli = "application/x-br" ApplicationDeflate = "application/x-deflate" )
View Source
const (
CwdVariable = "[cwd]"
)
Variables ¶
This section is empty.
Functions ¶
func DirFS ¶
Example ¶
dir := "file:///c:/Users/markb/GitHub/core/iox/ioxtest"
fileSystem := DirFS(dir)
fs.WalkDir(fileSystem, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
log.Fatal(err)
}
if path != "." {
buf, err1 := fs.ReadFile(fileSystem, path)
fmt.Printf("test: fs.ReadFile() -> [err:%v] [%v] %v\n", err1, path, buf != nil)
}
return nil
})
Output: test: fs.ReadFile() -> [err:<nil>] [address1.json] true test: fs.ReadFile() -> [err:<nil>] [address2.json] true test: fs.ReadFile() -> [err:<nil>] [address3.json] true test: fs.ReadFile() -> [err:<nil>] [hello-world.gz] true test: fs.ReadFile() -> [err:<nil>] [hello-world.txt] true test: fs.ReadFile() -> [err:<nil>] [status-504.json] true test: fs.ReadFile() -> [err:<nil>] [test-response.gz] true test: fs.ReadFile() -> [err:<nil>] [test-response.txt] true test: fs.ReadFile() -> [err:<nil>] [test-response2.gz] true test: fs.ReadFile() -> [err:<nil>] [test-response2.txt] true
func EncodeContent ¶
Example ¶
package main
import (
"encoding/json"
"fmt"
"net/http"
)
type echo struct {
Method string `json:"method"`
Host string `json:"host"`
Url string `json:"url"`
Header http.Header `json:"header"`
}
func main() {
h := make(http.Header)
h.Add("x-request-id", "1234-56-7890")
e := echo{
Method: http.MethodGet,
Host: "localhost",
Url: "https://www.google.com/search?q=golang",
Header: h,
}
content, err1 := json.Marshal(e)
if err1 != nil {
fmt.Printf("test: json.Marshal() -> [err:%v]\n", err1)
}
//buf, encoding, err := EncodeContent(nil, content)
//fmt.Printf("test: EncodeContent-Nil-Header() -> [buf:%v] [encoding:%v] [err:%v]\n", len(buf), encoding, err)
h2 := make(http.Header)
buf, encoding, err := EncodeContent(h2, content)
fmt.Printf("test: EncodeContent-No-Accept-Encoding() -> [buf:%v] [encoding:%v] [err:%v]\n", len(buf), encoding, err)
h2.Add(AcceptEncoding, AcceptEncodingValue)
buf, encoding, err = EncodeContent(h2, content)
ct := http.DetectContentType(buf)
fmt.Printf("test: EncodeContent() -> [buf:%v] [encoding:%v] [content-type:%v] [err:%v]\n", len(buf), encoding, ct, err)
}
Output: test: EncodeContent-No-Accept-Encoding() -> [buf:0] [encoding:] [err:<nil>] test: EncodeContent() -> [buf:144] [encoding:gzip] [content-type:application/x-gzip] [err:<nil>]
func ReadFile ¶
ReadFile - read a file with a Status
Example ¶
s := status504
buf, status := ReadFile(s)
fmt.Printf("test: ReadFile(%v) -> [type:%v] [buf:%v] [status:%v]\n", s, reflect.TypeOf(s), len(buf), status)
s = address1Url
buf, status = ReadFile(s)
fmt.Printf("test: ReadFile(%v) -> [type:%v] [buf:%v] [status:%v]\n", s, reflect.TypeOf(s), len(buf), status)
s = status504
u := parseRaw(s)
buf, status = ReadFile(u.String())
fmt.Printf("test: ReadFile(%v) -> [type:%v] [buf:%v] [status:%v]\n", s, reflect.TypeOf(u), len(buf), status)
s = address1Url
u = parseRaw(s)
buf, status = ReadFile(u.String())
fmt.Printf("test: ReadFile(%v) -> [type:%v] [buf:%v] [status:%v]\n", s, reflect.TypeOf(u), len(buf), status)
Output: test: ReadFile(file://[cwd]/ioxtest/status-504.json) -> [type:string] [buf:82] [status:<nil>] test: ReadFile(file://[cwd]/ioxtest/address1.json) -> [type:string] [buf:68] [status:<nil>] test: ReadFile(file://[cwd]/ioxtest/status-504.json) -> [type:*url.URL] [buf:82] [status:<nil>] test: ReadFile(file://[cwd]/ioxtest/address1.json) -> [type:*url.URL] [buf:68] [status:<nil>]
func ReadFileWithEncoding ¶
ReadFileWithEncoding - read a file with a possible encoding and a Status
Example ¶
buf, status := ReadFileWithEncoding(helloWorldGzip, nil)
fmt.Printf("test: ReadFileWithEncoding(\"%v\",nil) -> [buf:%v] [status:%v]\n", helloWorldGzip, string(buf), status)
h := make(http.Header)
h.Set(ContentEncoding, GzipEncoding)
buf, status = ReadFileWithEncoding(helloWorldGzip, h)
fmt.Printf("test: ReadFileWithEncoding(\"%v\",h) -> [buf:%v] [status:%v]\n", helloWorldGzip, string(buf), status)
buf, status = ReadFileWithEncoding(helloWorldTxt, nil)
fmt.Printf("test: ReadFileWithEncoding(\"%v\",nil) -> [buf:%v] [status:%v]\n", helloWorldTxt, string(buf), status)
Output: test: ReadFileWithEncoding("file://[cwd]/ioxtest/hello-world.gz",nil) -> [buf:Hello World!!] [status:<nil>] test: ReadFileWithEncoding("file://[cwd]/ioxtest/hello-world.gz",h) -> [buf:Hello World!!] [status:<nil>] test: ReadFileWithEncoding("file://[cwd]/ioxtest/hello-world.txt",nil) -> [buf:Hello World!!] [status:<nil>]
func ReadMap ¶
Example ¶
m, err := ReadMap(serviceConfigTxt)
fmt.Printf("test: ReadMap() -> [err:%v] %v\n", err, m)
Output: test: ReadMap() -> [err:<nil>] map[ORIGIN-INSTANCE-ID:instance-id ORIGIN-REGION:region ORIGIN-SERVICE:service ORIGIN-SUB-ZONE:sub-zone ORIGIN-ZONE:zone RUNTIME_ENV_KEY:RT_ENV egress-listener-addr:0.0.0.0:8087 ingress-listener-addr:0.0.0.0:8086]
func ValidateUri ¶
func WriteMap ¶
Example ¶
m := make(map[string]string)
m["ORIGIN-INSTANCE-ID"] = "instance-id"
m["ORIGIN-REGION"] = "region"
m["ORIGIN-SERVICE"] = "service"
m["ORIGIN-SUB-ZONE"] = "sub-zone"
m["ORIGIN-ZONE"] = "zone"
fmt.Printf("test: WriteMap() -> %v\n", WriteMap(m))
Output: test: WriteMap() -> ORIGIN-SUB-ZONE : sub-zone ORIGIN-ZONE : zone ORIGIN-INSTANCE-ID : instance-id ORIGIN-REGION : region ORIGIN-SERVICE : service
Types ¶
type EncodingReader ¶
type EncodingReader interface {
io.ReadCloser
}
func NewEncodingReader ¶
func NewGzipReader ¶
func NewGzipReader(r io.Reader) (EncodingReader, error)
func NewIdentityReader ¶
func NewIdentityReader(r io.Reader) EncodingReader
NewIdentityReader - The default (identity) encoding; the use of no transformation whatsoever
type EncodingWriter ¶
type EncodingWriter interface {
io.WriteCloser
ContentEncoding() string
}
func NewEncodingWriter ¶
func NewGzipWriter ¶
func NewGzipWriter(w io.Writer) EncodingWriter
func NewIdentityWriter ¶
func NewIdentityWriter(w io.Writer) EncodingWriter
NewIdentityWriter - The default (identity) encoding; the use of no transformation whatsoever
Click to show internal directories.
Click to hide internal directories.