some junk

master
Jordan Orelli 3 years ago
parent 0bbae3091a
commit a2bdee608c

@ -1,6 +1,6 @@
module orel.li/modularium module orel.li/modularium
go 1.17 go 1.18
require ( require (
golang.org/x/mod v0.5.1 // indirect golang.org/x/mod v0.5.1 // indirect

@ -1,24 +1,64 @@
package main package main
import ( import (
"context"
"embed" "embed"
"encoding/json" "encoding/json"
"fmt" "fmt"
"net"
"net/http" "net/http"
"os"
"time" "time"
"golang.org/x/mod/module" "golang.org/x/mod/module"
"golang.org/x/mod/zip" "golang.org/x/mod/zip"
"orel.li/modularium/internal/ref"
) )
//go:embed meta //go:embed meta
var content embed.FS var content embed.FS
type handler struct { type handler struct {
path ref.Ref[string]
index ref.Ref[pathArg]
}
func (h handler) run() error {
addr, err := net.ResolveUnixAddr("unix", h.path.Val())
if err != nil {
return fmt.Errorf("bad listen address: %w", err)
}
l, err := net.ListenUnix("unix", addr)
if err != nil {
return fmt.Errorf("unable to open unix socket: %w", err)
}
os.Chmod(h.path.Val(), 0777)
server := http.Server{
Handler: h,
}
onShutdown(func() error {
log_info.Print("shutting down http server")
return server.Shutdown(context.TODO())
})
// ??
start := time.Now()
err = server.Serve(l)
if err != nil {
// I dunno how to check for the right error, offhand
if time.Since(start) < time.Second {
return fmt.Errorf("unable to start server: %v", err)
}
}
log_info.Printf("http serve result: %v", err)
return nil
} }
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()) log_info.Printf("%s %s %s", r.Method, r.Host, r.URL.String())
switch r.URL.Path { switch r.URL.Path {
case "/fart": case "/fart":
@ -46,6 +86,7 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
default: default:
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
w.Write([]byte("not found"))
} }
} }

@ -0,0 +1,11 @@
{
"orel.li": {
"fart": {
"list_versions": [
"0.0.1",
"0.0.2",
"0.0.3"
]
}
}
}

@ -0,0 +1,17 @@
package ref
func New[T any](v *T) Ref[T] {
if v == nil {
var zero T
return Ref[T]{ptr: &zero}
}
return Ref[T]{ptr: v}
}
type Ref[T any] struct {
ptr *T
}
func (r Ref[T]) Val() T {
return *r.ptr
}

@ -6,11 +6,10 @@ import (
"flag" "flag"
"fmt" "fmt"
"log" "log"
"net"
"net/http"
"os" "os"
"os/signal" "os/signal"
"time"
"orel.li/modularium/internal/ref"
) )
var log_error = log.New(os.Stderr, "", 0) var log_error = log.New(os.Stderr, "", 0)
@ -28,43 +27,6 @@ func bail(status int, t string, args ...interface{}) {
//go:embed usage //go:embed usage
var usage string var usage string
var options struct {
Path string
}
func run() error {
addr, err := net.ResolveUnixAddr("unix", options.Path)
if err != nil {
return fmt.Errorf("bad listen address: %w", err)
}
l, err := net.ListenUnix("unix", addr)
if err != nil {
return fmt.Errorf("unable to open unix socket: %w", err)
}
os.Chmod(options.Path, 0777)
server := http.Server{
Handler: new(handler),
}
onShutdown(func() error {
log_info.Print("shutting down http server")
return server.Shutdown(context.TODO())
})
// ??
start := time.Now()
err = server.Serve(l)
if err != nil {
// I dunno how to check for the right error, offhand
if time.Since(start) < time.Second {
return fmt.Errorf("unable to start server: %v", err)
}
}
log_info.Printf("http serve result: %v", err)
return nil
}
func sigCancel(ctx context.Context) context.Context { func sigCancel(ctx context.Context) context.Context {
c := make(chan os.Signal, 1) c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt) signal.Notify(c, os.Interrupt)
@ -79,20 +41,29 @@ func sigCancel(ctx context.Context) context.Context {
} }
func main() { func main() {
flag.Parse() sigCancel(context.Background())
root := flag.NewFlagSet("", flag.ExitOnError)
root.Parse(os.Args[1:])
switch root.Arg(0) {
case "serve":
path := "/var/run/orel.li/http.sock"
index := pathArg{path: "./modules-index.json"}
h := handler{
path: ref.New(&path),
index: ref.New(&index),
}
ctx := context.Background() serveFlags := flag.NewFlagSet("serve", flag.ExitOnError)
ctx = sigCancel(ctx) serveFlags.StringVar(&path, "l", path, "path for a unix domain socket to listen on")
if len(os.Args) != 2 { serveFlags.Var(&index, "index", "an index config")
bail(1, usage) serveFlags.Parse(root.Args()[1:])
}
if err := run(); err != nil { if err := h.run(); err != nil {
bail(1, err.Error()) bail(1, err.Error())
} }
default:
bail(0, usage)
} }
func init() {
flag.StringVar(&options.Path, "l", "./http.sock", "path for a unix domain socket to listen on ")
} }

@ -0,0 +1,21 @@
package main
import (
"fmt"
"os"
)
type pathArg struct {
path string
}
func (p *pathArg) Set(s string) error {
_, err := os.Stat(s)
if err != nil {
return fmt.Errorf("bad path arg: %w", err)
}
p.path = s
return nil
}
func (p pathArg) String() string { return p.path }

@ -14,10 +14,9 @@ func shutdown(cause error) {
if cause != nil { if cause != nil {
status = 1 status = 1
log_error.Printf("shutting down due to error: %v", cause) log_error.Printf("shutting down due to error: %v", cause)
} else {
log_info.Print("shutting down")
} }
if len(shutdownHandlers) > 0 { if len(shutdownHandlers) > 0 {
log_info.Print("shutting down")
for i := len(shutdownHandlers) - 1; i >= 0; i-- { for i := len(shutdownHandlers) - 1; i >= 0; i-- {
f := shutdownHandlers[i] f := shutdownHandlers[i]
if err := f(); err != nil { if err := f(); err != nil {

Loading…
Cancel
Save