From 67e4b234d31626976ad630043814235c936f8bbf Mon Sep 17 00:00:00 2001 From: Lizzy Hunt Date: Sat, 9 Mar 2024 21:29:25 -0700 Subject: finish static page --- html/fruitvote/main.go | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 html/fruitvote/main.go (limited to 'html/fruitvote/main.go') 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 +} -- cgit v1.2.3-70-g09d2