2024-10-29 23:08:11 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"embed"
|
|
|
|
"encoding/base64"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2024-10-30 09:33:03 +00:00
|
|
|
"time"
|
2024-10-29 23:08:11 +00:00
|
|
|
|
2024-10-30 09:24:41 +00:00
|
|
|
"git.1e99.eu/1e99/passed/middlewares"
|
2024-10-29 23:08:11 +00:00
|
|
|
"git.1e99.eu/1e99/passed/routes"
|
|
|
|
"git.1e99.eu/1e99/passed/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
//go:embed static/*
|
|
|
|
var embedFS embed.FS
|
|
|
|
|
|
|
|
func run() error {
|
|
|
|
storage, err := storage.NewStore()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-10-30 10:47:17 +00:00
|
|
|
go func() {
|
|
|
|
ticker := time.Tick(20 * time.Second)
|
|
|
|
for {
|
|
|
|
<-ticker
|
|
|
|
|
|
|
|
err := storage.ClearExpired()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to clear expired passwords: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Cleared expired passwords.")
|
|
|
|
}
|
|
|
|
}()
|
2024-10-29 23:21:25 +00:00
|
|
|
|
2024-10-29 23:08:11 +00:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.Handle("GET /", routes.ServeFiles(embedFS, "static"))
|
2024-10-30 09:33:03 +00:00
|
|
|
mux.Handle(
|
|
|
|
"POST /api/password",
|
|
|
|
middlewares.RateLimiter(
|
|
|
|
routes.CreatePassword(storage, 12*1024, base64.StdEncoding),
|
|
|
|
1*time.Minute,
|
|
|
|
5,
|
|
|
|
),
|
|
|
|
)
|
2024-10-29 23:08:11 +00:00
|
|
|
mux.Handle("GET /api/password/{id}", routes.GetPassword(storage, base64.StdEncoding))
|
2024-10-29 23:36:53 +00:00
|
|
|
mux.Handle("HEAD /api/password/{id}", routes.HasPassword(storage))
|
2024-10-29 23:08:11 +00:00
|
|
|
|
|
|
|
address := os.Getenv("PASSED_ADDRESS")
|
|
|
|
if address == "" {
|
2024-10-30 09:09:18 +00:00
|
|
|
log.Printf("No PASSED_ADDRESS provided, defaulting to \":3000\"")
|
2024-10-29 23:08:11 +00:00
|
|
|
address = ":3000"
|
|
|
|
}
|
|
|
|
|
2024-10-30 09:24:41 +00:00
|
|
|
err = http.ListenAndServe(address, middlewares.Logger(mux))
|
2024-10-29 23:08:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
err := run()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("%s", err)
|
|
|
|
}
|
|
|
|
}
|