2022-05-02 08:19:37 -07:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2022-05-17 13:35:26 -07:00
|
|
|
"net/http"
|
2022-05-02 08:19:37 -07:00
|
|
|
"os"
|
2022-05-25 15:41:06 -07:00
|
|
|
"strconv"
|
|
|
|
"time"
|
2022-05-02 08:19:37 -07:00
|
|
|
|
2023-06-03 07:18:47 -07:00
|
|
|
"codeberg.org/pronounscc/pronouns.cc/backend/db"
|
|
|
|
"codeberg.org/pronounscc/pronouns.cc/backend/server/auth"
|
|
|
|
"codeberg.org/pronounscc/pronouns.cc/backend/server/rate"
|
2022-05-02 08:19:37 -07:00
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/go-chi/chi/v5/middleware"
|
2023-04-13 14:33:48 -07:00
|
|
|
"github.com/go-chi/cors"
|
2022-05-25 15:41:06 -07:00
|
|
|
"github.com/go-chi/httprate"
|
2022-05-17 13:35:26 -07:00
|
|
|
"github.com/go-chi/render"
|
2022-05-02 08:19:37 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// Revision is the git commit, filled at build time
|
2023-03-15 02:04:48 -07:00
|
|
|
var (
|
|
|
|
Revision = "[unknown]"
|
|
|
|
Tag = "[unknown]"
|
|
|
|
)
|
2022-05-02 08:19:37 -07:00
|
|
|
|
2023-03-08 05:13:04 -08:00
|
|
|
// Repository is the URL of the git repository
|
2023-06-03 07:18:47 -07:00
|
|
|
const Repository = "https://codeberg.org/pronounscc/pronouns.cc"
|
2023-03-08 05:13:04 -08:00
|
|
|
|
2022-05-02 08:19:37 -07:00
|
|
|
type Server struct {
|
|
|
|
Router *chi.Mux
|
|
|
|
|
|
|
|
DB *db.DB
|
|
|
|
Auth *auth.Verifier
|
|
|
|
}
|
|
|
|
|
|
|
|
func New() (*Server, error) {
|
2022-09-20 04:02:48 -07:00
|
|
|
db, err := db.New()
|
2022-05-02 08:19:37 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
s := &Server{
|
|
|
|
Router: chi.NewMux(),
|
|
|
|
|
|
|
|
DB: db,
|
|
|
|
Auth: auth.New(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.Getenv("DEBUG") == "true" {
|
|
|
|
s.Router.Use(middleware.Logger)
|
|
|
|
}
|
|
|
|
s.Router.Use(middleware.Recoverer)
|
2023-04-13 14:33:48 -07:00
|
|
|
// add CORS
|
|
|
|
s.Router.Use(cors.Handler(cors.Options{
|
2023-05-24 15:33:54 -07:00
|
|
|
AllowedOrigins: []string{"https://*", "http://*"},
|
|
|
|
// Allow all methods normally used by the API
|
|
|
|
AllowedMethods: []string{"HEAD", "GET", "POST", "PATCH", "DELETE"},
|
2023-04-13 14:33:48 -07:00
|
|
|
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type"},
|
|
|
|
AllowCredentials: false,
|
|
|
|
MaxAge: 300,
|
|
|
|
}))
|
|
|
|
|
2022-05-02 08:19:37 -07:00
|
|
|
// enable authentication for all routes (but don't require it)
|
|
|
|
s.Router.Use(s.maybeAuth)
|
|
|
|
|
2022-05-25 15:41:06 -07:00
|
|
|
// rate limit handling
|
2023-02-24 06:53:35 -08:00
|
|
|
// - base is 120 req/minute (2/s)
|
2022-05-25 15:41:06 -07:00
|
|
|
// - keyed by Authorization header if valid token is provided, otherwise by IP
|
|
|
|
// - returns rate limit reset info in error
|
2023-02-24 06:53:35 -08:00
|
|
|
rateLimiter := rate.NewLimiter(120, time.Minute,
|
2022-05-25 15:41:06 -07:00
|
|
|
httprate.WithKeyFuncs(func(r *http.Request) (string, error) {
|
|
|
|
_, ok := ClaimsFromContext(r.Context())
|
|
|
|
if token := r.Header.Get("Authorization"); ok && token != "" {
|
|
|
|
return token, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ip, err := httprate.KeyByIP(r)
|
|
|
|
return ip, err
|
|
|
|
}),
|
|
|
|
httprate.WithLimitHandler(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
reset, _ := strconv.Atoi(w.Header().Get("X-RateLimit-Reset"))
|
|
|
|
|
|
|
|
render.Status(r, http.StatusTooManyRequests)
|
|
|
|
render.JSON(w, r, APIError{
|
|
|
|
Code: ErrTooManyRequests,
|
|
|
|
Message: errCodeMessages[ErrTooManyRequests],
|
|
|
|
RatelimitReset: &reset,
|
|
|
|
})
|
|
|
|
}),
|
2023-02-24 06:53:35 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
// set scopes
|
|
|
|
// users
|
|
|
|
rateLimiter.Scope("GET", "/users/*", 60)
|
2023-03-14 08:16:07 -07:00
|
|
|
rateLimiter.Scope("PATCH", "/users/@me", 10)
|
2023-02-24 06:53:35 -08:00
|
|
|
|
|
|
|
// members
|
|
|
|
rateLimiter.Scope("GET", "/users/*/members", 60)
|
|
|
|
rateLimiter.Scope("GET", "/users/*/members/*", 60)
|
|
|
|
|
2023-03-14 08:16:07 -07:00
|
|
|
rateLimiter.Scope("POST", "/members", 10)
|
2023-02-24 06:53:35 -08:00
|
|
|
rateLimiter.Scope("GET", "/members/*", 60)
|
2023-03-14 08:16:07 -07:00
|
|
|
rateLimiter.Scope("PATCH", "/members/*", 20)
|
2023-02-24 06:53:35 -08:00
|
|
|
rateLimiter.Scope("DELETE", "/members/*", 5)
|
|
|
|
|
|
|
|
// auth
|
|
|
|
rateLimiter.Scope("*", "/auth/*", 20)
|
|
|
|
rateLimiter.Scope("*", "/auth/tokens", 10)
|
|
|
|
rateLimiter.Scope("*", "/auth/invites", 10)
|
|
|
|
rateLimiter.Scope("POST", "/auth/discord/*", 10)
|
|
|
|
|
|
|
|
s.Router.Use(rateLimiter.Handler())
|
2022-05-25 15:41:06 -07:00
|
|
|
|
2023-04-17 14:44:21 -07:00
|
|
|
// increment the total requests counter whenever a request is made
|
|
|
|
s.Router.Use(func(next http.Handler) http.Handler {
|
|
|
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
s.DB.TotalRequests.Inc()
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
return http.HandlerFunc(fn)
|
|
|
|
})
|
|
|
|
|
2022-05-17 13:35:26 -07:00
|
|
|
// return an API error for not found + method not allowed
|
|
|
|
s.Router.NotFound(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
render.Status(r, errCodeStatuses[ErrNotFound])
|
|
|
|
render.JSON(w, r, APIError{
|
|
|
|
Code: ErrNotFound,
|
|
|
|
Message: errCodeMessages[ErrNotFound],
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
s.Router.MethodNotAllowed(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
render.Status(r, errCodeStatuses[ErrMethodNotAllowed])
|
|
|
|
render.JSON(w, r, APIError{
|
|
|
|
Code: ErrMethodNotAllowed,
|
|
|
|
Message: errCodeMessages[ErrMethodNotAllowed],
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-05-02 08:19:37 -07:00
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type ctxKey int
|
|
|
|
|
|
|
|
const (
|
|
|
|
ctxKeyClaims ctxKey = 1
|
|
|
|
)
|