figured out how to serve something w00t

master
Jordan Orelli 3 years ago
parent 778a2f135a
commit daa904b591

@ -1,3 +1,8 @@
module orel.li/modularium module orel.li/modularium
go 1.17 go 1.17
require (
golang.org/x/mod v0.5.1 // indirect
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 // indirect
)

@ -0,0 +1,4 @@
golang.org/x/mod v0.5.1 h1:OJxoQ/rynoF0dcCdI7cLPktw/hR2cueqYfjm43oqK38=
golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 h1:/atklqdjdhuosWIl6AIbOeHJjicWYPqR9bpxqxYG2pA=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

@ -1,11 +1,64 @@
package main package main
import ( import (
"embed"
"encoding/json"
"fmt"
"net/http" "net/http"
"time"
"golang.org/x/mod/module"
"golang.org/x/mod/zip"
) )
type handler struct{} //go:embed meta
var content embed.FS
type handler struct {
}
func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log_info.Printf("%s %s", r.Method, r.URL.String())
switch r.URL.Path {
case "/fart":
serveFile(w, "meta/fart/root.html")
case "/modules/orel.li/fart/@v/list":
serveFile(w, "meta/fart/version-list")
case "/modules/orel.li/fart/@latest",
"/modules/orel.li/fart/@v/v0.0.3.info":
e := json.NewEncoder(w)
e.Encode(versionInfo{
Version: "v0.0.3",
Time: time.Now(),
})
case "/modules/orel.li/fart/@v/v0.0.3.mod":
serveFile(w, "meta/fart/modfile")
case "/modules/orel.li/fart/@v/v0.0.3.zip":
err := zip.CreateFromDir(w, module.Version{
Path: "orel.li/fart",
Version: "v0.0.3",
}, "/home/jorelli/modularium/modules/orel.li/fart")
if err != nil {
log_error.Printf("zip error: %v", err)
}
case "/":
w.WriteHeader(http.StatusOK)
default:
w.WriteHeader(http.StatusNotFound)
}
}
func serveFile(w http.ResponseWriter, path string) {
b, err := content.ReadFile(path)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, err.Error())
}
w.Write(b)
}
type versionInfo struct {
Version string // version string
Time time.Time // commit time
} }

@ -41,12 +41,15 @@ func run(o options) error {
if err != nil { if err != nil {
return fmt.Errorf("unable to open unix socket: %w", err) return fmt.Errorf("unable to open unix socket: %w", err)
} }
onShutdown(func() error { return l.Close() }) os.Chmod(o.Path, 0777)
server := http.Server{ server := http.Server{
Handler: new(handler), Handler: new(handler),
} }
onShutdown(func() error { return server.Shutdown(nil) }) onShutdown(func() error {
log_info.Print("shutting down http server")
return server.Shutdown(context.TODO())
})
// ?? // ??
start := time.Now() start := time.Now()

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

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

@ -0,0 +1,3 @@
0.0.1
0.0.2
0.0.3

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

@ -0,0 +1,9 @@
package main
import (
"fmt"
)
func main() {
fmt.Println("pfft")
}

@ -1,8 +1,8 @@
package main package main
import ( import (
"sync"
"os" "os"
"sync"
) )
var shutdownHandlers []func() error var shutdownHandlers []func() error
@ -17,9 +17,12 @@ func shutdown(cause error) {
} else { } else {
log_info.Print("shutting down") log_info.Print("shutting down")
} }
for _, f := range shutdownHandlers { if len(shutdownHandlers) > 0 {
if err := f(); err != nil { for i := len(shutdownHandlers) - 1; i >= 0; i-- {
log_error.Printf("error in shutdown: %v", err) f := shutdownHandlers[i]
if err := f(); err != nil {
log_error.Printf("error in shutdown: %v", err)
}
} }
} }
os.Exit(status) os.Exit(status)

Loading…
Cancel
Save