-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
39 lines (32 loc) · 846 Bytes
/
server.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 (
"io/ioutil"
"net/http"
"os"
"os/user"
)
func main() {
http.HandleFunc("/save", saveSongList)
http.HandleFunc("/", handleAll)
http.ListenAndServe(":5000", nil)
}
func handleAll(w http.ResponseWriter, r *http.Request) {
body, _ := ioutil.ReadAll(r.Body)
response, _ := http.Get(string(body))
body2, _ := ioutil.ReadAll(response.Body)
w.Header().Set("content-type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Write(body2)
}
func saveSongList(w http.ResponseWriter, r *http.Request) {
body, _ := ioutil.ReadAll(r.Body)
usr, err := user.Current()
if err != nil {
panic(err)
}
if f, err := os.OpenFile(usr.HomeDir+"/Music/chordify-extension-backup.txt",
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644); err == nil {
defer f.Close()
f.WriteString(string(body) + "\n")
}
}