unix sockets

Signed-off-by: Christine Dodrill <me@christine.website>
This commit is contained in:
Cadey Ratio 2021-04-02 08:02:10 -04:00
parent b7a4f713cd
commit e34440a796
6 changed files with 42 additions and 3 deletions

1
.envrc Normal file
View File

@ -0,0 +1 @@
eval "$(lorri direnv)"

3
go.mod
View File

@ -8,7 +8,8 @@ require (
github.com/eaburns/peggy v1.0.1
github.com/facebookgo/flagenv v0.0.0-20160425205200-fcd59fca7456
github.com/perlin-network/life v0.0.0-20191010061010-fa85cafea8ac
github.com/pkg/errors v0.9.1 // indirect
github.com/rs/cors v1.7.0
within.website/ln v0.7.0
within.website/ln v0.9.1
within.website/x v1.1.8
)

4
go.sum
View File

@ -83,6 +83,8 @@ github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2/go.mod h1:iIss55rK
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rs/cors v1.6.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik=
@ -153,5 +155,7 @@ within.website/johaus v1.1.0/go.mod h1:0RBs2TucQ3CZQiVoqNj9Wch7mtZY05uvNO14v0lKc
within.website/ln v0.6.0/go.mod h1:ifURKqsCJekcsdUE+hyCdcuhQqQ+9v9DfA++ZqYxZFE=
within.website/ln v0.7.0 h1:cZUc53cZF/+hWuEAv1VbqlYJ5czuPFHKfH0hLKmlIUA=
within.website/ln v0.7.0/go.mod h1:ifURKqsCJekcsdUE+hyCdcuhQqQ+9v9DfA++ZqYxZFE=
within.website/ln v0.9.1 h1:Qi8IjeCnU43jXijKtr5qtcbjuiCVAudOIxqTim7svnc=
within.website/ln v0.9.1/go.mod h1:I+Apk8qxMStNXTZdyDMqDqe6CB8Hn6+W/Gyf5QbY+2E=
within.website/x v1.1.8 h1:OSiR9OQo+nRf17tiZV559bgKFB7x6xfcjPj9vVcJxVo=
within.website/x v1.1.8/go.mod h1:L6lqE0SSvN7RMnVFMuKsArOEzuRkT1R2HKYJgu/un7Q=

21
http.go
View File

@ -5,7 +5,9 @@ import (
"flag"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"github.com/rs/cors"
"within.website/ln/ex"
@ -22,7 +24,24 @@ func doHTTP() error {
http.Handle("/play", doTemplate(playgroundTemplate))
http.HandleFunc("/api/playground", runPlayground)
return http.ListenAndServe(":"+*port, ex.HTTPLog(cors.Default().Handler(http.DefaultServeMux)))
srv := &http.Server{
Addr: ":" + *port,
Handler: ex.HTTPLog(cors.Default().Handler(http.DefaultServeMux)),
}
if *sockpath != "" {
os.RemoveAll(*sockpath)
l, err := net.Listen("unix", *sockpath)
if err != nil {
return fmt.Errorf("can't listen on %s: %v", *sockpath, err)
}
defer l.Close()
return srv.Serve(l)
} else {
return srv.ListenAndServe()
}
}
func httpError(w http.ResponseWriter, err error, code int) {

View File

@ -17,6 +17,7 @@ var (
outFname = flag.String("o", "", "if specified, write the webassembly binary created by -p here")
watFname = flag.String("o-wat", "", "if specified, write the uncompiled webassembly created by -p here")
port = flag.String("port", "", "HTTP port to listen on")
sockpath = flag.String("sockpath", "", "Unix domain socket to listen on")
writeTao = flag.Bool("koan", false, "if true, print the h koan and then exit")
writeVersion = flag.Bool("v", false, "if true, print the version of h and then exit")
)
@ -100,7 +101,7 @@ func main() {
return
}
if *port != "" {
if *port != "" || *sockpath != "" {
err := doHTTP()
if err != nil {
panic(err)

13
shell.nix Normal file
View File

@ -0,0 +1,13 @@
{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
buildInputs = with pkgs; [
go
goimports
gopls
wabt
# keep this line if you use bash
bashInteractive
];
}