2023-09-10 22:18:30 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2023-09-11 01:46:14 +02:00
|
|
|
"html/template"
|
|
|
|
"io"
|
2023-09-10 22:18:30 +02:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
2023-09-11 19:54:28 +02:00
|
|
|
|
2023-09-10 22:18:30 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
DEFAULT_HTTP_PORT = "8080"
|
|
|
|
DEFAULT_HTTPS_PORT = "4433"
|
|
|
|
)
|
|
|
|
|
2023-09-11 01:46:14 +02:00
|
|
|
type DownloadFormats struct {
|
|
|
|
VideoRes string
|
|
|
|
videoOnly bool
|
|
|
|
audioOnly bool
|
|
|
|
}
|
|
|
|
|
2023-09-10 22:18:30 +02:00
|
|
|
type apiMessageResponse struct {
|
|
|
|
Message string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Logger struct {
|
|
|
|
Handler http.Handler
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Logger) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
tNow := time.Now().UTC()
|
|
|
|
l.Handler.ServeHTTP(w, r)
|
|
|
|
methodString := l.getMethodLogString(r.Method)
|
|
|
|
log.Printf(" %s %s %s %v", r.RemoteAddr, methodString, r.URL, time.Since(tNow))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Logger) getMethodLogString(method string) string {
|
|
|
|
colorBlue := "\033[34m"
|
|
|
|
colorReset := "\033[0m"
|
2023-09-10 22:33:06 +02:00
|
|
|
colorRed := "\033[31m"
|
|
|
|
colorGreen := "\033[32m"
|
|
|
|
colorYellow := "\033[33m"
|
2023-09-10 22:18:30 +02:00
|
|
|
// colorPurple := "\033[35m"
|
|
|
|
// colorCyan := "\033[36m"
|
|
|
|
// colorWhite := "\033[37m"
|
2023-09-10 22:33:06 +02:00
|
|
|
switch method {
|
|
|
|
case "GET": return colorBlue + "GET" + colorReset
|
|
|
|
case "POST": return colorGreen + "POST" + colorReset
|
|
|
|
case "DELETE": return colorRed + "DELETE" + colorReset
|
|
|
|
case "PUT": return colorYellow + "PUT" + colorReset
|
|
|
|
default: return method
|
|
|
|
}
|
2023-09-10 22:18:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewLogger(handler http.Handler) *Logger {
|
|
|
|
return &Logger{Handler: handler}
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeJSONResponse(w http.ResponseWriter, s string) http.ResponseWriter {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
2023-09-11 01:46:14 +02:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2023-09-10 22:18:30 +02:00
|
|
|
jsonResp, err := json.Marshal(apiMessageResponse{Message: s})
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
_, err = w.Write(jsonResp)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
|
|
|
return w
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-09-11 20:44:41 +02:00
|
|
|
var templates *template.Template
|
|
|
|
|
|
|
|
// TODO: Change all this to have a unified context
|
2023-09-11 22:27:18 +02:00
|
|
|
type Context struct {
|
|
|
|
request *http.Request
|
|
|
|
Formats []DownloadFormats
|
|
|
|
AppURL string
|
|
|
|
StatusCode int
|
|
|
|
Err *error
|
|
|
|
IsTLS bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewContext(r *http.Request) *Context {
|
|
|
|
return &Context{
|
|
|
|
request: r,
|
|
|
|
StatusCode: 200,
|
|
|
|
Formats: []DownloadFormats{
|
|
|
|
{
|
|
|
|
VideoRes: "720p",
|
|
|
|
videoOnly: false,
|
|
|
|
audioOnly: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
AppURL: r.Host,
|
|
|
|
IsTLS: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-11 20:44:41 +02:00
|
|
|
var AppURL string = "http://localhost:8080"
|
|
|
|
type HTMLBaseData struct {
|
|
|
|
Formats []DownloadFormats
|
|
|
|
AppURL string
|
|
|
|
}
|
|
|
|
|
|
|
|
var appData HTMLBaseData = HTMLBaseData{
|
|
|
|
AppURL: AppURL,
|
|
|
|
}
|
2023-09-11 19:54:28 +02:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
|
|
|
log.Println("[ init ] Starting...")
|
2023-09-11 20:44:41 +02:00
|
|
|
templates = template.Must(template.ParseFS(TemplatesFS , "templates/*.html"))
|
2023-09-11 19:54:28 +02:00
|
|
|
|
|
|
|
}
|
2023-09-10 22:18:30 +02:00
|
|
|
func main() {
|
|
|
|
|
|
|
|
handler := http.NewServeMux()
|
2023-09-11 20:44:41 +02:00
|
|
|
handler.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(PublicFS))))
|
2023-09-10 22:18:30 +02:00
|
|
|
|
|
|
|
handler.HandleFunc(
|
|
|
|
"/download",
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
2023-09-11 22:27:18 +02:00
|
|
|
ctx := NewContext(r)
|
2023-09-11 01:46:14 +02:00
|
|
|
if r.Method != "POST" {
|
|
|
|
w.WriteHeader(400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
userURL := r.FormValue("URL")
|
2023-09-10 22:18:30 +02:00
|
|
|
if userURL == "" {
|
|
|
|
w = writeJSONResponse(w, "Provide URL as query")
|
|
|
|
return
|
|
|
|
}
|
2023-09-11 01:46:14 +02:00
|
|
|
// Get URL from convx
|
|
|
|
req, err := http.Get(fmt.Sprintf("http://localhost:80?url=%s", userURL))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err.Error())
|
2023-09-11 22:27:18 +02:00
|
|
|
ctx.StatusCode = 500
|
|
|
|
ctx.Err = &err
|
|
|
|
err = templates.ExecuteTemplate(w,"download-result.html", ctx)
|
2023-09-11 01:46:14 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if req.StatusCode != 200 {
|
|
|
|
log.Printf("Got %v from convx\n", req)
|
2023-09-11 22:27:18 +02:00
|
|
|
ctx.StatusCode = 500
|
|
|
|
ctx.Err = &err
|
|
|
|
err = templates.ExecuteTemplate(w,"download-result.html", ctx)
|
2023-09-11 01:46:14 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
body, err := io.ReadAll(req.Body)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error while reading convx response body. \n%v", err.Error())
|
2023-09-11 22:27:18 +02:00
|
|
|
ctx.StatusCode = 500
|
|
|
|
ctx.Err = &err
|
|
|
|
err = templates.ExecuteTemplate(w,"download-result.html", ctx)
|
2023-09-11 01:46:14 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
downloadURL := string(body)
|
|
|
|
log.Println("URL from convx", downloadURL)
|
|
|
|
|
2023-09-11 22:27:18 +02:00
|
|
|
err = templates.ExecuteTemplate(w,"download-result.html", ctx)
|
2023-09-11 01:46:14 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err.Error())
|
2023-09-11 22:27:18 +02:00
|
|
|
ctx.StatusCode = 500
|
|
|
|
ctx.Err = &err
|
|
|
|
err = templates.ExecuteTemplate(w,"download-result.html", ctx)
|
2023-09-11 01:46:14 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
},
|
2023-09-10 22:18:30 +02:00
|
|
|
)
|
2023-09-11 01:46:14 +02:00
|
|
|
handler.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
2023-09-11 22:27:18 +02:00
|
|
|
ctx := NewContext(r)
|
|
|
|
log.Println(ctx.AppURL)
|
2023-09-11 01:46:14 +02:00
|
|
|
formats := []DownloadFormats{}
|
|
|
|
formats = append(formats, DownloadFormats{
|
|
|
|
VideoRes: "720p",
|
|
|
|
audioOnly: false,
|
|
|
|
videoOnly: false,
|
|
|
|
})
|
2023-09-11 20:44:41 +02:00
|
|
|
appData.Formats = formats
|
2023-09-11 22:27:18 +02:00
|
|
|
err := templates.ExecuteTemplate(w, "download.html", ctx)
|
2023-09-11 01:46:14 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err.Error())
|
|
|
|
w.WriteHeader(500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
2023-09-10 22:18:30 +02:00
|
|
|
|
|
|
|
wrappedHandler := NewLogger(handler)
|
|
|
|
srv := http.Server{
|
2023-09-11 01:46:14 +02:00
|
|
|
ReadTimeout: 10 * time.Second,
|
2023-09-10 22:18:30 +02:00
|
|
|
WriteTimeout: 10 * time.Second,
|
|
|
|
Addr: ":" + DEFAULT_HTTP_PORT,
|
|
|
|
Handler: wrappedHandler,
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Starting HTTP on %s", DEFAULT_HTTP_PORT)
|
|
|
|
log.Fatalln(srv.ListenAndServe())
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|