serve zip files

master
Jordan Orelli 3 years ago
parent 4462b61a08
commit f1ee16b319

@ -2,10 +2,10 @@ package main
import (
"context"
"embed"
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
"net"
"net/http"
@ -18,9 +18,6 @@ import (
"golang.org/x/mod/semver"
)
//go:embed meta
var content embed.FS
// this is pretty janky, but I didn't want to import a routing library
var (
latestP = regexp.MustCompile(`^/dl/(.+)/@latest$`)
@ -116,6 +113,7 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
// $base/$module/@v/$version.info - get info about a specific version
if matches := infoP.FindStringSubmatch(r.URL.Path); matches != nil {
modpath := matches[1]
modversion := matches[2]
@ -130,12 +128,13 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// return
// }
// if matches := zipP.FindStringSubmatch(r.URL.Path); matches != nil {
// modpath := matches[1]
// modversion := matches[2]
// h.zipfile(modpath, modversion, w, r)
// return
// }
// $base/$module/@v/$version.zip - get the zip bundle of a package version
if matches := zipP.FindStringSubmatch(r.URL.Path); matches != nil {
modpath := matches[1]
modversion := matches[2]
h.zipfile(modpath, modversion, w, r)
return
}
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("not found"))
@ -306,21 +305,30 @@ func (h handler) info(modpath, modversion string, w http.ResponseWriter, r *http
})
}
func (h handler) openZip(modpath, version string) (io.ReadCloser, error) {
dirname, basename := filepath.Split(modpath)
absdir := filepath.Join(h.root, "modules", dirname)
fname := filepath.Join(absdir, fmt.Sprintf("%s@%s.zip", basename, version))
return os.Open(fname)
}
// modfile serves the $base/$module/@v/$version.mod endpoint
func (h handler) modfile(modpath, modversion string, w http.ResponseWriter, r *http.Request) {
}
// zipfile serves the $base/$module/@v/$version.zip endpoint
func (h handler) zipfile(modpath, modversion string, w http.ResponseWriter, r *http.Request) {
}
func serveFile(w http.ResponseWriter, path string) {
b, err := content.ReadFile(path)
zf, err := h.openZip(modpath, modversion)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, err.Error())
writeError(w, err)
return
}
defer zf.Close()
w.Header().Add("Content-Type", "application/zip")
if _, err := io.Copy(w, zf); err != nil {
log_error.Printf("error writing zip for module %s version %s: %v", modpath, modversion, err)
}
w.Write(b)
}
type versionInfo struct {

@ -1,3 +0,0 @@
module orel.li/fart
go 1.17

@ -1,7 +0,0 @@
<html>
<head>
<meta name="go-import" content="orel.li/fart mod https://orel.li/modules">
</head>
<body>
<body>
</html>

@ -1,3 +0,0 @@
0.0.1
0.0.2
0.0.3
Loading…
Cancel
Save