Added reload function

This commit is contained in:
DutchEllie 2021-09-23 13:30:30 +02:00
parent 6acad26f07
commit 04a5024064
1 changed files with 19 additions and 0 deletions

View File

@ -45,6 +45,7 @@ func main() {
mux := http.NewServeMux()
mux.HandleFunc("/pepe", app.sendPepe)
mux.HandleFunc("/reload", app.reloadList)
app.infoLog.Printf("Starting server at :4000\n")
err = http.ListenAndServe(":4000", mux)
@ -63,3 +64,21 @@ func (app *application) sendPepe(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(URL))
}
func (app *application) reloadList(w http.ResponseWriter, r *http.Request) {
file, err := os.Open(app.pepe_dir)
if err != nil {
app.errorLog.Printf("Error opening pepe directory\n")
return
}
defer file.Close()
pepe_list, err := file.Readdirnames(0)
if err != nil {
app.errorLog.Printf("Error reading pepe directory file names\n")
return
}
app.pepe_list = pepe_list
w.WriteHeader(http.StatusOK)
w.Write([]byte("200 - Reloaded the list of pepes"))
}