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