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