feat: display error message
This commit is contained in:
		
							parent
							
								
									c8b706aee6
								
							
						
					
					
						commit
						84c85c98fe
					
				
							
								
								
									
										49
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										49
									
								
								main.go
									
									
									
									
									
								
							@ -81,6 +81,31 @@ func writeJSONResponse(w http.ResponseWriter, s string) http.ResponseWriter {
 | 
				
			|||||||
var templates *template.Template
 | 
					var templates *template.Template
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// TODO: Change all this to have a unified context
 | 
					// TODO: Change all this to have a unified context
 | 
				
			||||||
 | 
					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,
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var AppURL string = "http://localhost:8080"
 | 
					var AppURL string = "http://localhost:8080"
 | 
				
			||||||
type HTMLBaseData struct {
 | 
					type HTMLBaseData struct {
 | 
				
			||||||
    Formats []DownloadFormats
 | 
					    Formats []DownloadFormats
 | 
				
			||||||
@ -105,6 +130,7 @@ func main() {
 | 
				
			|||||||
    handler.HandleFunc(
 | 
					    handler.HandleFunc(
 | 
				
			||||||
        "/download", 
 | 
					        "/download", 
 | 
				
			||||||
        func(w http.ResponseWriter, r *http.Request) {
 | 
					        func(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
 | 
					            ctx := NewContext(r)
 | 
				
			||||||
            if r.Method != "POST" {
 | 
					            if r.Method != "POST" {
 | 
				
			||||||
                w.WriteHeader(400)
 | 
					                w.WriteHeader(400)
 | 
				
			||||||
                return
 | 
					                return
 | 
				
			||||||
@ -124,33 +150,42 @@ func main() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
            if err != nil {
 | 
					            if err != nil {
 | 
				
			||||||
                log.Println(err.Error())
 | 
					                log.Println(err.Error())
 | 
				
			||||||
                w.WriteHeader(500)
 | 
					                ctx.StatusCode = 500
 | 
				
			||||||
 | 
					                ctx.Err = &err
 | 
				
			||||||
 | 
					                err = templates.ExecuteTemplate(w,"download-result.html", ctx)
 | 
				
			||||||
                return
 | 
					                return
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            if req.StatusCode != 200 {
 | 
					            if req.StatusCode != 200 {
 | 
				
			||||||
                log.Printf("Got %v from convx\n", req)
 | 
					                log.Printf("Got %v from convx\n", req)
 | 
				
			||||||
                w.WriteHeader(500)
 | 
					                ctx.StatusCode = 500
 | 
				
			||||||
 | 
					                ctx.Err = &err
 | 
				
			||||||
 | 
					                err = templates.ExecuteTemplate(w,"download-result.html", ctx)
 | 
				
			||||||
                return
 | 
					                return
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            body, err := io.ReadAll(req.Body)
 | 
					            body, err := io.ReadAll(req.Body)
 | 
				
			||||||
            if err != nil {
 | 
					            if err != nil {
 | 
				
			||||||
                w.WriteHeader(500)
 | 
					 | 
				
			||||||
                log.Printf("Error while reading convx response body. \n%v", err.Error())
 | 
					                log.Printf("Error while reading convx response body. \n%v", err.Error())
 | 
				
			||||||
 | 
					                ctx.StatusCode = 500
 | 
				
			||||||
 | 
					                ctx.Err = &err
 | 
				
			||||||
 | 
					                err = templates.ExecuteTemplate(w,"download-result.html", ctx)
 | 
				
			||||||
                return
 | 
					                return
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            downloadURL := string(body)
 | 
					            downloadURL := string(body)
 | 
				
			||||||
            log.Println("URL from convx", downloadURL)
 | 
					            log.Println("URL from convx", downloadURL)
 | 
				
			||||||
            
 | 
					            
 | 
				
			||||||
            // Serve Button Template
 | 
					            err = templates.ExecuteTemplate(w,"download-result.html", ctx)
 | 
				
			||||||
            err = templates.ExecuteTemplate(w,"download-result.html", downloadURL)
 | 
					 | 
				
			||||||
            if err != nil {
 | 
					            if err != nil {
 | 
				
			||||||
                log.Println(err.Error())
 | 
					                log.Println(err.Error())
 | 
				
			||||||
                w.WriteHeader(500)
 | 
					                ctx.StatusCode = 500
 | 
				
			||||||
 | 
					                ctx.Err = &err
 | 
				
			||||||
 | 
					                err = templates.ExecuteTemplate(w,"download-result.html", ctx)
 | 
				
			||||||
                return
 | 
					                return
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
    handler.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
 | 
					    handler.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
 | 
					        ctx := NewContext(r)
 | 
				
			||||||
 | 
					        log.Println(ctx.AppURL)
 | 
				
			||||||
        formats := []DownloadFormats{}
 | 
					        formats := []DownloadFormats{}
 | 
				
			||||||
        formats = append(formats, DownloadFormats{
 | 
					        formats = append(formats, DownloadFormats{
 | 
				
			||||||
            VideoRes: "720p",
 | 
					            VideoRes: "720p",
 | 
				
			||||||
@ -158,7 +193,7 @@ func main() {
 | 
				
			|||||||
            videoOnly: false,
 | 
					            videoOnly: false,
 | 
				
			||||||
        })
 | 
					        })
 | 
				
			||||||
        appData.Formats = formats
 | 
					        appData.Formats = formats
 | 
				
			||||||
        err := templates.ExecuteTemplate(w, "download.html", appData)
 | 
					        err := templates.ExecuteTemplate(w, "download.html", ctx)
 | 
				
			||||||
        if err != nil {
 | 
					        if err != nil {
 | 
				
			||||||
            log.Println(err.Error())
 | 
					            log.Println(err.Error())
 | 
				
			||||||
            w.WriteHeader(500)
 | 
					            w.WriteHeader(500)
 | 
				
			||||||
 | 
				
			|||||||
@ -1 +1,10 @@
 | 
				
			|||||||
 | 
					<div id="get-btn">
 | 
				
			||||||
 | 
					{{ if  eq .StatusCode 200 }}
 | 
				
			||||||
<a href="{{ . }}" rel="" download target="_blank">Download</a>
 | 
					<a href="{{ . }}" rel="" download target="_blank">Download</a>
 | 
				
			||||||
 | 
					{{ else }}
 | 
				
			||||||
 | 
					<button>Try Again
 | 
				
			||||||
 | 
					    <!-- <a class="htmx-indicator" href="#" aria-busy="true"></a> -->
 | 
				
			||||||
 | 
					</button>
 | 
				
			||||||
 | 
					<mark>Internal Error. Try Again later</mark>
 | 
				
			||||||
 | 
					{{ end }}
 | 
				
			||||||
 | 
					</div>
 | 
				
			||||||
 | 
				
			|||||||
@ -3,7 +3,7 @@
 | 
				
			|||||||
    <head>
 | 
					    <head>
 | 
				
			||||||
        <meta charset="utf-8">
 | 
					        <meta charset="utf-8">
 | 
				
			||||||
        <meta name="viewport" content="width=device-width, initial-scale=1">
 | 
					        <meta name="viewport" content="width=device-width, initial-scale=1">
 | 
				
			||||||
        <link rel="stylesheet" href="{{ .AppURL }}/static/static/css/pico.min.css">
 | 
					        <link rel="stylesheet" href="{{ if .IsTLS }}https{{ else }}http{{ end }}://{{ .AppURL }}/static/static/css/pico.min.css">
 | 
				
			||||||
        <script src="https://unpkg.com/htmx.org@1.9.5"></script>
 | 
					        <script src="https://unpkg.com/htmx.org@1.9.5"></script>
 | 
				
			||||||
        <title>YT Download</title>
 | 
					        <title>YT Download</title>
 | 
				
			||||||
    </head>
 | 
					    </head>
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user