Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
stream-server.h-da.io
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Model registry
Operate
Environments
Analyze
Contributor analytics
Repository analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Stream Server Project
stream-server.h-da.io
Commits
25034b1e
Commit
25034b1e
authored
5 years ago
by
Simon Kirsten
Browse files
Options
Downloads
Patches
Plain Diff
Twitch helix api proxy patch
parent
a388d7c5
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
internal/server/server.go
+1
-6
1 addition, 6 deletions
internal/server/server.go
internal/server/twitch.go
+18
-46
18 additions, 46 deletions
internal/server/twitch.go
internal/static/index.html
+1
-1
1 addition, 1 deletion
internal/static/index.html
with
20 additions
and
53 deletions
internal/server/server.go
+
1
−
6
View file @
25034b1e
...
@@ -11,12 +11,7 @@ import (
...
@@ -11,12 +11,7 @@ import (
func
ListenAndServe
(
listenAddr
string
)
error
{
func
ListenAndServe
(
listenAddr
string
)
error
{
r
:=
chi
.
NewRouter
()
r
:=
chi
.
NewRouter
()
r
.
Route
(
"/twitch"
,
func
(
r
chi
.
Router
)
{
r
.
Mount
(
"/twitch"
,
http
.
StripPrefix
(
"/twitch/"
,
http
.
HandlerFunc
(
twitchHandler
)))
r
.
Get
(
"/getTopGames"
,
twitchGetTopGamesHandler
)
r
.
Get
(
"/searchGames"
,
twitchSearchGamesHandler
)
r
.
Get
(
"/getTopStreams"
,
twitchGetTopStreamsHandler
)
r
.
Get
(
"/getFeaturedStreams"
,
twitchGetFeaturedStreamsHandler
)
})
r
.
Get
(
"/display"
,
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
r
.
Get
(
"/display"
,
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
if
r
.
Header
.
Get
(
"Accept"
)
==
"text/event-stream"
{
if
r
.
Header
.
Get
(
"Accept"
)
==
"text/event-stream"
{
...
...
This diff is collapsed.
Click to expand it.
internal/server/twitch.go
+
18
−
46
View file @
25034b1e
package
server
package
server
import
(
import
(
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http"
"os"
"os"
streamserver
"stream-server"
streamserver
"stream-server"
"stream-server/internal/util"
"stream-server/pkg/twitch"
"stream-server/pkg/twitch"
)
)
...
@@ -22,70 +25,39 @@ func init() {
...
@@ -22,70 +25,39 @@ func init() {
}
}
}
}
// twitchGetTopGamesHandler handles the GetTopGames endpoint
// twitchHandler proxies request to the helix twitch API
func
twitchGetTopGamesHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
func
twitchHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
response
,
err
:=
client
.
GetTopGames
()
if
err
!=
nil
{
http
.
Error
(
w
,
err
.
Error
(),
http
.
StatusBadGateway
)
return
}
util
.
ServeIndentedJSON
(
w
,
r
,
&
response
)
url
:=
fmt
.
Sprintf
(
"https://api.twitch.tv/helix/%s"
,
r
.
URL
.
RequestURI
())
}
// twitchSearchGamesHandler handles the SearchGames endpoint
fmt
.
Printf
(
"url: %s
\n
"
,
url
)
func
twitchSearchGamesHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
paramQuery
,
err
:=
util
.
GetSingleQueryParam
(
r
,
"query"
)
if
err
!=
nil
{
http
.
Error
(
w
,
err
.
Error
(),
http
.
StatusBadRequest
)
return
}
if
paramQuery
==
nil
||
*
paramQuery
==
""
{
http
.
Error
(
w
,
"Parameter 'query' is required and can not be empty."
,
http
.
StatusBadRequest
)
return
}
re
sponse
,
err
:=
client
.
SearchGames
(
*
paramQuery
)
re
q
,
err
:=
http
.
NewRequest
(
"GET"
,
url
,
nil
)
if
err
!=
nil
{
if
err
!=
nil
{
http
.
Error
(
w
,
err
.
Error
(),
http
.
StatusBadGateway
)
http
.
Error
(
w
,
err
.
Error
(),
http
.
StatusBadGateway
)
return
return
}
}
util
.
ServeIndentedJSON
(
w
,
r
,
&
response
)
req
.
Header
.
Add
(
"Client-ID"
,
client
.
ClientID
)
}
resp
,
err
:=
http
.
DefaultClient
.
Do
(
req
)
// twitchGetTopStreamsHandler handles the GetTopStreams endpoint
func
twitchGetTopStreamsHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
paramChannels
:=
util
.
GetMultipleQueryParams
(
r
,
"channels"
)
game
,
err
:=
util
.
GetSingleQueryParam
(
r
,
"game"
)
if
err
!=
nil
{
http
.
Error
(
w
,
err
.
Error
(),
http
.
StatusBadRequest
)
return
}
language
,
err
:=
util
.
GetSingleQueryParam
(
r
,
"language"
)
if
err
!=
nil
{
if
err
!=
nil
{
http
.
Error
(
w
,
err
.
Error
(),
http
.
StatusBad
Request
)
http
.
Error
(
w
,
err
.
Error
(),
http
.
StatusBad
Gateway
)
return
return
}
}
response
,
err
:=
client
.
GetTopStreams
(
paramChannels
,
game
,
language
)
defer
resp
.
Body
.
Close
()
body
,
err
:=
ioutil
.
ReadAll
(
resp
.
Body
)
if
err
!=
nil
{
if
err
!=
nil
{
http
.
Error
(
w
,
err
.
Error
(),
http
.
StatusBadGateway
)
http
.
Error
(
w
,
err
.
Error
(),
http
.
StatusBadGateway
)
return
return
}
}
util
.
ServeIndentedJSON
(
w
,
r
,
&
response
)
var
prettyJSON
bytes
.
Buffer
}
error
:=
json
.
Indent
(
&
prettyJSON
,
body
,
""
,
" "
)
if
error
!=
nil
{
// twitchGetFeaturedStreamsHandler handles the GetFeaturedStreams endpoint
func
twitchGetFeaturedStreamsHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
response
,
err
:=
client
.
GetFeaturedStreams
()
if
err
!=
nil
{
http
.
Error
(
w
,
err
.
Error
(),
http
.
StatusBadGateway
)
http
.
Error
(
w
,
err
.
Error
(),
http
.
StatusBadGateway
)
return
return
}
}
util
.
ServeIndentedJSON
(
w
,
r
,
&
response
)
w
.
Write
(
prettyJSON
.
Bytes
()
)
}
}
This diff is collapsed.
Click to expand it.
internal/static/index.html
+
1
−
1
View file @
25034b1e
...
@@ -22,7 +22,7 @@
...
@@ -22,7 +22,7 @@
<h2>
Loading...
</h2>
<h2>
Loading...
</h2>
<p>
<p>
<a
href=
"https://stream-server.h-da.io"
>
Documentation
</a>
·
<a
href=
"https://stream-server.h-da.io"
>
Documentation
</a>
·
<a
href=
"https://
stream-server.h-da.io
/reference"
>
Reference
</a>
<a
href=
"https://
dev.twitch.tv/docs/api
/reference"
>
Reference
</a>
</p>
</p>
<small>
<small>
<a
href=
"javascript:toggleFullscreen()"
>
toggle fullscreen
</a>
<a
href=
"javascript:toggleFullscreen()"
>
toggle fullscreen
</a>
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment