blob: 282e2480f089c35298bcda40c8f0e3c2f46cecb4 [file] [log] [blame]
Philipp Schrader5562df72022-02-16 20:56:51 -08001package main
2
3import (
Philipp Schraderd3fac192022-03-02 20:35:46 -08004 "errors"
Philipp Schrader5562df72022-02-16 20:56:51 -08005 "flag"
6 "fmt"
Philipp Schrader4953cc32022-02-25 18:09:02 -08007 "io/ioutil"
Philipp Schrader8747f1b2022-02-23 23:56:22 -08008 "log"
Philipp Schrader5562df72022-02-16 20:56:51 -08009 "os"
10 "os/signal"
Philipp Schrader4953cc32022-02-25 18:09:02 -080011 "path/filepath"
Philipp Schrader5562df72022-02-16 20:56:51 -080012 "syscall"
13
Philipp Schrader8747f1b2022-02-23 23:56:22 -080014 "github.com/frc971/971-Robot-Code/scouting/db"
Philipp Schraderd3fac192022-03-02 20:35:46 -080015 "github.com/frc971/971-Robot-Code/scouting/scraping"
Philipp Schradercdb5cfc2022-02-20 14:57:07 -080016 "github.com/frc971/971-Robot-Code/scouting/webserver/requests"
Philipp Schrader5562df72022-02-16 20:56:51 -080017 "github.com/frc971/971-Robot-Code/scouting/webserver/server"
18 "github.com/frc971/971-Robot-Code/scouting/webserver/static"
19)
20
Philipp Schrader4953cc32022-02-25 18:09:02 -080021func getDefaultDatabasePath() string {
22 // If using `bazel run`, let's create the database in the root of the
23 // workspace.
24 workspaceDir := os.Getenv("BUILD_WORKSPACE_DIRECTORY")
25 if workspaceDir != "" {
26 return filepath.Join(workspaceDir, "scouting.db")
27 }
28 // If we're inside a `bazel test`, then we will create the database in
29 // a temporary directory.
30 testTempDir := os.Getenv("TEST_TMPDIR")
31 if testTempDir != "" {
32 tempDir, err := ioutil.TempDir(testTempDir, "db")
33 if err != nil {
34 log.Fatal("Failed to create temporary directory in TEST_TMPDIR: ", err)
35 }
36 return filepath.Join(tempDir, "scouting.db")
37 }
38 return filepath.Join(".", "scouting.db")
39}
40
Philipp Schrader5562df72022-02-16 20:56:51 -080041func main() {
42 portPtr := flag.Int("port", 8080, "The port number to bind to.")
43 dirPtr := flag.String("directory", ".", "The directory to serve at /.")
Philipp Schrader4953cc32022-02-25 18:09:02 -080044 dbPathPtr := flag.String("database", getDefaultDatabasePath(), "The path to the database.")
Philipp Schraderd3fac192022-03-02 20:35:46 -080045 blueAllianceConfigPtr := flag.String("tba_config", "",
46 "The path to your The Blue Alliance JSON config. "+
47 "It needs an \"api_key\" field with your TBA API key. "+
Philipp Schrader72beced2022-03-07 05:29:52 -080048 "Optionally, it can have a \"base_url\" field with the TBA API base URL.")
Philipp Schrader5562df72022-02-16 20:56:51 -080049 flag.Parse()
50
Philipp Schrader4953cc32022-02-25 18:09:02 -080051 database, err := db.NewDatabase(*dbPathPtr)
Philipp Schrader8747f1b2022-02-23 23:56:22 -080052 if err != nil {
53 log.Fatal("Failed to connect to database: ", err)
54 }
55
Philipp Schraderd3fac192022-03-02 20:35:46 -080056 scrapeMatchList := func(year int32, eventCode string) ([]scraping.Match, error) {
57 if *blueAllianceConfigPtr == "" {
58 return nil, errors.New("Cannot scrape TBA's match list without a config file.")
59 }
60 return scraping.AllMatches(year, eventCode, *blueAllianceConfigPtr)
61 }
62
Philipp Schrader5562df72022-02-16 20:56:51 -080063 scoutingServer := server.NewScoutingServer()
64 static.ServePages(scoutingServer, *dirPtr)
Philipp Schraderd3fac192022-03-02 20:35:46 -080065 requests.HandleRequests(database, scrapeMatchList, scoutingServer)
Philipp Schrader5562df72022-02-16 20:56:51 -080066 scoutingServer.Start(*portPtr)
67 fmt.Println("Serving", *dirPtr, "on port", *portPtr)
68
69 // Block until the user hits Ctrl-C.
70 sigint := make(chan os.Signal, 1)
71 signal.Notify(sigint, syscall.SIGINT)
72 fmt.Println("Waiting for CTRL-C or SIGINT.")
73 <-sigint
74
75 fmt.Println("Shutting down.")
76 scoutingServer.Stop()
77 fmt.Println("Successfully shut down.")
78}