aboutsummaryrefslogtreecommitdiff
path: root/cmd/web/main.go
blob: 1e29e27929ccba11ff680ba2bd6dbeac9e96b206 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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)
		}
	}
}