Changed way to get pepe links
This commit is contained in:
parent
20e3925c4a
commit
27e2244842
|
@ -12,6 +12,8 @@ type application struct {
|
||||||
errorLog *log.Logger
|
errorLog *log.Logger
|
||||||
infoLog *log.Logger
|
infoLog *log.Logger
|
||||||
|
|
||||||
|
pepe_list []string
|
||||||
|
|
||||||
pepe_dir string
|
pepe_dir string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,44 +23,43 @@ func main() {
|
||||||
infoLog := log.New(os.Stdout, "INFO\t", log.Ldate|log.Ltime)
|
infoLog := log.New(os.Stdout, "INFO\t", log.Ldate|log.Ltime)
|
||||||
errorLog := log.New(os.Stderr, "ERROR\t", log.Ldate|log.Ltime|log.Lshortfile)
|
errorLog := log.New(os.Stderr, "ERROR\t", log.Ldate|log.Ltime|log.Lshortfile)
|
||||||
|
|
||||||
|
file, err := os.Open(pepe_dir)
|
||||||
|
if err != nil {
|
||||||
|
errorLog.Printf("Error opening pepe directory\n")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
pepe_list, err := file.Readdirnames(0)
|
||||||
|
if err != nil {
|
||||||
|
errorLog.Printf("Error reading pepe directory file names\n")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
file.Close()
|
||||||
|
|
||||||
app := &application{
|
app := &application{
|
||||||
infoLog: infoLog,
|
infoLog: infoLog,
|
||||||
errorLog: errorLog,
|
errorLog: errorLog,
|
||||||
pepe_dir: pepe_dir,
|
pepe_dir: pepe_dir,
|
||||||
|
pepe_list: pepe_list,
|
||||||
}
|
}
|
||||||
|
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
mux.HandleFunc("/pepe", app.sendPepe)
|
mux.HandleFunc("/pepe", app.sendPepe)
|
||||||
|
|
||||||
app.infoLog.Printf("Starting server at :4000\n")
|
app.infoLog.Printf("Starting server at :4000\n")
|
||||||
err := http.ListenAndServe(":4000", mux)
|
err = http.ListenAndServe(":4000", mux)
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *application) sendPepe(w http.ResponseWriter, r *http.Request) {
|
func (app *application) sendPepe(w http.ResponseWriter, r *http.Request) {
|
||||||
file, err := os.Open(app.pepe_dir)
|
|
||||||
if err != nil {
|
|
||||||
app.errorLog.Printf("Error opening pepe directory\n")
|
|
||||||
http.Error(w, "Internal Server Error", 500)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
names, err := file.Readdirnames(0)
|
|
||||||
if err != nil {
|
|
||||||
app.errorLog.Printf("Error reading pepe directory file names\n")
|
|
||||||
http.Error(w, "Internal Server Error", 500)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Random number generator
|
// Random number generator
|
||||||
s := rand.NewSource(time.Now().UnixMicro())
|
s := rand.NewSource(time.Now().UnixMicro())
|
||||||
rd := rand.New(s) // Init pseudorandom generator
|
rd := rand.New(s) // Init pseudorandom generator
|
||||||
number := rd.Intn(len(names))
|
number := rd.Intn(len(app.pepe_list))
|
||||||
|
|
||||||
baseURL := "https://cdn.nicecock.eu/pepe/1.00/"
|
baseURL := "https://cdn.nicecock.eu/pepe/1.00/"
|
||||||
URL := baseURL + names[number]
|
URL := baseURL + app.pepe_list[number]
|
||||||
|
|
||||||
w.Write([]byte(URL))
|
w.Write([]byte(URL))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue