blob: 8f4298b44c8df117c1da18d264763891b196519b [file] [log] [blame]
Philipp Schrader5562df72022-02-16 20:56:51 -08001package main
2
3import (
Philipp Schrader7365d322022-03-06 16:40:08 -08004 "encoding/json"
Philipp Schraderd3fac192022-03-02 20:35:46 -08005 "errors"
Philipp Schrader5562df72022-02-16 20:56:51 -08006 "flag"
7 "fmt"
Philipp Schrader4953cc32022-02-25 18:09:02 -08008 "io/ioutil"
Philipp Schrader8747f1b2022-02-23 23:56:22 -08009 "log"
Philipp Schrader5562df72022-02-16 20:56:51 -080010 "os"
11 "os/signal"
Philipp Schrader4e661d62022-03-13 22:15:56 -070012 "strconv"
Philipp Schrader5562df72022-02-16 20:56:51 -080013 "syscall"
Philipp Schrader7365d322022-03-06 16:40:08 -080014 "time"
Philipp Schrader5562df72022-02-16 20:56:51 -080015
Philipp Schrader8747f1b2022-02-23 23:56:22 -080016 "github.com/frc971/971-Robot-Code/scouting/db"
Philipp Schraderd3fac192022-03-02 20:35:46 -080017 "github.com/frc971/971-Robot-Code/scouting/scraping"
Philipp Schradercdb5cfc2022-02-20 14:57:07 -080018 "github.com/frc971/971-Robot-Code/scouting/webserver/requests"
Philipp Schrader5562df72022-02-16 20:56:51 -080019 "github.com/frc971/971-Robot-Code/scouting/webserver/server"
20 "github.com/frc971/971-Robot-Code/scouting/webserver/static"
21)
22
Philipp Schrader7365d322022-03-06 16:40:08 -080023type DatabaseConfig struct {
24 Username string `json:"username"`
25 Password string `json:"password"`
26 Port int `json:"port"`
27}
28
29func 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 Schrader4953cc32022-02-25 18:09:02 -080033 }
Philipp Schrader7365d322022-03-06 16:40:08 -080034
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 Schrader4953cc32022-02-25 18:09:02 -080038 }
Philipp Schrader7365d322022-03-06 16:40:08 -080039
40 return &config, nil
Philipp Schrader4953cc32022-02-25 18:09:02 -080041}
42
Philipp Schrader4e661d62022-03-13 22:15:56 -070043// Gets the default port to use for the webserver. If wrapped by
44// apache_wrapper(), we use the port dictated by the wrapper.
45func 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 Schrader5562df72022-02-16 20:56:51 -080058func main() {
Philipp Schrader4e661d62022-03-13 22:15:56 -070059 portPtr := flag.Int("port", getDefaultPort(), "The port number to bind to.")
Philipp Schrader5562df72022-02-16 20:56:51 -080060 dirPtr := flag.String("directory", ".", "The directory to serve at /.")
Philipp Schrader7365d322022-03-06 16:40:08 -080061 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 Schraderd3fac192022-03-02 20:35:46 -080071 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 Schrader72beced2022-03-07 05:29:52 -080074 "Optionally, it can have a \"base_url\" field with the TBA API base URL.")
Philipp Schrader5562df72022-02-16 20:56:51 -080075 flag.Parse()
76
Philipp Schrader7365d322022-03-06 16:40:08 -080077 // 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 Schrader8747f1b2022-02-23 23:56:22 -0800116 if err != nil {
117 log.Fatal("Failed to connect to database: ", err)
118 }
Philipp Schrader7365d322022-03-06 16:40:08 -0800119 defer database.Close()
Philipp Schrader8747f1b2022-02-23 23:56:22 -0800120
Philipp Schraderd3fac192022-03-02 20:35:46 -0800121 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 Schrader5562df72022-02-16 20:56:51 -0800128 scoutingServer := server.NewScoutingServer()
129 static.ServePages(scoutingServer, *dirPtr)
Philipp Schraderd3fac192022-03-02 20:35:46 -0800130 requests.HandleRequests(database, scrapeMatchList, scoutingServer)
Philipp Schrader5562df72022-02-16 20:56:51 -0800131 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 Schrader7365d322022-03-06 16:40:08 -0800137 signal.Notify(sigint, syscall.SIGTERM)
138 fmt.Println("Waiting for CTRL-C or SIGTERM.")
Philipp Schrader5562df72022-02-16 20:56:51 -0800139 <-sigint
140
141 fmt.Println("Shutting down.")
142 scoutingServer.Stop()
143 fmt.Println("Successfully shut down.")
144}