package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
)

func main() {
	http.HandleFunc("/api/data", handleData)
	fmt.Println("Mock REST API server listening on :8081")
	http.ListenAndServe(":8081", nil)
}

func handleData(w http.ResponseWriter, r *http.Request) {
	if r.Method != http.MethodPost {
		http.Error(w, "Only POST method is allowed", http.StatusMethodNotAllowed)
		return
	}

	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		http.Error(w, "Error reading request body", http.StatusInternalServerError)
		return
	}

	fmt.Printf("Received data: %s\n", string(body))
	w.WriteHeader(http.StatusOK)
	w.Write([]byte("Data received successfully"))
}
