Skip to content

Commit

Permalink
Add the /v1/about API endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ironsmile committed Oct 20, 2024
1 parent 16fa0e0 commit 253d357
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ Authentication tokens can be acquired using the `/v1/login/token/` endpoint desc

### Endpoints

* [About](#about)
* [Search](#search)
* [Browse](#browse)
* [Play a Song](#play-a-song)
Expand All @@ -270,6 +271,25 @@ Authentication tokens can be acquired using the `/v1/login/token/` endpoint desc
* [Token Request](#token-request)
* [Register Token](#register-token)

### About

Query information about the server.

```sh
GET /v1/about
```

The returned response includes the server version. Example response:

```js
{
"server_version":"v1.5.4"
}
```

This information could be used by clients to know what APIs are supported by the
server.

### Search

One can do a search query at the following endpoint
Expand Down
2 changes: 2 additions & 0 deletions src/webserver/apiv1_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import "net/http"

// The following are URL Path endpoints for certain API calls.
const (
APIv1EndpointAbout = "/v1/about"
APIv1EndpointFile = "/v1/file/{fileID}"
APIv1EndpointAlbumArtwork = "/v1/album/{albumID}/artwork"
APIv1EndpointDownloadAlbum = "/v1/album/{albumID}"
Expand All @@ -18,6 +19,7 @@ const (
// APIv1Methods defines on which HTTP methods APIv1 endpoints will respond to.
// It is an uri_path => list of HTTP methods map.
var APIv1Methods map[string][]string = map[string][]string{
APIv1EndpointAbout: {http.MethodGet},
APIv1EndpointFile: {http.MethodGet},
APIv1EndpointDownloadAlbum: {http.MethodGet},
APIv1EndpointBrowse: {http.MethodGet},
Expand Down
38 changes: 38 additions & 0 deletions src/webserver/handler_about.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package webserver

import (
"encoding/json"
"fmt"
"net/http"

"github.com/ironsmile/euterpe/src/version"
)

type aboutHandler struct {
resp aboutResponse
}

// NewAboutHandler returns the HTTP handler which shows a JSON with information
// about the server.
func NewAboutHandler() http.Handler {
return &aboutHandler{
resp: aboutResponse{
ServerVersion: version.Version,
},
}
}

func (h *aboutHandler) ServeHTTP(writer http.ResponseWriter, req *http.Request) {
writer.Header().Add("Content-Type", "application/json; charset=utf-8")
enc := json.NewEncoder(writer)
if err := enc.Encode(h.resp); err != nil {
writer.Header().Set("Content-Type", "plain/text; charset=utf-8")
msg := fmt.Sprintf("Failed to encode JSON response: %s", err)
http.Error(writer, msg, http.StatusInternalServerError)
return
}
}

type aboutResponse struct {
ServerVersion string `json:"server_version"`
}
4 changes: 4 additions & 0 deletions src/webserver/webserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func (srv *Server) serveGoroutine() {
artistImageHandler := NewArtistImagesHandler(srv.library)
browseHandler := NewBrowseHandler(srv.library)
mediaFileHandler := NewFileHandler(srv.library)
aboutHandler := NewAboutHandler()
loginHandler := NewLoginHandler(srv.cfg.Authenticate)
loginTokenHandler := NewLoginTokenHandler(srv.cfg.Authenticate)
logoutHandler := NewLogoutHandler()
Expand All @@ -125,6 +126,9 @@ func (srv *Server) serveGoroutine() {
router.UseEncodedPath()

// API v1 methods.
router.Handle(APIv1EndpointAbout, aboutHandler).Methods(
APIv1Methods[APIv1EndpointAbout]...,
)
router.Handle(APIv1EndpointFile, mediaFileHandler).Methods(
APIv1Methods[APIv1EndpointFile]...,
)
Expand Down

0 comments on commit 253d357

Please sign in to comment.