Compare commits
No commits in common. "afcd407f1d158b40fc2a36e5b1d0b9fe38597069" and "ed7d58799083835f0e6c29331d69dedb045722c5" have entirely different histories.
afcd407f1d
...
ed7d587990
3 changed files with 4 additions and 57 deletions
13
main.go
13
main.go
|
@ -6,9 +6,7 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.1e99.eu/1e99/passed/middlewares"
|
|
||||||
"git.1e99.eu/1e99/passed/routes"
|
"git.1e99.eu/1e99/passed/routes"
|
||||||
"git.1e99.eu/1e99/passed/storage"
|
"git.1e99.eu/1e99/passed/storage"
|
||||||
)
|
)
|
||||||
|
@ -26,14 +24,7 @@ func run() error {
|
||||||
|
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
mux.Handle("GET /", routes.ServeFiles(embedFS, "static"))
|
mux.Handle("GET /", routes.ServeFiles(embedFS, "static"))
|
||||||
mux.Handle(
|
mux.Handle("POST /api/password", routes.CreatePassword(storage, 12*1024, base64.StdEncoding))
|
||||||
"POST /api/password",
|
|
||||||
middlewares.RateLimiter(
|
|
||||||
routes.CreatePassword(storage, 12*1024, base64.StdEncoding),
|
|
||||||
1*time.Minute,
|
|
||||||
5,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
mux.Handle("GET /api/password/{id}", routes.GetPassword(storage, base64.StdEncoding))
|
mux.Handle("GET /api/password/{id}", routes.GetPassword(storage, base64.StdEncoding))
|
||||||
mux.Handle("HEAD /api/password/{id}", routes.HasPassword(storage))
|
mux.Handle("HEAD /api/password/{id}", routes.HasPassword(storage))
|
||||||
|
|
||||||
|
@ -43,7 +34,7 @@ func run() error {
|
||||||
address = ":3000"
|
address = ":3000"
|
||||||
}
|
}
|
||||||
|
|
||||||
err = http.ListenAndServe(address, middlewares.Logger(mux))
|
err = http.ListenAndServe(address, routes.Logger(mux))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,44 +0,0 @@
|
||||||
package middlewares
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/http"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func RateLimiter(handler http.Handler, clearInterval time.Duration, maxRequests int) http.HandlerFunc {
|
|
||||||
requests := make(map[string]int)
|
|
||||||
lock := sync.Mutex{}
|
|
||||||
|
|
||||||
ticker := time.NewTicker(clearInterval)
|
|
||||||
go func() {
|
|
||||||
for {
|
|
||||||
<-ticker.C
|
|
||||||
|
|
||||||
lock.Lock()
|
|
||||||
clear(requests)
|
|
||||||
lock.Unlock()
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
return func(res http.ResponseWriter, req *http.Request) {
|
|
||||||
addr := req.RemoteAddr
|
|
||||||
|
|
||||||
lock.Lock()
|
|
||||||
count, found := requests[addr]
|
|
||||||
if !found {
|
|
||||||
count = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
count += 1
|
|
||||||
requests[addr] = count
|
|
||||||
lock.Unlock()
|
|
||||||
|
|
||||||
if count > maxRequests {
|
|
||||||
http.Error(res, "Too many requests", http.StatusTooManyRequests)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
handler.ServeHTTP(res, req)
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,4 +1,4 @@
|
||||||
package middlewares
|
package routes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
@ -7,7 +7,7 @@ import (
|
||||||
|
|
||||||
func Logger(handler http.Handler) http.HandlerFunc {
|
func Logger(handler http.Handler) http.HandlerFunc {
|
||||||
return func(res http.ResponseWriter, req *http.Request) {
|
return func(res http.ResponseWriter, req *http.Request) {
|
||||||
log.Printf("%-30s %-80s", req.RemoteAddr, req.URL.Path)
|
log.Printf("%-80s", req.URL.Path)
|
||||||
handler.ServeHTTP(res, req)
|
handler.ServeHTTP(res, req)
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue