-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
115 lines (107 loc) · 2.65 KB
/
main.go
File metadata and controls
115 lines (107 loc) · 2.65 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"fmt"
"html/template"
"io"
"log"
"net/http"
"os"
"os/exec"
"time"
)
func handleHome(w http.ResponseWriter, r *http.Request) {
t := template.Must(template.ParseFiles("htmls/main.html"))
if err := t.Execute(w, nil); err != nil {
log.Fatal(err)
}
}
func handleFile(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
err := r.ParseMultipartForm(10 << 20)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
file, fileHeader, err := r.FormFile("file")
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
defer file.Close()
filename := "./audioFiles/" + time.Now().String() + fileHeader.Filename
dst, err := os.Create(filename)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer dst.Close()
_, err = io.Copy(dst, file)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
cmd := exec.Command("python", "LoadAudio.py", "-f", filename)
output, err := cmd.CombinedOutput()
if err != nil {
http.Error(w, err.Error()+" "+string(output), http.StatusInternalServerError)
return
}
// if err := cmd.Start(); err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
// return
// }
// if err := cmd.Wait(); err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
// return
// }
w.Write([]byte("cargando audio..."))
}
func handlePrompt(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
prompt := r.FormValue("prompt")
if prompt == "" {
http.Error(w, "no hay prompt", http.StatusBadRequest)
return
}
response, err := http.Get("http://127.0.0.1:8090?prompt=" + prompt)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
body, err := io.ReadAll(response.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
response.Body.Close()
if response.StatusCode > 299 {
errstring := fmt.Sprintf(
"statuscode %d: body: %s",
response.StatusCode,
string(body),
)
http.Error(w, errstring, http.StatusInternalServerError)
return
}
w.Write(body)
}
func main() {
fs := http.FileServer(http.Dir("./static"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
http.HandleFunc("/audio", handleFile)
http.HandleFunc("/prompt", handlePrompt)
http.HandleFunc("/", handleHome)
port := ":8000"
log.Print("corriendo servidor en puerto " + port)
err := http.ListenAndServe(port, nil)
if err != nil {
log.Fatal(err)
}
}