Move list of commands from WS initial payload to /commands REST endpoint
This commit is contained in:
parent
ad65c643ad
commit
43a5693ee5
@ -24,6 +24,21 @@
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"title": "Commands",
|
||||
"parameters": {},
|
||||
"entrypoint": "commands",
|
||||
"path": "/commands",
|
||||
"authentication": [],
|
||||
"apis": [
|
||||
{
|
||||
"name": "list",
|
||||
"path": "/",
|
||||
"method": "GET",
|
||||
"title": "List of available commands"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"title": "Channels",
|
||||
"description": "A channel is a representation of a sequence of messages. It has meta data like channel subject. Channels may be public, private or group.",
|
||||
|
||||
18
api/messaging/spec/commands.json
Normal file
18
api/messaging/spec/commands.json
Normal file
@ -0,0 +1,18 @@
|
||||
{
|
||||
"Title": "Commands",
|
||||
"Interface": "Commands",
|
||||
"Struct": null,
|
||||
"Parameters": {},
|
||||
"Protocol": "",
|
||||
"Authentication": [],
|
||||
"Path": "/commands",
|
||||
"APIs": [
|
||||
{
|
||||
"Name": "list",
|
||||
"Method": "GET",
|
||||
"Title": "List of available commands",
|
||||
"Path": "/",
|
||||
"Parameters": null
|
||||
}
|
||||
]
|
||||
}
|
||||
@ -251,6 +251,30 @@ A channel is a representation of a sequence of messages. It has meta data like c
|
||||
|
||||
|
||||
|
||||
# Commands
|
||||
|
||||
| Method | Endpoint | Purpose |
|
||||
| ------ | -------- | ------- |
|
||||
| `GET` | `/commands/` | List of available commands |
|
||||
|
||||
## List of available commands
|
||||
|
||||
#### Method
|
||||
|
||||
| URI | Protocol | Method | Authentication |
|
||||
| --- | -------- | ------ | -------------- |
|
||||
| `/commands/` | HTTP/S | GET | |
|
||||
|
||||
#### Request parameters
|
||||
|
||||
| Parameter | Type | Method | Description | Default | Required? |
|
||||
| --------- | ---- | ------ | ----------- | ------- | --------- |
|
||||
|
||||
---
|
||||
|
||||
|
||||
|
||||
|
||||
# Messages
|
||||
|
||||
Messages represent individual messages in the chat system. Messages are typed, indicating the event which triggered the message.
|
||||
|
||||
31
messaging/rest/commands.go
Normal file
31
messaging/rest/commands.go
Normal file
@ -0,0 +1,31 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/crusttech/crust/messaging/rest/request"
|
||||
"github.com/crusttech/crust/messaging/types"
|
||||
)
|
||||
|
||||
var _ = errors.Wrap
|
||||
|
||||
type Commands struct {
|
||||
// xxx service.XXXService
|
||||
}
|
||||
|
||||
func (Commands) New() *Commands {
|
||||
return &Commands{}
|
||||
}
|
||||
|
||||
func (ctrl *Commands) List(ctx context.Context, r *request.CommandsList) (interface{}, error) {
|
||||
return types.CommandSet{
|
||||
&types.Command{
|
||||
Name: "echo",
|
||||
Description: "It does exactly what it says on the tin"},
|
||||
&types.Command{
|
||||
Name: "shrug",
|
||||
Description: "It does exactly what it says on the tin"},
|
||||
}, nil
|
||||
}
|
||||
56
messaging/rest/handlers/commands.go
Normal file
56
messaging/rest/handlers/commands.go
Normal file
@ -0,0 +1,56 @@
|
||||
package handlers
|
||||
|
||||
/*
|
||||
Hello! This file is auto-generated from `docs/src/spec.json`.
|
||||
|
||||
For development:
|
||||
In order to update the generated files, edit this file under the location,
|
||||
add your struct fields, imports, API definitions and whatever you want, and:
|
||||
|
||||
1. run [spec](https://github.com/titpetric/spec) in the same folder,
|
||||
2. run `./_gen.php` in this folder.
|
||||
|
||||
You may edit `commands.go`, `commands.util.go` or `commands_test.go` to
|
||||
implement your API calls, helper functions and tests. The file `commands.go`
|
||||
is only generated the first time, and will not be overwritten if it exists.
|
||||
*/
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/titpetric/factory/resputil"
|
||||
|
||||
"github.com/crusttech/crust/messaging/rest/request"
|
||||
)
|
||||
|
||||
// Internal API interface
|
||||
type CommandsAPI interface {
|
||||
List(context.Context, *request.CommandsList) (interface{}, error)
|
||||
}
|
||||
|
||||
// HTTP API interface
|
||||
type Commands struct {
|
||||
List func(http.ResponseWriter, *http.Request)
|
||||
}
|
||||
|
||||
func NewCommands(ch CommandsAPI) *Commands {
|
||||
return &Commands{
|
||||
List: func(w http.ResponseWriter, r *http.Request) {
|
||||
defer r.Body.Close()
|
||||
params := request.NewCommandsList()
|
||||
resputil.JSON(w, params.Fill(r), func() (interface{}, error) {
|
||||
return ch.List(r.Context(), params)
|
||||
})
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (ch *Commands) MountRoutes(r chi.Router, middlewares ...func(http.Handler) http.Handler) {
|
||||
r.Group(func(r chi.Router) {
|
||||
r.Use(middlewares...)
|
||||
r.Get("/commands/", ch.List)
|
||||
})
|
||||
}
|
||||
71
messaging/rest/request/commands.go
Normal file
71
messaging/rest/request/commands.go
Normal file
@ -0,0 +1,71 @@
|
||||
package request
|
||||
|
||||
/*
|
||||
Hello! This file is auto-generated from `docs/src/spec.json`.
|
||||
|
||||
For development:
|
||||
In order to update the generated files, edit this file under the location,
|
||||
add your struct fields, imports, API definitions and whatever you want, and:
|
||||
|
||||
1. run [spec](https://github.com/titpetric/spec) in the same folder,
|
||||
2. run `./_gen.php` in this folder.
|
||||
|
||||
You may edit `commands.go`, `commands.util.go` or `commands_test.go` to
|
||||
implement your API calls, helper functions and tests. The file `commands.go`
|
||||
is only generated the first time, and will not be overwritten if it exists.
|
||||
*/
|
||||
|
||||
import (
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"encoding/json"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
var _ = chi.URLParam
|
||||
var _ = multipart.FileHeader{}
|
||||
|
||||
// Commands list request parameters
|
||||
type CommandsList struct {
|
||||
}
|
||||
|
||||
func NewCommandsList() *CommandsList {
|
||||
return &CommandsList{}
|
||||
}
|
||||
|
||||
func (cReq *CommandsList) Fill(r *http.Request) (err error) {
|
||||
if strings.ToLower(r.Header.Get("content-type")) == "application/json" {
|
||||
err = json.NewDecoder(r.Body).Decode(cReq)
|
||||
|
||||
switch {
|
||||
case err == io.EOF:
|
||||
err = nil
|
||||
case err != nil:
|
||||
return errors.Wrap(err, "error parsing http request body")
|
||||
}
|
||||
}
|
||||
|
||||
if err = r.ParseForm(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
get := map[string]string{}
|
||||
post := map[string]string{}
|
||||
urlQuery := r.URL.Query()
|
||||
for name, param := range urlQuery {
|
||||
get[name] = string(param[0])
|
||||
}
|
||||
postVars := r.Form
|
||||
for name, param := range postVars {
|
||||
post[name] = string(param[0])
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
var _ RequestFiller = NewCommandsList()
|
||||
@ -26,6 +26,7 @@ func MountRoutes() func(chi.Router) {
|
||||
handlers.NewChannel(Channel{}.New()).MountRoutes(r)
|
||||
handlers.NewMessage(Message{}.New()).MountRoutes(r)
|
||||
handlers.NewSearch(Search{}.New()).MountRoutes(r)
|
||||
handlers.NewCommands(Commands{}.New()).MountRoutes(r)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -7,18 +7,3 @@ type (
|
||||
Description string `db:"description" json:"description"`
|
||||
}
|
||||
)
|
||||
|
||||
var (
|
||||
Preset CommandSet // @todo move this to someplace safe
|
||||
)
|
||||
|
||||
func init() {
|
||||
Preset = CommandSet{
|
||||
&Command{
|
||||
Name: "echo",
|
||||
Description: "It does exactly what it says on the tin"},
|
||||
&Command{
|
||||
Name: "shrug",
|
||||
Description: "It does exactly what it says on the tin"},
|
||||
}
|
||||
}
|
||||
|
||||
@ -113,10 +113,6 @@ func (sess *Session) connected() (err error) {
|
||||
}
|
||||
}
|
||||
|
||||
if err = sess.sendReply(payload.Commands(types.Preset)); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Tell everyone that user has connected
|
||||
if err = sess.sendToAll(&outgoing.Connected{UserID: payload.Uint64toa(sess.user.Identity())}); err != nil {
|
||||
return
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user