2022-01-08 20:31:10 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-05-16 06:15:09 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2023-07-13 23:31:20 +00:00
|
|
|
"hacklab.nilfm.cc/quartzgun/indentalUserDB"
|
|
|
|
"hacklab.nilfm.cc/quartzgun/middleware"
|
2024-11-28 16:54:35 +00:00
|
|
|
"hacklab.nilfm.cc/quartzgun/rateLimiter"
|
2023-07-13 23:31:20 +00:00
|
|
|
"hacklab.nilfm.cc/quartzgun/renderer"
|
|
|
|
"hacklab.nilfm.cc/quartzgun/router"
|
2022-05-16 06:15:09 +00:00
|
|
|
"html/template"
|
2022-01-08 20:31:10 +00:00
|
|
|
"net/http"
|
2022-05-16 06:15:09 +00:00
|
|
|
"testing"
|
2022-01-08 20:31:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func AddContent(next http.Handler) http.Handler {
|
2022-05-16 06:15:09 +00:00
|
|
|
handlerFunc := func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
if !req.Form.Has("Content") {
|
|
|
|
req.Form.Add("Content", "Yesssssss")
|
|
|
|
}
|
|
|
|
next.ServeHTTP(w, req)
|
|
|
|
}
|
|
|
|
return http.HandlerFunc(handlerFunc)
|
2022-01-08 20:31:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ApiSomething(next http.Handler) http.Handler {
|
2022-05-16 06:15:09 +00:00
|
|
|
handlerFunc := func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
*req = *req.WithContext(context.WithValue(req.Context(), "apiData", "something"))
|
|
|
|
next.ServeHTTP(w, req)
|
|
|
|
}
|
2022-01-08 20:31:10 +00:00
|
|
|
|
2022-05-16 06:15:09 +00:00
|
|
|
return http.HandlerFunc(handlerFunc)
|
2022-01-08 20:31:10 +00:00
|
|
|
}
|
|
|
|
|
2022-05-16 06:15:09 +00:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
udb := indentalUserDB.CreateIndentalUserDB("testData/userDB.ndtl")
|
|
|
|
udb.AddUser("nilix", "questing")
|
2022-08-03 02:46:34 +00:00
|
|
|
sesh, _ := udb.InitiateSession("nilix", "questing", 60)
|
2022-01-08 20:31:10 +00:00
|
|
|
|
2024-11-28 16:54:35 +00:00
|
|
|
bouncer := rateLimiter.IpRateLimiter{
|
|
|
|
map[string]*rateLimiter.RateLimitData{},
|
|
|
|
5,
|
|
|
|
2,
|
|
|
|
}
|
|
|
|
|
2022-05-16 06:15:09 +00:00
|
|
|
fmt.Printf("%s // %s\n", sesh, sesh)
|
|
|
|
rtr := &router.Router{
|
|
|
|
StaticPaths: map[string]string{
|
|
|
|
"/static": "testData/static",
|
|
|
|
},
|
|
|
|
Fallback: *template.Must(template.ParseFiles("testData/templates/error.html", "testData/templates/footer.html")),
|
|
|
|
}
|
2022-01-08 20:31:10 +00:00
|
|
|
|
2022-05-18 04:29:40 +00:00
|
|
|
rtr.Get("/login", renderer.Template(
|
|
|
|
"testData/templates/login.html"))
|
|
|
|
|
2022-08-03 02:46:34 +00:00
|
|
|
rtr.Post("/login", middleware.Authorize("/", udb, "/login?tryagain=1", 120))
|
2022-05-18 04:29:40 +00:00
|
|
|
|
2023-07-13 23:31:20 +00:00
|
|
|
rtr.Post("/provision", middleware.Provision(udb, 60))
|
|
|
|
rtr.Get("/protected", middleware.Validate(renderer.Template("testData/templates/test.html"), udb, map[string]string{}))
|
2022-08-03 03:17:53 +00:00
|
|
|
|
2022-05-18 04:29:40 +00:00
|
|
|
rtr.Get("/", middleware.Protected(
|
|
|
|
renderer.Template(
|
2022-05-21 03:36:54 +00:00
|
|
|
"testData/templates/test.html"), http.MethodGet, udb, "/login"))
|
2022-01-08 20:31:10 +00:00
|
|
|
|
2024-11-28 16:54:35 +00:00
|
|
|
rtr.Get("/json", middleware.Throttle(ApiSomething(renderer.JSON("apiData")), bouncer.RateLimit))
|
2022-01-08 20:31:10 +00:00
|
|
|
|
2022-05-16 06:15:09 +00:00
|
|
|
rtr.Get(`/thing/(?P<Thing>\w+)`, renderer.Template("testData/templates/paramTest.html"))
|
2022-01-08 20:31:10 +00:00
|
|
|
|
2022-05-16 06:15:09 +00:00
|
|
|
http.ListenAndServe(":8080", rtr)
|
2022-01-08 20:31:10 +00:00
|
|
|
}
|