-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
39 lines (33 loc) · 813 Bytes
/
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
package main
import (
"github.com/gorilla/mux"
"github.com/rs/cors"
"github.com/joho/godotenv"
"net/http"
"os"
"encoding/json"
"log"
"fmt"
"golyrics/controller"
)
func search(w http.ResponseWriter, r *http.Request){
query := r.URL.Query().Get("song")
fmt.Println(query)
response, err := json.Marshal(controller.SearchLyrics(query))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(response)
}
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
router := mux.NewRouter()
router.HandleFunc("/search", search).Methods("GET")
handler := cors.Default().Handler(router)
log.Fatal(http.ListenAndServe(":"+os.Getenv("PORT"), handler))
}