diff options
author | Lizzy Hunt <elizabeth.hunt@simponic.xyz> | 2024-03-09 21:29:25 -0700 |
---|---|---|
committer | Lizzy Hunt <elizabeth.hunt@simponic.xyz> | 2024-03-09 21:29:25 -0700 |
commit | 67e4b234d31626976ad630043814235c936f8bbf (patch) | |
tree | fcf4259005094271ab475d4f55cea1c251357817 /html/fruitvote/main.go | |
parent | 2c4b0cf6c40d7b866a9c538a4df06bc36e189d89 (diff) | |
download | tilde.club-67e4b234d31626976ad630043814235c936f8bbf.tar.gz tilde.club-67e4b234d31626976ad630043814235c936f8bbf.zip |
finish static page
Diffstat (limited to 'html/fruitvote/main.go')
-rw-r--r-- | html/fruitvote/main.go | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/html/fruitvote/main.go b/html/fruitvote/main.go new file mode 100644 index 0000000..73c1c17 --- /dev/null +++ b/html/fruitvote/main.go @@ -0,0 +1,57 @@ +package main + +import ( + "flag" + "fmt" + "net" + "net/http" + "os" + "os/exec" + "strings" +) + +func indexHandler(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + w.Write([]byte("Hello, this is a Unix socket HTTP server in Go!")) +} + +func main() { + socketPath, users := getArgs() + os.Remove(socketPath) + + listener, err := net.Listen("unix", socketPath) + if err != nil { + panic(err) + } + os.Chmod(socketPath, 0700) + defer listener.Close() + + for _, user := range strings.Split(users, ",") { + setACL(socketPath, user) + } + + mux := http.NewServeMux() + mux.HandleFunc("/", indexHandler) + + http.Serve(listener, mux) +} + +func setACL(socketPath, user string) { + cmd := exec.Command("setfacl", "-m", "u:"+user+":rwx", socketPath) + if err := cmd.Run(); err != nil { + panic("failed to set ACL: " + err.Error()) + } +} + +func getArgs() (string, string) { + socketPath := flag.String("socket-path", "/tmp/go-server.sock", "Path to the Unix socket") + users := flag.String("users", "", "Comma-separated list of users for ACL") + flag.Parse() + + if *users == "" { + fmt.Println("You must specify at least one user with --users") + os.Exit(1) + } + + return *socketPath, *users +} |