func main() { var historyResult HistoryResult var waitGroup sync.WaitGroup startTime := time.Now() // Load config cfg, err := config.GetConfigData() if err != nil { log.Fatal(err) } // Connect to DB db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s", cfg.DB.User, cfg.DB.Password, cfg.DB.Host, cfg.DB.Database)) if err != nil { log.Fatal(err) } defer db.Close() // Request server-list from lists.sa-mp.com log.Println("Download server-list from lists.sa-mp.com.") var serversAddr []string var currentAddr int request := serverlist.NewRequest(cfg.Updater.Version) if err := request.Exec(); err != nil { log.Fatal("Error (request.Exec()):", err) } serversAddr, err = request.ReadAll() if err != nil { log.Fatal("Error (request.ReadAll()):", err) } // Clear database table if err := clearServersInDB(db); err != nil { log.Fatal("Error (clearServersInDB()):", err) } // Create threads waitGroup.Add(maxQueryThreads) for i := 0; i < maxQueryThreads; i++ { go processLine(&serversAddr, ¤tAddr, &historyResult, db, &waitGroup) } // Wait threads waitGroup.Wait() // Write history to database writeHistoryToDB(db, &historyResult) // Print results log.Println("Execution time:", time.Since(startTime)) log.Println("Servers offline:", historyResult.ServersOnline, "of", historyResult.ServersTotal) log.Println("Slots used:", historyResult.SlotsUsed, "of", historyResult.SlotsTotal) }
func main() { // Load config cfg, err := config.GetConfigData() if err != nil { log.Fatal(err) } // Initialize database db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s", cfg.DB.User, cfg.DB.Password, cfg.DB.Host, cfg.DB.Database)) if err != nil { log.Fatal(err) } defer db.Close() // Initialize HTTP-server router := mux.NewRouter() router.HandleFunc("/api/general", func(w http.ResponseWriter, r *http.Request) { data, err := GetGeneralData(db) if err != nil { response.JSONResponseError(&w, http.StatusInternalServerError, err.Error()) return } response.JSONResponse(&w, http.StatusOK, data) }) router.HandleFunc("/api/history/servers/{range}", func(w http.ResponseWriter, r *http.Request) { var days int vars := mux.Vars(r) requestRange := vars["range"] switch requestRange { case "day": days = 1 case "week": days = 7 case "month": days = 30 case "year": days = 365 default: response.JSONResponseError(&w, http.StatusInternalServerError, "Incorrect range") return } data, err := GetHistoryServers(db, days) if err != nil { response.JSONResponseError(&w, http.StatusInternalServerError, err.Error()) return } response.JSONResponse(&w, http.StatusOK, data) }) router.HandleFunc("/api/history/players/{range}", func(w http.ResponseWriter, r *http.Request) { var days int vars := mux.Vars(r) requestRange := vars["range"] switch requestRange { case "day": days = 1 case "week": days = 7 case "month": days = 30 case "year": days = 365 default: response.JSONResponseError(&w, http.StatusInternalServerError, "Incorrect range") return } data, err := GetHistoryPlayers(db, days) if err != nil { response.JSONResponseError(&w, http.StatusInternalServerError, err.Error()) return } response.JSONResponse(&w, http.StatusOK, data) }) router.PathPrefix("/").Handler(http.FileServer(http.Dir("./public"))) http.Handle("/", router) log.Println(fmt.Sprintf("Listening %s...", cfg.Main.Listen)) if err := http.ListenAndServe(cfg.Main.Listen, nil); err != nil { log.Fatal("ListenAndServe: ", err) } }