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