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

import (
	"log"

	"git.riedstra.us/mitch/steam-export/lib/config"
	"git.riedstra.us/mitch/steam-export/lib/steam"

	"github.com/kataras/iris"
)

func main() {
	config, err := config.LoadConfig()
	if err != nil {
		log.Fatal(err)
	}

	api := iris.New()

	api.Get("/api/libraries/", listLibraries(config))
	api.Get("/api/libraries/:id", listLibrary(config))

	api.Get("/data/:value/:and/:some/:giggles", dataTest())
	api.Get("/data/:value/:and", dataTest())
	api.Get("/data/:value", dataTest())

	api.Listen(config.Listen)
}

func dataTest() iris.HandlerFunc {
	return func(ctx *iris.Context) {
		ctx.JSON(200, ctx.ParamsSentence())
	}
}

func listLibraries(c *config.Config) iris.HandlerFunc {
	return func(ctx *iris.Context) {
		libs, err := steam.ProcessMultipleLibraries(c.SteamRepositories)
		if err != nil {
			ctx.JSON(500, err)
		}
		ctx.JSON(200, libs)
	}
}

func listLibrary(c *config.Config) iris.HandlerFunc {
	return func(ctx *iris.Context) {
		id, err := ctx.ParamInt("id")
		if err != nil {
			ctx.JSON(500, err)
		}

		libs, err := steam.ProcessMultipleLibraries(c.SteamRepositories)
		if err != nil {
			ctx.JSON(500, err)
		}

		if id >= len(libs) {
			ctx.JSON(500, &struct{ Error string }{Error: "Index out of bounds"})
		}

		ctx.JSON(200, libs[id])
	}
}