refactor: move route handlers to named functions

This commit is contained in:
HeshamTB 2023-09-16 05:09:44 +03:00
parent e9aafd0cea
commit 36b012ac92

127
main.go
View File

@ -146,57 +146,8 @@ func handleToAudio(ctx *Context, w http.ResponseWriter) http.ResponseWriter {
return w return w
} }
func init() {
log.Println("[ init ] Starting...") func handleDownload(w http.ResponseWriter, r *http.Request) {
templates = template.Must(template.ParseFS(TemplatesFS , "templates/*.html"))
log.Println("[ init ] Templates Loaded")
}
func main() {
updater, err := apkupdater.InitPkgUpdater()
if err != nil {
log.Println("Could not init Package Updater!\n", err.Error())
}
updater.Run()
handler := http.NewServeMux()
handler.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(PublicFS))))
handler.Handle("/assets/", http.FileServer(http.FS(AssetsFS)))
handler.HandleFunc(
"/robots.txt",
func(w http.ResponseWriter, r *http.Request) {
robotsFile, err := RobotsFS.ReadFile("static/robots.txt")
if err != nil {
log.Println(err.Error())
w.WriteHeader(500)
return
}
w.Write(robotsFile)
},
)
handler.HandleFunc(
"/sitemap.txt",
func(w http.ResponseWriter, r *http.Request) {
sitemapFile, err := AssetsFS.ReadFile("assets/sitemap.txt")
if err != nil {
log.Println(err.Error())
w.WriteHeader(500)
return
}
w.Write(sitemapFile)
},
)
handler.HandleFunc(
"/download",
func(w http.ResponseWriter, r *http.Request) {
ctx := NewContext(r) ctx := NewContext(r)
if r.Method != "POST" { if r.Method != "POST" {
w.WriteHeader(400) w.WriteHeader(400)
@ -247,12 +198,9 @@ func main() {
err = templates.ExecuteTemplate(w,"download-result.html", ctx) err = templates.ExecuteTemplate(w,"download-result.html", ctx)
return return
} }
}, }
)
handler.HandleFunc( func handleDirectDownload(w http.ResponseWriter, r *http.Request) {
"/download-direct",
func(w http.ResponseWriter, r *http.Request) {
ctx := NewContext(r) ctx := NewContext(r)
if r.Method != "GET" { if r.Method != "GET" {
@ -324,11 +272,9 @@ func main() {
log.Println(err.Error()) log.Println(err.Error())
} }
log.Printf("Copied %d bytes", n) log.Printf("Copied %d bytes", n)
}, }
)
func handleRoot(w http.ResponseWriter, r *http.Request) {
handler.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
ctx := NewContext(r) ctx := NewContext(r)
formats := []DownloadFormats{} formats := []DownloadFormats{}
formats = append(formats, DownloadFormats{ formats = append(formats, DownloadFormats{
@ -342,10 +288,9 @@ func main() {
w.WriteHeader(500) w.WriteHeader(500)
return return
} }
}) }
handler.HandleFunc("/valid-link", func(w http.ResponseWriter, r *http.Request) {
func handleValidLink(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" { if r.Method != "POST" {
w.WriteHeader(400) w.WriteHeader(400)
return return
@ -366,7 +311,63 @@ func main() {
} }
templates.ExecuteTemplate(w, "url-validation.html", ctx) templates.ExecuteTemplate(w, "url-validation.html", ctx)
}) }
func init() {
log.Println("[ init ] Starting...")
templates = template.Must(template.ParseFS(TemplatesFS , "templates/*.html"))
log.Println("[ init ] Templates Loaded")
}
func main() {
updater, err := apkupdater.InitPkgUpdater()
if err != nil {
log.Println("Could not init Package Updater!\n", err.Error())
}
updater.Run()
handler := http.NewServeMux()
handler.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(PublicFS))))
handler.Handle("/assets/", http.FileServer(http.FS(AssetsFS)))
handler.HandleFunc(
"/robots.txt",
func(w http.ResponseWriter, r *http.Request) {
robotsFile, err := RobotsFS.ReadFile("static/robots.txt")
if err != nil {
log.Println(err.Error())
w.WriteHeader(500)
return
}
w.Write(robotsFile)
},
)
handler.HandleFunc(
"/sitemap.txt",
func(w http.ResponseWriter, r *http.Request) {
sitemapFile, err := AssetsFS.ReadFile("assets/sitemap.txt")
if err != nil {
log.Println(err.Error())
w.WriteHeader(500)
return
}
w.Write(sitemapFile)
},
)
handler.HandleFunc("/", handleRoot)
handler.HandleFunc("/download", handleDownload)
handler.HandleFunc("/download-direct", handleDirectDownload)
handler.HandleFunc("/valid-link", handleValidLink)
wrappedHandler := NewLogger(handler) wrappedHandler := NewLogger(handler)
srv := http.Server{ srv := http.Server{