pepe server
This commit is contained in:
parent
082eb82659
commit
eafaf48233
|
@ -0,0 +1,13 @@
|
|||
# syntax=docker/dockerfile:1
|
||||
FROM golang:1.17 AS builder
|
||||
WORKDIR /go/src/quenten.nl/pepeservice
|
||||
|
||||
COPY . ./
|
||||
RUN go mod download
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app ./
|
||||
|
||||
FROM alpine:latest
|
||||
RUN apk --no-cache add ca-certificates
|
||||
WORKDIR /app
|
||||
COPY --from=builder /go/src/quenten.nl/pepeservice/app .
|
||||
CMD ["./app"]
|
|
@ -0,0 +1,3 @@
|
|||
module quenten.nl/pepeservice
|
||||
|
||||
go 1.17
|
|
@ -0,0 +1,64 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
type application struct {
|
||||
errorLog *log.Logger
|
||||
infoLog *log.Logger
|
||||
|
||||
pepe_dir string
|
||||
}
|
||||
|
||||
func main() {
|
||||
pepe_dir := os.Getenv("PEPE_DIR")
|
||||
|
||||
infoLog := log.New(os.Stdout, "INFO\t", log.Ldate|log.Ltime)
|
||||
errorLog := log.New(os.Stderr, "ERROR\t", log.Ldate|log.Ltime|log.Lshortfile)
|
||||
|
||||
app := &application{
|
||||
infoLog: infoLog,
|
||||
errorLog: errorLog,
|
||||
pepe_dir: pepe_dir,
|
||||
}
|
||||
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/pepe", app.sendPepe)
|
||||
|
||||
app.infoLog.Printf("Starting server at :4000\n")
|
||||
err := http.ListenAndServe(":4000", mux)
|
||||
log.Fatal(err)
|
||||
|
||||
}
|
||||
|
||||
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
|
||||
s := rand.NewSource(time.Now().UnixMicro())
|
||||
rd := rand.New(s) // Init pseudorandom generator
|
||||
number := rd.Intn(len(names))
|
||||
|
||||
baseURL := "https://cdn.nicecock.eu/pepe/1.00/"
|
||||
URL := baseURL + names[number]
|
||||
|
||||
w.Write([]byte(URL))
|
||||
}
|
Loading…
Reference in New Issue