upspinfsys

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2023 License: BSD-3-Clause Imports: 9 Imported by: 0

README

upspinfsys

fs.FS implementation for Upspin.

Go Reference

package main

import (
    "log"
    "net/http"
    
    "github.com/rschio/upspinfsys"
    "upspin.io/client"
    "upspin.io/config"
    "upspin.io/transports"
)

func main() {
    cfg, err := config.FromFile("config")
    if err != nil {
        log.Fatal(err)
    }
    transports.Init(cfg)
    c := client.New(cfg)
    
    fsys := upspinfsys.UpspinFS(c)
    http.Handle("/", http.FileServer(http.FS(fsys)))
    http.ListenAndServe(":8080", nil)
}

Documentation

Overview

Package upspinfsys provides an implementation of fs.FS interface for Upspin. It implements the fs.FS interface and the necessary methods to serve it using the http.FileServer too.

Limitations:

  • The FileMode does not represent the Access file correctly.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func UpspinFS

func UpspinFS(c upspin.Client) fs.FS

UpspinFS returns a fs.FS implementation. To use the Open function is necessary to pass the full path of the file (the file system is not rooted at client's home).

Example
fsys := upspinfsys.UpspinFS(c)
home := string(cfg.UserName())
path := filepath.Join(home, "documents", "doc1.txt")

f, err := fsys.Open(path)
if err != nil {
	log.Fatal(err)
}
data, err := io.ReadAll(f)
if err != nil {
	log.Fatal(err)
}

fmt.Printf("%s\n", data)
Output:

doc1
Example (HttpServer)
fsys := upspinfsys.UpspinFS(c)
home := string(cfg.UserName())
path := filepath.Join(home, "documents", "doc2.txt")

go func() {
	http.Handle("/", http.FileServer(http.FS(fsys)))
	http.ListenAndServe(":8080", nil)
}()

resp, err := http.Get("http://localhost:8080/" + path)
if err != nil {
	log.Fatal(err)
}
defer resp.Body.Close()

data, err := io.ReadAll(resp.Body)
if err != nil {
	log.Fatal(err)
}

fmt.Printf("%s\n", data)
Output:

doc2

Types

This section is empty.

Jump to

Keyboard shortcuts

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