blob: 1811b7f7727b3261c9f66bdf4a95f45329c8ee08 [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 Schraderc49eaf72023-02-26 16:56:52 -080017 "github.com/frc971/971-Robot-Code/scouting/scraping/background"
Philipp Schrader43c730b2023-02-26 20:27:44 -080018 "github.com/frc971/971-Robot-Code/scouting/webserver/match_list"
Yash Chainani37261d42022-04-19 16:21:27 -070019 "github.com/frc971/971-Robot-Code/scouting/webserver/rankings"
Philipp Schradercdb5cfc2022-02-20 14:57:07 -080020 "github.com/frc971/971-Robot-Code/scouting/webserver/requests"
Philipp Schrader5562df72022-02-16 20:56:51 -080021 "github.com/frc971/971-Robot-Code/scouting/webserver/server"
22 "github.com/frc971/971-Robot-Code/scouting/webserver/static"
23)
24
Philipp Schrader7365d322022-03-06 16:40:08 -080025type DatabaseConfig struct {
26 Username string `json:"username"`
27 Password string `json:"password"`
28 Port int `json:"port"`
29}
30
31func 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 Schrader4953cc32022-02-25 18:09:02 -080035 }
Philipp Schrader7365d322022-03-06 16:40:08 -080036
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 Schrader4953cc32022-02-25 18:09:02 -080040 }
Philipp Schrader7365d322022-03-06 16:40:08 -080041
42 return &config, nil
Philipp Schrader4953cc32022-02-25 18:09:02 -080043}
44
Philipp Schrader4e661d62022-03-13 22:15:56 -070045// Gets the default port to use for the webserver. If wrapped by
46// apache_wrapper(), we use the port dictated by the wrapper.
47func 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 Schrader5562df72022-02-16 20:56:51 -080060func main() {
Philipp Schrader4e661d62022-03-13 22:15:56 -070061 portPtr := flag.Int("port", getDefaultPort(), "The port number to bind to.")
Philipp Schrader5562df72022-02-16 20:56:51 -080062 dirPtr := flag.String("directory", ".", "The directory to serve at /.")
Philipp Schrader7365d322022-03-06 16:40:08 -080063 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 Schraderd3fac192022-03-02 20:35:46 -080073 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 Schrader72beced2022-03-07 05:29:52 -080076 "Optionally, it can have a \"base_url\" field with the TBA API base URL.")
Philipp Schrader5562df72022-02-16 20:56:51 -080077 flag.Parse()
78
Philipp Schrader7365d322022-03-06 16:40:08 -080079 // 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 Schrader8747f1b2022-02-23 23:56:22 -0800118 if err != nil {
119 log.Fatal("Failed to connect to database: ", err)
120 }
Philipp Schradereecb8962022-06-01 21:02:42 -0700121 defer database.Delete()
Philipp Schrader8747f1b2022-02-23 23:56:22 -0800122
Philipp Schrader5562df72022-02-16 20:56:51 -0800123 scoutingServer := server.NewScoutingServer()
124 static.ServePages(scoutingServer, *dirPtr)
Philipp Schrader43c730b2023-02-26 20:27:44 -0800125 requests.HandleRequests(database, scoutingServer)
Philipp Schrader5562df72022-02-16 20:56:51 -0800126 scoutingServer.Start(*portPtr)
127 fmt.Println("Serving", *dirPtr, "on port", *portPtr)
128
Philipp Schrader43c730b2023-02-26 20:27:44 -0800129 // 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 Schraderc49eaf72023-02-26 16:56:52 -0800137 rankingsScraper := background.BackgroundScraper{}
138 rankingsScraper.Start(func() {
139 rankings.GetRankings(database, 0, "", *blueAllianceConfigPtr)
140 })
Yash Chainani37261d42022-04-19 16:21:27 -0700141
Philipp Schrader5562df72022-02-16 20:56:51 -0800142 // Block until the user hits Ctrl-C.
143 sigint := make(chan os.Signal, 1)
144 signal.Notify(sigint, syscall.SIGINT)
Philipp Schrader7365d322022-03-06 16:40:08 -0800145 signal.Notify(sigint, syscall.SIGTERM)
146 fmt.Println("Waiting for CTRL-C or SIGTERM.")
Philipp Schrader5562df72022-02-16 20:56:51 -0800147 <-sigint
148
149 fmt.Println("Shutting down.")
150 scoutingServer.Stop()
Philipp Schraderc49eaf72023-02-26 16:56:52 -0800151 rankingsScraper.Stop()
Philipp Schrader43c730b2023-02-26 20:27:44 -0800152 matchListScraper.Stop()
Philipp Schrader5562df72022-02-16 20:56:51 -0800153 fmt.Println("Successfully shut down.")
154}