Skip to Content

HTTP to Upspin gateway

Posted on 2 mins read

First time I heard about Upspin from this video

and I very like it idea. Who don’t know what it is about, can check the site https://upspin.io/ From the documentation:

Upspin provides a global name space to name all your files. Given an Upspin name, a file can be shared securely, copied efficiently without “download” and “upload”, and accessed from anywhere that has a network connection.

and recently I deployed upspin server on my GCP VM

but no one of my friends uses upspin yet and I want to have ability to share some of my public files I decided to write simple HTTP to Upspin gateway.

and it turned out to be quite simple:

I just need to register a separate user in keyserver, you can find information here

put user keys and config to the server and write this simple server:

package main

import (
	"bytes"
	"io/ioutil"
	"log"
	"net/http"

	"upspin.io/client"
	"upspin.io/config"
	"upspin.io/transports"
	"upspin.io/upspin"
)

var c upspin.Client

func main() {
	data, err := ioutil.ReadFile("/home/user/upspin/config")
	if err != nil {
		log.Fatal(err)
	}
	cfg, err := config.InitConfig(bytes.NewReader(data))
	if err != nil {
		log.Fatal(err)
	}

	transports.Init(cfg)
	c = client.New(cfg)
	http.HandleFunc("/", handler)
	log.Fatal(http.ListenAndServe(":80", nil))
}

func handler(w http.ResponseWriter, r *http.Request) {
	path := r.URL.Path[1:]
	if path == "favicon.ico" {
		return
	}
	b, err := c.Get(upspin.PathName(path))
	if err != nil {
		log.Print(err)
	}
	w.Write(b)
}

and in pieces:

so, here we just read config and initialize upspin config struct

	data, err := ioutil.ReadFile("/home/user/upspin/config")
	if err != nil {
		log.Fatal(err)
	}
	cfg, err := config.InitConfig(bytes.NewReader(data))
	if err != nil {
		log.Fatal(err)
	}

then we need to initialize transports and create new upspin client

	transports.Init(cfg)
	c = client.New(cfg)

and then in the handler, we just call Get method with path that we get from the URL

	b, err := c.Get(upspin.PathName(path))

now, if you run this server and append upspin path to the URL you will get your file from upspin(if it’s publicly available, read about access control)

My server deploy on the domain up.aborilov.ru, you can try on it, for example: http://up.aborilov.ru/augie@upspin.io/Images/Augie/smaller.jpg

upspin mascot

comments powered by Disqus