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