Write a Golang program that listens for incoming HTTP requests on a specified port and logs the request method, path, and headers to a MongoDB database. The program should use environment variables for configuring the MongoDB connection string and listening port.
PLAINFORMAT
package main

import (
	"context"
	"fmt"
	"log"
	"net/http"
	"os"
	"time"

	"github.com/gorilla/mux"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type RequestLog struct {
	Method  string
	Path    string
	Headers http.Header
	Time    time.Time
}

func main() {
	mongoURI := os.Getenv("MONGO_URI")
	if mongoURI == "" {
		log.Fatal("Missing MONGO_URI environment variable")
	}

	listenPort := os.Getenv("LISTEN_PORT")
	if listenPort == "" {
		log.Fatal("Missing LISTEN_PORT environment variable")
	}

	client, err := mongo.NewClient(options.Client().ApplyURI(mongoURI))
	if err != nil {
		log.Fatalf("Error creating MongoDB client: %v", err)
	}

	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()

	err = client.Connect(ctx)
	if err != nil {
		log.Fatalf("Error connecting to MongoDB: %v", err)
	}
	defer client.Disconnect(ctx)

	collection := client.Database("httplogs").Collection("requests")

	r := mux.NewRouter()
	r.HandleFunc("/{path:.*}", func(w http.ResponseWriter, r *http.Request) {
		requestLog := &RequestLog{
			Method:  r.Method,
			Path:    r.URL.Path,
			Headers: r.Header,
			Time:    time.Now(),
		}

		_, err := collection.InsertOne(ctx, requestLog)
		if err != nil {
			log.Printf("Error inserting log into MongoDB: %v", err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		fmt.Fprintf(w, "Logged request: %+v\n", requestLog)
	})

	log.Printf("Listening on port %s", listenPort)
	http.ListenAndServe(":"+listenPort, r)
}