aboutsummaryrefslogtreecommitdiff
path: root/cmd/web/app.go
blob: b0749b5648d2b6a29215ec9570d7ea55f6b0875a (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
package main

import (
	"html/template"
	"sync"
	"time"

	"riedstra.dev/mitch/steam-export/steam"
)

type steamLib struct {
	steam.Library
	sync.Mutex
}

type statusInfo struct {
	sync.RWMutex
	Running     bool
	Error       error
	Url         string
	Transferred int64
	Size        int64
	Start       *time.Time
}

type App struct {
	Library *steamLib
	Status  *statusInfo

	Templates *template.Template

	// Sending to this channel triggers the downloader in the background
	download chan string
}

func NewApp(libPath string) (*App, error) {
	lib, err := steam.NewLibrary(libPath)
	if err != nil {
		return nil, err
	}

	a := &App{
		Library:  &steamLib{},
		Status:   &statusInfo{},
		download: make(chan string),
	}

	a.Library.Library = *lib

	a.Templates = template.Must(template.New("index").Parse(indexTemplate))

	return a, nil
}

func (a *App) LibrarySet(path string) {
	Logger.Println("Starting library reload")
	a.Library.Lock()
	defer a.Library.Unlock()
	var err error
	l2, err := steam.NewLibrary(path)
	if err != nil {
		Logger.Printf("Error reopening lib: %s", err)
		return
	}
	a.Library.Library = *l2
	Logger.Println("Done reloading lib")
}

func (a *App) LibraryReload() {
	cur := a.Library.Folder
	a.LibrarySet(cur)
	return
}