-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
73 lines (58 loc) · 1.47 KB
/
main.go
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 (
"flag"
"fmt"
"io"
"net/http"
"github.com/gildasch/upspin-music/album"
"github.com/gildasch/upspin-music/upspin"
"github.com/gin-gonic/gin"
"upspin.io/client"
"upspin.io/config"
_ "upspin.io/transports"
)
type Accesser interface {
List(path string) ([]*album.Album, error)
Get(path string) (io.Reader, error)
}
func main() {
confPathPtr := flag.String("config", "~/upspin/config", "path to the upspin configuration file")
baseURLPtr := flag.String("baseURL", "", "the base URL of the service")
flag.Parse()
cfg, err := config.FromFile(*confPathPtr)
if err != nil {
fmt.Println(err)
return
}
baseURL := *baseURLPtr
client := client.New(cfg)
if client == nil {
fmt.Println("client could be initialized")
}
accesser := upspin.Accesser{client}
router := gin.Default()
router.Static("/static", "./static")
router.LoadHTMLFiles("templates/index.html")
router.GET("/listen/*path", func(c *gin.Context) {
albums, err := accesser.List(c.Param("path"))
if err != nil {
fmt.Println("accesser.List:", err)
c.Status(http.StatusNotFound)
return
}
c.HTML(http.StatusOK, "index.html", gin.H{
"albums": albums,
"baseURL": baseURL,
})
})
router.GET("/get/*path", func(c *gin.Context) {
reader, name, t, err := accesser.Get(c.Param("path"))
if err != nil {
fmt.Println("accesser.Get:", err)
c.Status(http.StatusNotFound)
return
}
http.ServeContent(c.Writer, c.Request, name, t, reader)
})
router.Run()
}