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