filewrite

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: May 18, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package filewrite provides ResourceCache that also saves signed exchanges to files on the Store operations to the cache. The ResourceCache works as a wrapper around another ResourceCache and uses a MappingRule to locate the files to write the signed exchanges to. Here is an example:

exampleCache := filewrite.NewFileWriteCache(filewrite.Config{
	BaseCache: cache.NewOnMemoryCache(),
	ExchangeMapping: filewrite.AddBaseDir(
		filewrite.AppendExt(filewrite.UsePhysicalURLPath(), ".sxg"),
		"/tmp/sxg",
	),
})

exampleCache uses an on-memory cache for the underlying cache. It saves signed exchanges under /tmp/sxg, to the location parallel to the URL path, and with the .sxg extension. For example, the signed exchange for:

https://www.example.com/hello/world/index.html

would be saved to:

/tmp/sxg/hello/world/index.html.sxg

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewFileWriteCache

func NewFileWriteCache(config Config) cache.ResourceCache

NewFileWriteCache creates and initializes a new ResourceCache that also saves signed exchanges to files on the Store operations.

Types

type Config

type Config struct {
	// BaseCache specifies the underlying ResourceCache.
	BaseCache cache.ResourceCache

	// ExchangeMapping specifies the rule to determine the location of signed
	// exchange files. nil is equivalent to MapToDevNull.
	ExchangeMapping MappingRule

	// ValidityMapping is currently unused.
	ValidityMapping MappingRule
}

Config holds the parameters to NewFileWriteCache.

type MappingRule

type MappingRule interface {
	// Map returns the path to the target file. The returned path can be
	// empty, in which case the data is not written to any file.
	Map(r *resource.Resource) (string, error)
}

MappingRule defines the rule of mapping Resources into files. More precisely, MappingRule determines the file to store the signed exchange for each Resource.

In future, MappingRule will also be used for the validity data.

func AddBaseDir

func AddBaseDir(rule MappingRule, dir string) MappingRule

AddBaseDir returns a new MappingRule that calls rule.Map then prepends dir to the returned path.

Example
package main

import (
	"fmt"
	"log"

	"github.com/eanavitarte/webpackager/internal/urlutil"
	"github.com/eanavitarte/webpackager/resource"
	"github.com/eanavitarte/webpackager/resource/cache/filewrite"
)

func main() {
	urlParse := urlutil.MustParse // Like url.Parse, panicking on an error.

	r := resource.NewResource(urlParse("https://example.com/hello/world/"))
	r.PhysicalURL = urlParse("https://example.com/hello/world/index.html")
	r.ValidityURL = urlParse("https://example.com/hello/world/index.html.validity.1564617600")

	mapping := filewrite.AddBaseDir(filewrite.UsePhysicalURLPath(), "/tmp")

	got, err := mapping.Map(r)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(got)
}
Output:

/tmp/hello/world/index.html

func AppendExt

func AppendExt(rule MappingRule, ext string) MappingRule

AppendExt returns a new MappingRule that calls rule.Map then appends ext to the returned path. ext usually starts with a period (e.g. ".sxg") and is known as a file extension of a file suffix.

AppendExt panics if ext contains a directory separator ("/").

Example
package main

import (
	"fmt"
	"log"

	"github.com/eanavitarte/webpackager/internal/urlutil"
	"github.com/eanavitarte/webpackager/resource"
	"github.com/eanavitarte/webpackager/resource/cache/filewrite"
)

func main() {
	urlParse := urlutil.MustParse // Like url.Parse, panicking on an error.

	r := resource.NewResource(urlParse("https://example.com/hello/world/"))
	r.PhysicalURL = urlParse("https://example.com/hello/world/index.html")
	r.ValidityURL = urlParse("https://example.com/hello/world/index.html.validity.1564617600")

	mapping := filewrite.AppendExt(filewrite.UsePhysicalURLPath(), ".sxg")

	got, err := mapping.Map(r)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(got)
}
Output:

hello/world/index.html.sxg

func MapToDevNull

func MapToDevNull() MappingRule

MapToDevNull returns a MappingRule to write no files.

func StripDir

func StripDir(rule MappingRule) MappingRule

StripDir returns a new MappingRule that calls rule.Map then eliminates the directory part (anything but the last element) from the returned path.

Example
package main

import (
	"fmt"
	"log"

	"github.com/eanavitarte/webpackager/internal/urlutil"
	"github.com/eanavitarte/webpackager/resource"
	"github.com/eanavitarte/webpackager/resource/cache/filewrite"
)

func main() {
	urlParse := urlutil.MustParse // Like url.Parse, panicking on an error.

	r := resource.NewResource(urlParse("https://example.com/hello/world/"))
	r.PhysicalURL = urlParse("https://example.com/hello/world/index.html")
	r.ValidityURL = urlParse("https://example.com/hello/world/index.html.validity.1564617600")

	mapping := filewrite.StripDir(filewrite.UsePhysicalURLPath())

	got, err := mapping.Map(r)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(got)
}
Output:

index.html

func UsePhysicalURLPath

func UsePhysicalURLPath() MappingRule

UsePhysicalURLPath returns a MappingRule to use PhysicalURL.Path. The leading slash ("/") is stripped so the returned path becomes relative from the current working directory, not the root directory.

PhysicalURL.Path must be cleaned (e.g. no "." or ".." elements) and have a filename. The UsePhysicalURLPath mapping returns an error otherwise.

Example
package main

import (
	"fmt"
	"log"

	"github.com/eanavitarte/webpackager/internal/urlutil"
	"github.com/eanavitarte/webpackager/resource"
	"github.com/eanavitarte/webpackager/resource/cache/filewrite"
)

func main() {
	urlParse := urlutil.MustParse // Like url.Parse, panicking on an error.

	r := resource.NewResource(urlParse("https://example.com/hello/world/"))
	r.PhysicalURL = urlParse("https://example.com/hello/world/index.html")
	r.ValidityURL = urlParse("https://example.com/hello/world/index.html.validity.1564617600")

	mapping := filewrite.UsePhysicalURLPath()

	got, err := mapping.Map(r)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(got)
}
Output:

hello/world/index.html

func UseValidityURLPath

func UseValidityURLPath() MappingRule

UseValidityURLPath returns a MappingRule to use ValidityURL.Path. The leading slash ("/") is stripped so the returned path becomes relative from the current working directory, not the root directory.

ValidityURL.Path must be cleaned (e.g. no "." or ".." elements) and have a filename. The UseValidityURLPath mapping returns an error otherwise.

Example
package main

import (
	"fmt"
	"log"

	"github.com/eanavitarte/webpackager/internal/urlutil"
	"github.com/eanavitarte/webpackager/resource"
	"github.com/eanavitarte/webpackager/resource/cache/filewrite"
)

func main() {
	urlParse := urlutil.MustParse // Like url.Parse, panicking on an error.

	r := resource.NewResource(urlParse("https://example.com/hello/world/"))
	r.PhysicalURL = urlParse("https://example.com/hello/world/index.html")
	r.ValidityURL = urlParse("https://example.com/hello/world/index.html.validity.1564617600")

	mapping := filewrite.UseValidityURLPath()

	got, err := mapping.Map(r)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(got)
}
Output:

hello/world/index.html.validity.1564617600

Jump to

Keyboard shortcuts

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