aboutsummaryrefslogtreecommitdiff
path: root/cmd/web/main.go
blob: 875b2d8e29b8d214db0fd4d0efde2cdb7d2ac8bf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main

import (
	"embed"
	"flag"
	"fmt"
	"log"
	"math/rand"
	"net/http"
	"os"
	"time"

	"github.com/gorilla/mux"
)

var (
	Version = "Development"
	Logger  = log.New(os.Stderr, "", log.LstdFlags)
	Listen  = ":8899"

	//go:embed static/*
	embeddedStatic embed.FS
	//go:embed templates/index.html
	indexTemplate string
)

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 do we listen on?")
	fl.StringVar(&DefaultLib, "L", DefaultLib, "Full path to default library")
	fl.Parse(os.Args[1:])

	if *debug {
		Logger.SetFlags(log.LstdFlags | log.Llongfile)
	}

	a, err := NewApp(DefaultLib)
	if err != nil {
		Logger.Fatal(err)
	}

	go a.installer()

	r := mux.NewRouter()

	r.HandleFunc("/quit", HandleQuit)
	r.Handle("/setLib", UnauthorizedIfNotLocal(http.HandlerFunc(a.HandleSetLib)))
	r.Handle("/delete", UnauthorizedIfNotLocal(http.HandlerFunc(a.HandleDelete)))
	r.Handle("/install", UnauthorizedIfNotLocal(http.HandlerFunc(a.HandleInstall)))
	r.HandleFunc("/steam-export-web.exe", serveSelf)
	r.HandleFunc("/download/{game}", a.HandleDownload)
	r.Handle("/status", UnauthorizedIfNotLocal(http.HandlerFunc(a.HandleStats)))
	r.PathPrefix("/static").Handler(
		http.FileServer(http.FS(embeddedStatic)))
	r.HandleFunc("/", a.HandleIndex)

	s := http.Server{
		Handler: r,
		Addr:    Listen,
	}

	go startBrowser()

	for i := 0; i < 5; i++ {
		err = s.ListenAndServe()
		if err != nil {
			Logger.Printf("Encountered: %s", err)
			rand.Seed(time.Now().UnixNano())
			Listen = fmt.Sprintf(":%d", rand.Intn(9000)+1024)
			Logger.Printf("Trying: %s", Listen)
			s.Addr = Listen
		}

	}
}