Skip to content

Commit

Permalink
Merge pull request #849 from go-kivik/at
Browse files Browse the repository at this point in the history
Add _active_tasks to server
  • Loading branch information
flimzy authored Dec 17, 2023
2 parents 8aed23a + ff89076 commit a639f3f
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 33 deletions.
47 changes: 47 additions & 0 deletions x/server/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

package server

import (
"net/http"

"github.com/go-chi/chi/v5"
"gitlab.com/flimzy/httpe"
)

func (s *Server) db() httpe.HandlerWithError {
return httpe.HandlerWithErrorFunc(func(w http.ResponseWriter, r *http.Request) error {
db := chi.URLParam(r, "db")
stats, err := s.client.DB(db).Stats(r.Context())
if err != nil {
return err
}
return serveJSON(w, http.StatusOK, stats)
})
}

func (s *Server) dbExists() httpe.HandlerWithError {
return httpe.HandlerWithErrorFunc(func(w http.ResponseWriter, r *http.Request) error {
db := chi.URLParam(r, "db")
exists, err := s.client.DBExists(r.Context(), db)
if err != nil {
return err
}
if !exists {
w.WriteHeader(http.StatusNotFound)
return nil
}
w.WriteHeader(http.StatusOK)
return nil
})
}
29 changes: 1 addition & 28 deletions x/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (s *Server) routes(mux *chi.Mux) {
admin := auth.With(
httpe.ToMiddleware(adminRequired),
)
auth.Get("/_active_tasks", httpe.ToHandler(s.notImplemented()).ServeHTTP)
admin.Get("/_active_tasks", httpe.ToHandler(s.activeTasks()).ServeHTTP)
admin.Get("/_all_dbs", httpe.ToHandler(s.allDBs()).ServeHTTP)
auth.Get("/_dbs_info", httpe.ToHandler(s.notImplemented()).ServeHTTP)
auth.Post("/_dbs_info", httpe.ToHandler(s.notImplemented()).ServeHTTP)
Expand Down Expand Up @@ -262,33 +262,6 @@ func (s *Server) allDBs() httpe.HandlerWithError {
})
}

func (s *Server) db() httpe.HandlerWithError {
return httpe.HandlerWithErrorFunc(func(w http.ResponseWriter, r *http.Request) error {
db := chi.URLParam(r, "db")
stats, err := s.client.DB(db).Stats(r.Context())
if err != nil {
return err
}
return serveJSON(w, http.StatusOK, stats)
})
}

func (s *Server) dbExists() httpe.HandlerWithError {
return httpe.HandlerWithErrorFunc(func(w http.ResponseWriter, r *http.Request) error {
db := chi.URLParam(r, "db")
exists, err := s.client.DBExists(r.Context(), db)
if err != nil {
return err
}
if !exists {
w.WriteHeader(http.StatusNotFound)
return nil
}
w.WriteHeader(http.StatusOK)
return nil
})
}

func (s *Server) conf(ctx context.Context, section, key string, target interface{}) error {
value, err := s.config.Key(ctx, section, key)
if err != nil {
Expand Down
7 changes: 2 additions & 5 deletions x/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,8 @@ func TestServer(t *testing.T) {
method: http.MethodGet,
path: "/_active_tasks",
headers: map[string]string{"Authorization": basicAuth(userAdmin)},
wantStatus: http.StatusNotImplemented,
wantJSON: map[string]interface{}{
"error": "not_implemented",
"reason": "Feature not implemented",
},
wantStatus: http.StatusOK,
wantJSON: []interface{}{},
},
{
name: "all dbs",
Expand Down
9 changes: 9 additions & 0 deletions x/server/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,12 @@ func (s *Server) up() httpe.HandlerWithError {
})
})
}

// activeTasks returns a list of running tasks. For now it always returns an
// empty list, as this server doesn't support running asynchronous tasks. But it
// may be expanded in the future.
func (s *Server) activeTasks() httpe.HandlerWithError {
return httpe.HandlerWithErrorFunc(func(w http.ResponseWriter, _ *http.Request) error {
return serveJSON(w, http.StatusOK, []interface{}{})
})
}

0 comments on commit a639f3f

Please sign in to comment.