Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
160 lines (142 loc) · 4.9 KB
/
Copy pathmain.go
File metadata and controls
160 lines (142 loc) · 4.9 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Command subsonic is Streamline's Subsonic and OpenSubsonic server, run as an
// extension: /rest for apps, /share for public share links. Apps sign in with
// the account's email and a Subsonic password kept in the extension's own
// database.
package main
import (
"net/http"
"strings"
"github.com/datahearth/streamline/extensions/sdk"
"github.com/streamline-hq/streamline-subsonic-ext/envelope"
)
// version is the manifest's version and the serverVersion every response
// carries.
const version = "2.0.0"
// main is never run in a reactor module. It names the two exports only so the
// unused linter sees them as entry points; nothing else calls them.
func main() { _, _ = manifest, handle }
//go:wasmexport manifest
func manifest() int32 {
return sdk.WriteManifest(sdk.Manifest{
Name: "subsonic",
Version: version,
StreamlineAPI: sdk.APIVersion,
Mounts: []sdk.Mount{
{Prefix: "/rest", Auth: "self"},
{Prefix: "/share", Auth: "self"},
},
Views: []string{
"artists", "artist_members", "albums", "album_artists", "tracks",
"music_credits", "music_files", "genres", "users", "user_logins",
},
Folders: []string{"music", "artwork", "cache"},
Services: []string{"ffmpeg", "library_scan"},
AccountCards: accountCards(),
})
}
//go:wasmexport handle
func handle() int32 { return sdk.Dispatch(route) }
// endpoints maps a /rest endpoint name to its handler; register fills it from
// the init of each domain file.
var endpoints = map[string]handler{}
// register routes /rest/<name> and /rest/<name>.view to h. It is called from a
// file's init, so adding an endpoint never edits this file.
func register(name string, h handler) {
endpoints[name] = h
endpoints[name+".view"] = h
}
// binaryEndpoints answer binary data on success and an XML error document on
// failure, whatever f says, as the API defines for stream, getCoverArt and
// their kin.
var binaryEndpoints = map[string]bool{}
// registerBinary is register for an endpoint that answers binary data.
func registerBinary(name string, h handler) {
register(name, h)
binaryEndpoints[name] = true
}
// shareHandler answers /share/...; the shares file sets it from its init.
var shareHandler func(sdk.Request) sdk.Response
func registerShare(h func(sdk.Request) sdk.Response) { shareHandler = h }
// openSubsonicExtensions is answered without credentials, so a client can
// learn what the server supports before it signs in.
const openSubsonicExtensions = "getOpenSubsonicExtensions"
// extensions are the OpenSubsonic extensions this server implements.
var extensions = []openSubsonicExtension{
{Name: "formPost", Versions: []int{1}},
{Name: "songLyrics", Versions: []int{1}},
{Name: "transcodeOffset", Versions: []int{1}},
{Name: "indexBasedQueue", Versions: []int{1}},
}
func route(r sdk.Request) sdk.Response {
if err := ensureSchema(); err != nil {
sdk.Log("error", "preparing the database failed", "error", err.Error())
return textResponse(http.StatusInternalServerError, "internal error")
}
if r.Path == "/cards" || strings.HasPrefix(r.Path, "/cards/") {
return cards(r)
}
switch r.Mount {
case "/rest":
name := strings.TrimSuffix(strings.TrimPrefix(r.Path, "/"), ".view")
return serveREST(r, name)
case "/share":
if shareHandler != nil {
return shareHandler(r)
}
}
return notFoundResponse()
}
func serveREST(r sdk.Request, name string) sdk.Response {
c, err := newReqCtx(r)
if err != nil {
return (&reqCtx{req: r}).fail(envelope.CodeGeneric, "Malformed request")
}
c.xmlErrors = binaryEndpoints[name]
if c.param("f") == envelope.FormatJSONP &&
!envelope.ValidCallback(c.param("callback")) {
// No callback to wrap the error in: it goes out as XML.
c.xmlErrors = true
return c.failErr(envelope.MissingParam("callback"))
}
if name == openSubsonicExtensions {
return c.ok(&body{OpenSubsonicExtensions: extensions})
}
if err := c.checkClient(); err != nil {
return c.failErr(err)
}
if err := c.authenticate(); err != nil {
return c.failErr(err)
}
h, found := endpoints[name]
if !found {
return c.fail(envelope.CodeGeneric, "Not implemented")
}
return callHandler(c, name, h)
}
// checkClient requires the v and c every call carries, and refuses a v that
// names a protocol this server cannot speak (error 20 or 30).
func (c *reqCtx) checkClient() error {
v := c.param("v")
if v == "" {
return envelope.MissingParam("v")
}
if c.param("c") == "" {
return envelope.MissingParam("c")
}
return envelope.CheckVersion(v)
}
// callHandler runs h and turns a panic into the generic Subsonic error, so a
// bug in one endpoint answers like any other failure and not as a bare 500.
func callHandler(c *reqCtx, name string, h handler) sdk.Response {
var resp sdk.Response
func() {
defer func() {
if p := recover(); p != nil {
sdk.Log("error", "a subsonic handler panicked", "endpoint", name)
resp = c.fail(envelope.CodeGeneric, "Internal error")
}
}()
resp = h(c)
}()
return resp
}