Philipp Schrader | 5562df7 | 2022-02-16 20:56:51 -0800 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
Philipp Schrader | 7365d32 | 2022-03-06 16:40:08 -0800 | [diff] [blame] | 4 | "encoding/json" |
Philipp Schrader | d3fac19 | 2022-03-02 20:35:46 -0800 | [diff] [blame] | 5 | "errors" |
Philipp Schrader | 5562df7 | 2022-02-16 20:56:51 -0800 | [diff] [blame] | 6 | "flag" |
| 7 | "fmt" |
Philipp Schrader | 4953cc3 | 2022-02-25 18:09:02 -0800 | [diff] [blame] | 8 | "io/ioutil" |
Philipp Schrader | 8747f1b | 2022-02-23 23:56:22 -0800 | [diff] [blame] | 9 | "log" |
Philipp Schrader | 5562df7 | 2022-02-16 20:56:51 -0800 | [diff] [blame] | 10 | "os" |
| 11 | "os/signal" |
Philipp Schrader | 4e661d6 | 2022-03-13 22:15:56 -0700 | [diff] [blame] | 12 | "strconv" |
Philipp Schrader | 5562df7 | 2022-02-16 20:56:51 -0800 | [diff] [blame] | 13 | "syscall" |
Philipp Schrader | 7365d32 | 2022-03-06 16:40:08 -0800 | [diff] [blame] | 14 | "time" |
Philipp Schrader | 5562df7 | 2022-02-16 20:56:51 -0800 | [diff] [blame] | 15 | |
Philipp Schrader | 8747f1b | 2022-02-23 23:56:22 -0800 | [diff] [blame] | 16 | "github.com/frc971/971-Robot-Code/scouting/db" |
Philipp Schrader | c49eaf7 | 2023-02-26 16:56:52 -0800 | [diff] [blame] | 17 | "github.com/frc971/971-Robot-Code/scouting/scraping/background" |
Philipp Schrader | 0247300 | 2023-03-07 19:10:17 -0800 | [diff] [blame^] | 18 | "github.com/frc971/971-Robot-Code/scouting/webserver/driver_ranking" |
Philipp Schrader | 43c730b | 2023-02-26 20:27:44 -0800 | [diff] [blame] | 19 | "github.com/frc971/971-Robot-Code/scouting/webserver/match_list" |
Yash Chainani | 37261d4 | 2022-04-19 16:21:27 -0700 | [diff] [blame] | 20 | "github.com/frc971/971-Robot-Code/scouting/webserver/rankings" |
Philipp Schrader | cdb5cfc | 2022-02-20 14:57:07 -0800 | [diff] [blame] | 21 | "github.com/frc971/971-Robot-Code/scouting/webserver/requests" |
Philipp Schrader | 5562df7 | 2022-02-16 20:56:51 -0800 | [diff] [blame] | 22 | "github.com/frc971/971-Robot-Code/scouting/webserver/server" |
| 23 | "github.com/frc971/971-Robot-Code/scouting/webserver/static" |
| 24 | ) |
| 25 | |
Philipp Schrader | 7365d32 | 2022-03-06 16:40:08 -0800 | [diff] [blame] | 26 | type DatabaseConfig struct { |
| 27 | Username string `json:"username"` |
| 28 | Password string `json:"password"` |
| 29 | Port int `json:"port"` |
| 30 | } |
| 31 | |
| 32 | func readDatabaseConfig(configPath string) (*DatabaseConfig, error) { |
| 33 | content, err := ioutil.ReadFile(configPath) |
| 34 | if err != nil { |
| 35 | return nil, errors.New(fmt.Sprint("Failed to open database config at ", configPath, ": ", err)) |
Philipp Schrader | 4953cc3 | 2022-02-25 18:09:02 -0800 | [diff] [blame] | 36 | } |
Philipp Schrader | 7365d32 | 2022-03-06 16:40:08 -0800 | [diff] [blame] | 37 | |
| 38 | var config DatabaseConfig |
| 39 | if err := json.Unmarshal([]byte(content), &config); err != nil { |
| 40 | return nil, errors.New(fmt.Sprint("Failed to parse database config file as JSON: ", err)) |
Philipp Schrader | 4953cc3 | 2022-02-25 18:09:02 -0800 | [diff] [blame] | 41 | } |
Philipp Schrader | 7365d32 | 2022-03-06 16:40:08 -0800 | [diff] [blame] | 42 | |
| 43 | return &config, nil |
Philipp Schrader | 4953cc3 | 2022-02-25 18:09:02 -0800 | [diff] [blame] | 44 | } |
| 45 | |
Philipp Schrader | 4e661d6 | 2022-03-13 22:15:56 -0700 | [diff] [blame] | 46 | // Gets the default port to use for the webserver. If wrapped by |
| 47 | // apache_wrapper(), we use the port dictated by the wrapper. |
| 48 | func getDefaultPort() int { |
| 49 | port_str := os.Getenv("APACHE_WRAPPED_PORT") |
| 50 | if port_str != "" { |
| 51 | port, err := strconv.Atoi(port_str) |
| 52 | if err != nil { |
| 53 | log.Fatalf("Failed to parse \"%s\" as integer: %v", port_str, err) |
| 54 | } |
| 55 | return port |
| 56 | } |
| 57 | |
| 58 | return 8080 |
| 59 | } |
| 60 | |
Philipp Schrader | 5562df7 | 2022-02-16 20:56:51 -0800 | [diff] [blame] | 61 | func main() { |
Philipp Schrader | 4e661d6 | 2022-03-13 22:15:56 -0700 | [diff] [blame] | 62 | portPtr := flag.Int("port", getDefaultPort(), "The port number to bind to.") |
Philipp Schrader | 5562df7 | 2022-02-16 20:56:51 -0800 | [diff] [blame] | 63 | dirPtr := flag.String("directory", ".", "The directory to serve at /.") |
Philipp Schrader | 7365d32 | 2022-03-06 16:40:08 -0800 | [diff] [blame] | 64 | dbConfigPtr := flag.String("db_config", "", |
| 65 | "The postgres database JSON config. It needs the following keys: "+ |
| 66 | "\"username\", \"password\", and \"port\". "+ |
| 67 | "This option cannot be used in conjunction with -testdb_port.") |
| 68 | testDbPortPtr := flag.Int("testdb_port", 0, |
| 69 | "If set, connects to an instance of //scouting/db/testdb_server at the "+ |
| 70 | "specified port. This option cannot be used in conjunction with "+ |
| 71 | "-db_config.") |
| 72 | dbConnectRetries := flag.Int("db_retries", 5, |
| 73 | "The number of seconds to retry connecting to the database on startup.") |
Philipp Schrader | d3fac19 | 2022-03-02 20:35:46 -0800 | [diff] [blame] | 74 | blueAllianceConfigPtr := flag.String("tba_config", "", |
| 75 | "The path to your The Blue Alliance JSON config. "+ |
| 76 | "It needs an \"api_key\" field with your TBA API key. "+ |
Philipp Schrader | 72beced | 2022-03-07 05:29:52 -0800 | [diff] [blame] | 77 | "Optionally, it can have a \"base_url\" field with the TBA API base URL.") |
Philipp Schrader | 5562df7 | 2022-02-16 20:56:51 -0800 | [diff] [blame] | 78 | flag.Parse() |
| 79 | |
Philipp Schrader | 7365d32 | 2022-03-06 16:40:08 -0800 | [diff] [blame] | 80 | // Set up the database config. It's either specified via a JSON file on |
| 81 | // disk (-db_config) or we can generate an in-memory config when the |
| 82 | // user wants to connect to a `testdb_server` instance (-testdb_port). |
| 83 | var dbConfig *DatabaseConfig |
| 84 | var err error |
| 85 | if *dbConfigPtr != "" { |
| 86 | if *testDbPortPtr != 0 { |
| 87 | log.Fatal("Cannot specify both -db_config and -testdb_port. Choose one.") |
| 88 | } |
| 89 | dbConfig, err = readDatabaseConfig(*dbConfigPtr) |
| 90 | if err != nil { |
| 91 | log.Fatal("Failed to read ", *dbConfigPtr, ": ", err) |
| 92 | } |
| 93 | } else if *testDbPortPtr != 0 { |
| 94 | dbConfig = &DatabaseConfig{ |
| 95 | Username: "test", |
| 96 | Password: "password", |
| 97 | Port: *testDbPortPtr, |
| 98 | } |
| 99 | } else { |
| 100 | log.Fatal("Must specify one of -db_config and -testdb_port. See " + |
| 101 | "https://github.com/frc971/971-Robot-Code/blob/master/scouting/README.md " + |
| 102 | "for more information.") |
| 103 | } |
| 104 | |
| 105 | // Connect to the database. It may still be starting up so we do a |
| 106 | // bunch of retries here. The number of retries can be set on the |
| 107 | // command line. |
| 108 | var database *db.Database |
| 109 | for i := 0; i < *dbConnectRetries*10; i++ { |
| 110 | database, err = db.NewDatabase(dbConfig.Username, dbConfig.Password, dbConfig.Port) |
| 111 | if err == nil { |
| 112 | break |
| 113 | } |
| 114 | if i%10 == 0 { |
| 115 | log.Println("Failed to connect to the database. Retrying in a bit.") |
| 116 | } |
| 117 | time.Sleep(1 * time.Millisecond) |
| 118 | } |
Philipp Schrader | 8747f1b | 2022-02-23 23:56:22 -0800 | [diff] [blame] | 119 | if err != nil { |
| 120 | log.Fatal("Failed to connect to database: ", err) |
| 121 | } |
Philipp Schrader | eecb896 | 2022-06-01 21:02:42 -0700 | [diff] [blame] | 122 | defer database.Delete() |
Philipp Schrader | 8747f1b | 2022-02-23 23:56:22 -0800 | [diff] [blame] | 123 | |
Philipp Schrader | 5562df7 | 2022-02-16 20:56:51 -0800 | [diff] [blame] | 124 | scoutingServer := server.NewScoutingServer() |
| 125 | static.ServePages(scoutingServer, *dirPtr) |
Philipp Schrader | 43c730b | 2023-02-26 20:27:44 -0800 | [diff] [blame] | 126 | requests.HandleRequests(database, scoutingServer) |
Philipp Schrader | 5562df7 | 2022-02-16 20:56:51 -0800 | [diff] [blame] | 127 | scoutingServer.Start(*portPtr) |
| 128 | fmt.Println("Serving", *dirPtr, "on port", *portPtr) |
| 129 | |
Philipp Schrader | 43c730b | 2023-02-26 20:27:44 -0800 | [diff] [blame] | 130 | // Since Go doesn't support default arguments, we use 0 and "" to |
| 131 | // indicate that we want to source the values from the config. |
| 132 | |
| 133 | matchListScraper := background.BackgroundScraper{} |
| 134 | matchListScraper.Start(func() { |
| 135 | match_list.GetMatchList(database, 0, "", *blueAllianceConfigPtr) |
| 136 | }) |
| 137 | |
Philipp Schrader | c49eaf7 | 2023-02-26 16:56:52 -0800 | [diff] [blame] | 138 | rankingsScraper := background.BackgroundScraper{} |
| 139 | rankingsScraper.Start(func() { |
| 140 | rankings.GetRankings(database, 0, "", *blueAllianceConfigPtr) |
| 141 | }) |
Yash Chainani | 37261d4 | 2022-04-19 16:21:27 -0700 | [diff] [blame] | 142 | |
Philipp Schrader | 0247300 | 2023-03-07 19:10:17 -0800 | [diff] [blame^] | 143 | driverRankingParser := background.BackgroundScraper{} |
| 144 | driverRankingParser.Start(func() { |
| 145 | // Specify "" as the script path here so that the default is |
| 146 | // used. |
| 147 | driver_ranking.GenerateFullDriverRanking(database, "") |
| 148 | }) |
| 149 | |
Philipp Schrader | 5562df7 | 2022-02-16 20:56:51 -0800 | [diff] [blame] | 150 | // Block until the user hits Ctrl-C. |
| 151 | sigint := make(chan os.Signal, 1) |
| 152 | signal.Notify(sigint, syscall.SIGINT) |
Philipp Schrader | 7365d32 | 2022-03-06 16:40:08 -0800 | [diff] [blame] | 153 | signal.Notify(sigint, syscall.SIGTERM) |
| 154 | fmt.Println("Waiting for CTRL-C or SIGTERM.") |
Philipp Schrader | 5562df7 | 2022-02-16 20:56:51 -0800 | [diff] [blame] | 155 | <-sigint |
| 156 | |
| 157 | fmt.Println("Shutting down.") |
| 158 | scoutingServer.Stop() |
Philipp Schrader | c49eaf7 | 2023-02-26 16:56:52 -0800 | [diff] [blame] | 159 | rankingsScraper.Stop() |
Philipp Schrader | 0247300 | 2023-03-07 19:10:17 -0800 | [diff] [blame^] | 160 | driverRankingParser.Stop() |
Philipp Schrader | 43c730b | 2023-02-26 20:27:44 -0800 | [diff] [blame] | 161 | matchListScraper.Stop() |
Philipp Schrader | 5562df7 | 2022-02-16 20:56:51 -0800 | [diff] [blame] | 162 | fmt.Println("Successfully shut down.") |
| 163 | } |