forked from TheTipo01/restRoberto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
72 lines (56 loc) · 1.32 KB
/
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
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
package main
import (
"fmt"
libroberto "github.com/TheTipo01/libRoberto"
"github.com/bwmarrin/lit"
"github.com/gorilla/mux"
"github.com/kkyr/fig"
"net/http"
"strings"
"time"
)
const (
audioExtension = ".mp3"
)
var (
token = make(map[string]bool)
)
func init() {
var cfg config
lit.LogLevel = lit.LogError
err := fig.Load(&cfg, fig.File("config.yml"))
if err != nil {
lit.Error(err.Error())
return
}
// Set lit.LogLevel to the given value
switch strings.ToLower(cfg.LogLevel) {
case "logwarning", "warning":
lit.LogLevel = lit.LogWarning
case "loginformational", "informational":
lit.LogLevel = lit.LogInformational
case "logdebug", "debug":
lit.LogLevel = lit.LogDebug
}
// Loads tokens
for _, t := range cfg.Token {
token[t] = true
}
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/audio", audio).Methods("GET")
r.PathPrefix("/temp/").Handler(http.StripPrefix("/temp/", http.FileServer(http.Dir("./temp"))))
http.Handle("/", r)
_ = http.ListenAndServe(":8087", nil)
}
func audio(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
if !token[query.Get("token")] {
w.WriteHeader(http.StatusForbidden)
return
}
uuid := libroberto.GenAudio(query.Get("text"), "mp3", time.Second*30)
w.WriteHeader(http.StatusAccepted)
_, _ = fmt.Fprintf(w, "/temp/"+uuid+audioExtension)
}