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
 | 
			
		||||
 | 
			
		||||
// 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"
 | 
			
		||||
type HTMLBaseData struct {
 | 
			
		||||
    Formats []DownloadFormats
 | 
			
		||||
@ -105,6 +130,7 @@ func main() {
 | 
			
		||||
    handler.HandleFunc(
 | 
			
		||||
        "/download", 
 | 
			
		||||
        func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
            ctx := NewContext(r)
 | 
			
		||||
            if r.Method != "POST" {
 | 
			
		||||
                w.WriteHeader(400)
 | 
			
		||||
                return
 | 
			
		||||
@ -124,33 +150,42 @@ func main() {
 | 
			
		||||
 | 
			
		||||
            if err != nil {
 | 
			
		||||
                log.Println(err.Error())
 | 
			
		||||
                w.WriteHeader(500)
 | 
			
		||||
                ctx.StatusCode = 500
 | 
			
		||||
                ctx.Err = &err
 | 
			
		||||
                err = templates.ExecuteTemplate(w,"download-result.html", ctx)
 | 
			
		||||
                return
 | 
			
		||||
            }
 | 
			
		||||
            if req.StatusCode != 200 {
 | 
			
		||||
                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
 | 
			
		||||
            }
 | 
			
		||||
            body, err := io.ReadAll(req.Body)
 | 
			
		||||
            if err != nil {
 | 
			
		||||
                w.WriteHeader(500)
 | 
			
		||||
                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
 | 
			
		||||
            }
 | 
			
		||||
            downloadURL := string(body)
 | 
			
		||||
            log.Println("URL from convx", downloadURL)
 | 
			
		||||
            
 | 
			
		||||
            // Serve Button Template
 | 
			
		||||
            err = templates.ExecuteTemplate(w,"download-result.html", downloadURL)
 | 
			
		||||
            err = templates.ExecuteTemplate(w,"download-result.html", ctx)
 | 
			
		||||
            if err != nil {
 | 
			
		||||
                log.Println(err.Error())
 | 
			
		||||
                w.WriteHeader(500)
 | 
			
		||||
                ctx.StatusCode = 500
 | 
			
		||||
                ctx.Err = &err
 | 
			
		||||
                err = templates.ExecuteTemplate(w,"download-result.html", ctx)
 | 
			
		||||
                return
 | 
			
		||||
            }
 | 
			
		||||
        },
 | 
			
		||||
    )
 | 
			
		||||
    handler.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
        ctx := NewContext(r)
 | 
			
		||||
        log.Println(ctx.AppURL)
 | 
			
		||||
        formats := []DownloadFormats{}
 | 
			
		||||
        formats = append(formats, DownloadFormats{
 | 
			
		||||
            VideoRes: "720p",
 | 
			
		||||
@ -158,7 +193,7 @@ func main() {
 | 
			
		||||
            videoOnly: false,
 | 
			
		||||
        })
 | 
			
		||||
        appData.Formats = formats
 | 
			
		||||
        err := templates.ExecuteTemplate(w, "download.html", appData)
 | 
			
		||||
        err := templates.ExecuteTemplate(w, "download.html", ctx)
 | 
			
		||||
        if err != nil {
 | 
			
		||||
            log.Println(err.Error())
 | 
			
		||||
            w.WriteHeader(500)
 | 
			
		||||
 | 
			
		||||
@ -1 +1,10 @@
 | 
			
		||||
<div id="get-btn">
 | 
			
		||||
{{ if  eq .StatusCode 200 }}
 | 
			
		||||
<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>
 | 
			
		||||
        <meta charset="utf-8">
 | 
			
		||||
        <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>
 | 
			
		||||
        <title>YT Download</title>
 | 
			
		||||
    </head>
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user