|
| 1 | +// Copyright (c) 2021-2025 community-scripts ORG |
| 2 | +// Author: Michel Roegl-Brunner (michelroegl-brunner) |
| 3 | +// License: MIT | https://github.com/community-scripts/ProxmoxVE/raw/main/LICENSE |
| 4 | + |
| 5 | +package main |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "encoding/json" |
| 10 | + "fmt" |
| 11 | + "log" |
| 12 | + "net/http" |
| 13 | + "os" |
| 14 | + "time" |
| 15 | + |
| 16 | + "github.com/gorilla/mux" |
| 17 | + "github.com/joho/godotenv" |
| 18 | + "github.com/rs/cors" |
| 19 | + "go.mongodb.org/mongo-driver/bson" |
| 20 | + "go.mongodb.org/mongo-driver/bson/primitive" |
| 21 | + "go.mongodb.org/mongo-driver/mongo" |
| 22 | + "go.mongodb.org/mongo-driver/mongo/options" |
| 23 | +) |
| 24 | + |
| 25 | +var client *mongo.Client |
| 26 | +var collection *mongo.Collection |
| 27 | + |
| 28 | +func loadEnv() { |
| 29 | + if err := godotenv.Load(); err != nil { |
| 30 | + log.Fatal("Error loading .env file") |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +type DataModel struct { |
| 35 | + ID primitive.ObjectID `json:"id" bson:"_id,omitempty"` |
| 36 | + CT_TYPE uint `json:"ct_type" bson:"ct_type"` |
| 37 | + DISK_SIZE float32 `json:"disk_size" bson:"disk_size"` |
| 38 | + CORE_COUNT uint `json:"core_count" bson:"core_count"` |
| 39 | + RAM_SIZE uint `json:"ram_size" bson:"ram_size"` |
| 40 | + OS_TYPE string `json:"os_type" bson:"os_type"` |
| 41 | + OS_VERSION string `json:"os_version" bson:"os_version"` |
| 42 | + DISABLEIP6 string `json:"disableip6" bson:"disableip6"` |
| 43 | + NSAPP string `json:"nsapp" bson:"nsapp"` |
| 44 | + METHOD string `json:"method" bson:"method"` |
| 45 | + CreatedAt time.Time `json:"created_at" bson:"created_at"` |
| 46 | + PVEVERSION string `json:"pve_version" bson:"pve_version"` |
| 47 | + STATUS string `json:"status" bson:"status"` |
| 48 | + RANDOM_ID string `json:"random_id" bson:"random_id"` |
| 49 | + TYPE string `json:"type" bson:"type"` |
| 50 | + ERROR string `json:"error" bson:"error"` |
| 51 | +} |
| 52 | + |
| 53 | +type StatusModel struct { |
| 54 | + RANDOM_ID string `json:"random_id" bson:"random_id"` |
| 55 | + ERROR string `json:"error" bson:"error"` |
| 56 | + STATUS string `json:"status" bson:"status"` |
| 57 | +} |
| 58 | + |
| 59 | +func ConnectDatabase() { |
| 60 | + loadEnv() |
| 61 | + |
| 62 | + mongoURI := fmt.Sprintf("mongodb://%s:%s@%s:%s", |
| 63 | + os.Getenv("MONGO_USER"), |
| 64 | + os.Getenv("MONGO_PASSWORD"), |
| 65 | + os.Getenv("MONGO_IP"), |
| 66 | + os.Getenv("MONGO_PORT")) |
| 67 | + |
| 68 | + database := os.Getenv("MONGO_DATABASE") |
| 69 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 70 | + defer cancel() |
| 71 | + |
| 72 | + var err error |
| 73 | + client, err = mongo.Connect(ctx, options.Client().ApplyURI(mongoURI)) |
| 74 | + if err != nil { |
| 75 | + log.Fatal("Failed to connect to MongoDB!", err) |
| 76 | + } |
| 77 | + collection = client.Database(database).Collection("data_models") |
| 78 | + fmt.Println("Connected to MongoDB on 10.10.10.18") |
| 79 | +} |
| 80 | + |
| 81 | +func UploadJSON(w http.ResponseWriter, r *http.Request) { |
| 82 | + var input DataModel |
| 83 | + |
| 84 | + if err := json.NewDecoder(r.Body).Decode(&input); err != nil { |
| 85 | + http.Error(w, err.Error(), http.StatusBadRequest) |
| 86 | + return |
| 87 | + } |
| 88 | + input.CreatedAt = time.Now() |
| 89 | + |
| 90 | + _, err := collection.InsertOne(context.Background(), input) |
| 91 | + if err != nil { |
| 92 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 93 | + return |
| 94 | + } |
| 95 | + |
| 96 | + log.Println("Received data:", input) |
| 97 | + w.WriteHeader(http.StatusCreated) |
| 98 | + json.NewEncoder(w).Encode(map[string]string{"message": "Data saved successfully"}) |
| 99 | +} |
| 100 | + |
| 101 | +func UpdateStatus(w http.ResponseWriter, r *http.Request) { |
| 102 | + var input StatusModel |
| 103 | + |
| 104 | + if err := json.NewDecoder(r.Body).Decode(&input); err != nil { |
| 105 | + http.Error(w, err.Error(), http.StatusBadRequest) |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + filter := bson.M{"random_id": input.RANDOM_ID} |
| 110 | + update := bson.M{"$set": bson.M{"status": input.STATUS, "error": input.ERROR}} |
| 111 | + |
| 112 | + _, err := collection.UpdateOne(context.Background(), filter, update) |
| 113 | + if err != nil { |
| 114 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 115 | + return |
| 116 | + } |
| 117 | + |
| 118 | + log.Println("Updated data:", input) |
| 119 | + w.WriteHeader(http.StatusOK) |
| 120 | + json.NewEncoder(w).Encode(map[string]string{"message": "Record updated successfully"}) |
| 121 | +} |
| 122 | + |
| 123 | +func GetDataJSON(w http.ResponseWriter, r *http.Request) { |
| 124 | + var records []DataModel |
| 125 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 126 | + defer cancel() |
| 127 | + |
| 128 | + cursor, err := collection.Find(ctx, bson.M{}) |
| 129 | + if err != nil { |
| 130 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 131 | + return |
| 132 | + } |
| 133 | + defer cursor.Close(ctx) |
| 134 | + |
| 135 | + for cursor.Next(ctx) { |
| 136 | + var record DataModel |
| 137 | + if err := cursor.Decode(&record); err != nil { |
| 138 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 139 | + return |
| 140 | + } |
| 141 | + records = append(records, record) |
| 142 | + } |
| 143 | + |
| 144 | + w.Header().Set("Content-Type", "application/json") |
| 145 | + json.NewEncoder(w).Encode(records) |
| 146 | +} |
| 147 | + |
| 148 | +func main() { |
| 149 | + ConnectDatabase() |
| 150 | + |
| 151 | + router := mux.NewRouter() |
| 152 | + router.HandleFunc("/upload", UploadJSON).Methods("POST") |
| 153 | + router.HandleFunc("/upload/updatestatus", UpdateStatus).Methods("POST") |
| 154 | + router.HandleFunc("/data/json", GetDataJSON).Methods("GET") |
| 155 | + |
| 156 | + c := cors.New(cors.Options{ |
| 157 | + AllowedOrigins: []string{"*"}, |
| 158 | + AllowedMethods: []string{"GET", "POST"}, |
| 159 | + AllowedHeaders: []string{"Content-Type", "Authorization"}, |
| 160 | + AllowCredentials: true, |
| 161 | + }) |
| 162 | + |
| 163 | + handler := c.Handler(router) |
| 164 | + |
| 165 | + fmt.Println("Server running on port 8080") |
| 166 | + log.Fatal(http.ListenAndServe(":8080", handler)) |
| 167 | +} |
0 commit comments