package main import ( "embed" "flag" "fmt" "log" "net" "net/http" "os" "strconv" "strings" ) // Generate our swagger docs //go:generate swag init --parseDependency var ( // Version is overridden by the build script Version = "Development" // Logger default logging to stderr Logger = log.New(os.Stderr, "", log.LstdFlags) // Listen address for the webserver Listen = ":8899" //go:embed static/* StaticFS embed.FS //go:embed templates/* TemplateFS embed.FS ) // @title Steam Exporter API // @version 1.0 // @description The steam exporter is designed to make it easy to export steam games across the network. // @contact.name Mitchell Riedstra // @contact.url https://riedstra.dev/steam-export // @contact.email steam-export@riedstra.dev // @license.name ISC // @license.url https://opensource.org/licenses/ISC // @host localhost:8899 // @BasePath /api/v1 // @securityDefinitions.basic BasicAuth // @securityDefinitions.apikey ApiKeyAuth // @in header // @name Authorization func main() { fl := flag.NewFlagSet("steam-export", flag.ExitOnError) debug := fl.Bool("d", false, "Print line numbers in log") fl.StringVar(&Listen, "l", Listen, "What address/port do we listen on?") fl.StringVar(&DefaultLib, "L", DefaultLib, "Full path to default library") fl.StringVar(&isLocalCIDR, "t", isLocalCIDR, "Trusted CIDRs for additional controls, seperated by commas") fl.StringVar(&shareLink, "s", shareLink, "Share link, if blank make an educated guess") isDemo := fl.Bool("demo", false, "Whether or not to run in demo mode. You probably don't want this on.") localFS := fl.String("fs", "", "If not empty the local path to use instead of the "+ "embedded templates and /static directory.") noStartBrowser := fl.Bool("nobrowser", false, "If supplied, do not start the browser") fl.Parse(os.Args[1:]) if *debug { Logger.SetFlags(log.LstdFlags | log.Llongfile) } a, err := NewApp(DefaultLib) if err != nil { Logger.Fatal(err) } if *localFS != "" { a.useLocalFS(*localFS) } if *isDemo { a.Demo = true } s := http.Server{Handler: a} for i := 0; i < 5; i++ { l, err := net.Listen("tcp", Listen) if err != nil { Logger.Printf("Encountered: %s", err) parts := strings.Split(Listen, ":") port, err := strconv.Atoi(parts[1]) if err != nil { panic(err) } port++ Listen = fmt.Sprintf("%s:%d", parts[0], port) Logger.Printf("Trying: %s", Listen) continue } if !*noStartBrowser { // Not using 'localhost' due to the way windows listens by default startBrowser("http://127.0.0.1" + Listen) } err = s.Serve(l) if err != nil { panic(err) } } }