serve zip files

master
Jordan Orelli 3 years ago
parent 4462b61a08
commit f1ee16b319

@ -2,10 +2,10 @@ package main
import ( import (
"context" "context"
"embed"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io"
"io/fs" "io/fs"
"net" "net"
"net/http" "net/http"
@ -18,9 +18,6 @@ import (
"golang.org/x/mod/semver" "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 // this is pretty janky, but I didn't want to import a routing library
var ( var (
latestP = regexp.MustCompile(`^/dl/(.+)/@latest$`) latestP = regexp.MustCompile(`^/dl/(.+)/@latest$`)
@ -116,6 +113,7 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return return
} }
// $base/$module/@v/$version.info - get info about a specific version
if matches := infoP.FindStringSubmatch(r.URL.Path); matches != nil { if matches := infoP.FindStringSubmatch(r.URL.Path); matches != nil {
modpath := matches[1] modpath := matches[1]
modversion := matches[2] modversion := matches[2]
@ -130,12 +128,13 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// return // return
// } // }
// if matches := zipP.FindStringSubmatch(r.URL.Path); matches != nil { // $base/$module/@v/$version.zip - get the zip bundle of a package version
// modpath := matches[1] if matches := zipP.FindStringSubmatch(r.URL.Path); matches != nil {
// modversion := matches[2] modpath := matches[1]
// h.zipfile(modpath, modversion, w, r) modversion := matches[2]
// return h.zipfile(modpath, modversion, w, r)
// } return
}
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
w.Write([]byte("not found")) 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 // modfile serves the $base/$module/@v/$version.mod endpoint
func (h handler) modfile(modpath, modversion string, w http.ResponseWriter, r *http.Request) { func (h handler) modfile(modpath, modversion string, w http.ResponseWriter, r *http.Request) {
} }
// zipfile serves the $base/$module/@v/$version.zip endpoint // zipfile serves the $base/$module/@v/$version.zip endpoint
func (h handler) zipfile(modpath, modversion string, w http.ResponseWriter, r *http.Request) { func (h handler) zipfile(modpath, modversion string, w http.ResponseWriter, r *http.Request) {
} zf, err := h.openZip(modpath, modversion)
func serveFile(w http.ResponseWriter, path string) {
b, err := content.ReadFile(path)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError) writeError(w, err)
fmt.Fprintf(w, err.Error()) 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 { 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