/ random.go
random.go
1 package main 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "math/rand" 7 "net/http" 8 ) 9 10 type Object struct { 11 Name string 12 Count int 13 } 14 15 func generateRandomObjects() []Object { 16 objects := make([]Object, 10) 17 for i := range objects { 18 objects[i].Name = fmt.Sprintf("Name%d", i) 19 objects[i].Count = rand.Intn(100) 20 } 21 return objects 22 } 23 24 func main() { 25 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 26 fmt.Println("Received request") 27 objects := generateRandomObjects() 28 jsonData, err := json.Marshal(objects) 29 if err != nil { 30 http.Error(w, "Error generating JSON", http.StatusInternalServerError) 31 return 32 } 33 w.Header().Set("Content-Type", "application/json") 34 w.Header().Set("Access-Control-Allow-Origin", "*") 35 w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS") 36 w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Accept, Authorization, Origin") 37 w.Write(jsonData) 38 }) 39 40 fmt.Println("Server running on port 9292") 41 http.ListenAndServe(":9292", nil) 42 }