forked from mirrors/pronouns.cc
feat(backend): add /api/v1/meta endpoint
This commit is contained in:
parent
1687f04410
commit
8c187d0fb3
3 changed files with 55 additions and 0 deletions
|
@ -4,6 +4,7 @@ import (
|
|||
"codeberg.org/u1f320/pronouns.cc/backend/routes/auth"
|
||||
"codeberg.org/u1f320/pronouns.cc/backend/routes/bot"
|
||||
"codeberg.org/u1f320/pronouns.cc/backend/routes/member"
|
||||
"codeberg.org/u1f320/pronouns.cc/backend/routes/meta"
|
||||
"codeberg.org/u1f320/pronouns.cc/backend/routes/user"
|
||||
"codeberg.org/u1f320/pronouns.cc/backend/server"
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
@ -18,5 +19,6 @@ func mountRoutes(s *server.Server) {
|
|||
user.Mount(s, r)
|
||||
member.Mount(s, r)
|
||||
bot.Mount(s, r)
|
||||
meta.Mount(s, r)
|
||||
})
|
||||
}
|
||||
|
|
50
backend/routes/meta/meta.go
Normal file
50
backend/routes/meta/meta.go
Normal file
|
@ -0,0 +1,50 @@
|
|||
package meta
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"codeberg.org/u1f320/pronouns.cc/backend/server"
|
||||
"emperror.dev/errors"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/render"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
*server.Server
|
||||
}
|
||||
|
||||
func Mount(srv *server.Server, r chi.Router) {
|
||||
s := &Server{Server: srv}
|
||||
|
||||
r.Get("/meta", server.WrapHandler(s.meta))
|
||||
}
|
||||
|
||||
type MetaResponse struct {
|
||||
GitRepository string `json:"git_repository"`
|
||||
GitCommit string `json:"git_commit"`
|
||||
Users int64 `json:"users"`
|
||||
Members int64 `json:"members"`
|
||||
}
|
||||
|
||||
func (s *Server) meta(w http.ResponseWriter, r *http.Request) error {
|
||||
ctx := r.Context()
|
||||
|
||||
var numUsers, numMembers int64
|
||||
err := s.DB.QueryRow(ctx, "SELECT COUNT(*) FROM users").Scan(&numUsers)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "querying user count")
|
||||
}
|
||||
|
||||
err = s.DB.QueryRow(ctx, "SELECT COUNT(*) FROM members").Scan(&numMembers)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "querying user count")
|
||||
}
|
||||
|
||||
render.JSON(w, r, MetaResponse{
|
||||
GitRepository: server.Repository,
|
||||
GitCommit: server.Revision,
|
||||
Users: numUsers,
|
||||
Members: numMembers,
|
||||
})
|
||||
return nil
|
||||
}
|
|
@ -18,6 +18,9 @@ import (
|
|||
// Revision is the git commit, filled at build time
|
||||
var Revision = "[unknown]"
|
||||
|
||||
// Repository is the URL of the git repository
|
||||
const Repository = "https://codeberg.org/u1f320/pronouns.cc"
|
||||
|
||||
type Server struct {
|
||||
Router *chi.Mux
|
||||
|
||||
|
|
Loading…
Reference in a new issue