Add rate limiter

This commit is contained in:
NielsAD
2018-09-11 14:04:51 +02:00
parent 645787622c
commit c47e76cd3d
4 changed files with 21 additions and 10 deletions

6
.gitmodules vendored
View File

@@ -7,6 +7,6 @@
[submodule "vendor/github.com/karrick/godirwalk"]
path = vendor/github.com/karrick/godirwalk
url = https://github.com/karrick/godirwalk.git
[submodule "github.com/mattn/go-sqlite3"]
path = github.com/mattn/go-sqlite3
url = https://github.com/mattn/go-sqlite3.git
[submodule "vendor/github.com/ulule/limiter"]
path = vendor/github.com/ulule/limiter
url = https://github.com/ulule/limiter.git

23
main.go
View File

@@ -18,14 +18,18 @@ import (
"time"
_ "github.com/mattn/go-sqlite3"
"github.com/ulule/limiter"
"github.com/ulule/limiter/drivers/middleware/stdlib"
"github.com/ulule/limiter/drivers/store/memory"
)
var (
addr = flag.String("a", ":80", "TCP network address to listen for connections")
db = flag.String("d", ":memory:", "Database location")
dir = flag.String("r", ".", "Root directory to serve")
refresh = flag.String("i", "1h", "Refresh interval")
cached = flag.Bool("cached", false, "Serve everything from cache (rather than search/recursive queries only)")
addr = flag.String("a", ":80", "TCP network address to listen for connections")
db = flag.String("d", ":memory:", "Database location")
dir = flag.String("r", ".", "Root directory to serve")
refresh = flag.String("i", "1h", "Refresh interval")
forwarded = flag.Bool("forwarded", false, "Trust X-Real-IP and X-Forwarded-For headers")
cached = flag.Bool("cached", false, "Serve everything from cache (rather than search/recursive queries only)")
)
var logOut = log.New(os.Stdout, "", 0)
@@ -78,9 +82,14 @@ func main() {
}
})
limit := stdlib.NewMiddleware(
limiter.New(memory.NewStore(), limiter.Rate{Period: 1 * time.Second, Limit: 5}),
stdlib.WithForwardHeader(*forwarded),
)
srv := &http.Server{Addr: *addr}
http.Handle("/idx/", logRequest(http.StripPrefix("/idx/", fs)))
http.Handle("/dl/", logRequest(http.StripPrefix("/dl/", nodir(http.FileServer(http.Dir(fs.Root))))))
http.Handle("/idx/", limit.Handler(logRequest(http.StripPrefix("/idx/", fs))))
http.Handle("/dl/", limit.Handler(logRequest(http.StripPrefix("/dl/", nodir(http.FileServer(http.Dir(fs.Root)))))))
http.Handle("/sitemap.txt", http.HandlerFunc(fs.Sitemap))
http.Handle("/", pub)

1
vendor/github.com/pkg/errors generated vendored Submodule

1
vendor/github.com/ulule/limiter generated vendored Submodule