Created a centrilized operations handler

This allows all the requests to submit for an operation to be performed.
This will allow later versions to better handle various operations and
concurrency.
testing
Justin Reichardt 2022-01-11 14:55:09 -06:00
parent 5bc821274e
commit 26af26c73b
1 changed files with 25 additions and 5 deletions

View File

@ -39,27 +39,45 @@ const template = `
</body>
</html>
`
type ops struct{
URL string
Res chan string
}
var config struct{
saveLocation string
}
var opsc = make(chan ops)
func handleRequest (w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet{
log.Print("New Get request")
}else if r.Method == http.MethodPost {
r.ParseForm()
downloadVid(r)
go func() {
var o ops
o.URL = r.FormValue("URL")
opsc <- o
} ()
} else{
}
io.WriteString (w,template)
}
func downloadVid (r *http.Request) {
cmd := exec.Command("youtube-dl", r.FormValue("URL"))
cmd.Dir = config.saveLocation
log.Print(cmd.String())
cmd.Start()
}
func operationHandler(){
for true {
o := <- opsc
log.Print("Start Downloading: " + o.URL)
go func() {
cmd := exec.Command("youtube-dl", o.URL)
cmd.Dir = config.saveLocation
cmd.Start()
}()
}
log.Fatal("Exited operationsHandler")
}
func main () {
@ -69,6 +87,8 @@ func main () {
config.saveLocation = os.Getenv("VIDDOWNLOC")
}
go operationHandler()
handler := http.HandlerFunc(handleRequest)
http.Handle("/",handler)
log.Printf("Starting server on 8080")