nginx-cache-purge/serverCmd.go

76 lines
1.7 KiB
Go
Raw Normal View History

2024-08-01 02:09:30 -05:00
package main
import (
"fmt"
"io"
2024-08-01 09:01:40 -05:00
"log"
2024-08-01 02:09:30 -05:00
"net"
"net/http"
"os"
)
// The server command for the CLI to run the HTTP server.
type ServerCmd struct {
Socket string `help:"Socket path for HTTP communication." type:"path"`
}
// Handle request.
func (a *ServerCmd) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// Parse query parameters.
query := req.URL.Query()
cachePath := query.Get("path")
if cachePath == "" {
io.WriteString(w, "Need path parameter.")
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
key := query.Get("key")
if key == "" {
io.WriteString(w, "Need key parameter.")
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
excludes := query["exclude"]
// Purge cache.
err := app.PurgeCache(cachePath, key, excludes)
// If error, return error.
if err != nil {
fmt.Println("Error purging cache:", err)
io.WriteString(w, "Error occurred while processing purge.")
http.Error(w, http.StatusText(http.StatusBadGateway), http.StatusBadGateway)
return
}
// Successful purge.
w.Write([]byte("PURGED"))
}
// Start the FastCGI server.
func (a *ServerCmd) Run() error {
// Determine UNIX socket path.
unixSocket := a.Socket
if unixSocket == "" {
unixSocket = "/run/nginx-cache-purge/http.sock"
2024-08-01 02:09:30 -05:00
}
// If socket exists, remove it.
if _, err := os.Stat(unixSocket); !os.IsNotExist(err) {
os.Remove(unixSocket)
}
// Open the socket for FCGI communication.
listener, err := net.Listen("unix", unixSocket)
if err != nil {
return err
}
defer listener.Close()
// Start the FastCGI server.
2024-08-01 09:01:40 -05:00
log.Println("Starting server at", unixSocket)
2024-08-01 02:09:30 -05:00
http.HandleFunc("/", a.ServeHTTP)
err = http.Serve(listener, nil)
return err
}