Created
June 19, 2019 00:05
-
-
Save mattlockyer/e9dace9c581231dc9a724dfffb0760f9 to your computer and use it in GitHub Desktop.
YouTube Search with Go / GoLang Server. Client sends url query param "q" to server. Server searches YouTube API for videos matching q. Server decodes, does "anything", then responds to client with JSON.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<title>YouTube Search Example</title> | |
<meta name="description" content="YouTube Search Example"> | |
<meta name="author" content="Matt Lockyer"> | |
</head> | |
<body> | |
<input type="text" id="q" placeholder="Search YouTube" /> | |
<br /> | |
<button id="search"> | |
Search | |
</button> | |
<script> | |
const ge = (id) => document.getElementById(id) | |
window.onload = () => { | |
ge('search').onclick = async() => { | |
const q = ge('q').value | |
console.log("query", q) | |
const res = await fetch('http://localhost:8080/search?q=' + q, { | |
method: 'GET', mode: "cors", | |
}).then((res) => res.json()) | |
console.log(res) | |
} | |
} | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"net/http" | |
) | |
/******************************** | |
YouTube Search with Go / GoLang Server. | |
Client sends url query param "q" to server. | |
Server searches YouTube API for videos matching q. | |
Server decodes, does "anything", then responds to client with JSON. | |
********************************/ | |
var apiKey string = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" | |
type msi = map[string]interface{} | |
func search(w http.ResponseWriter, r *http.Request) { | |
// Read body | |
q, ok := r.URL.Query()["q"] | |
if !ok || len(q[0]) < 1 { | |
http.Error(w, "Url Param 'q' is missing", 500) | |
return | |
} | |
// Fetch response from YouTube | |
r2, err := http.Get("https://www.googleapis.com/youtube/v3/search?q=" + q[0] + | |
"&type=video&part=snippet&maxResults=20&key=" + apiKey) | |
if err != nil { | |
http.Error(w, err.Error(), 500) | |
return | |
} | |
defer r2.Body.Close() | |
var r3 msi | |
err = json.NewDecoder(r2.Body).Decode(&r3) | |
if err != nil { | |
http.Error(w, err.Error(), 500) | |
return | |
} | |
/******************************** | |
Do stuff with YouTube search results | |
********************************/ | |
println(r3) | |
// Return YouTube results | |
output, err := json.Marshal(r3) | |
if err != nil { | |
http.Error(w, err.Error(), 500) | |
return | |
} | |
w.Header().Set("content-type", "application/json") | |
w.Write(output) | |
} | |
func main() { | |
http.Handle("/", http.FileServer(http.Dir("./static"))) | |
http.HandleFunc("/search", search) | |
println("Listening on 8080") | |
http.ListenAndServe(":8080", nil) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment