From 3c0e208e060612c8a1f4b2744c8e4c50b15d3120 Mon Sep 17 00:00:00 2001 From: Tit Petric Date: Thu, 19 Jul 2018 16:44:42 +0200 Subject: [PATCH] del(websocket): move ws out of rest --- sam/docs/README.md | 20 +-------- sam/docs/src/spec.json | 23 ---------- sam/docs/src/spec/websocket.json | 36 ---------------- sam/rest/auth.go | 4 -- sam/rest/router.go | 11 +---- sam/rest/server/websocket.go | 39 ----------------- sam/rest/server/websocket_handlers.go | 27 ------------ sam/rest/server/websocket_requests.go | 48 --------------------- sam/rest/websocket.go | 21 --------- sam/types/websocket.go | 62 --------------------------- 10 files changed, 3 insertions(+), 288 deletions(-) delete mode 100644 sam/docs/src/spec/websocket.json delete mode 100644 sam/rest/server/websocket.go delete mode 100644 sam/rest/server/websocket_handlers.go delete mode 100644 sam/rest/server/websocket_requests.go delete mode 100644 sam/rest/websocket.go delete mode 100644 sam/types/websocket.go diff --git a/sam/docs/README.md b/sam/docs/README.md index 40ccdeead..28bb8ede7 100644 --- a/sam/docs/README.md +++ b/sam/docs/README.md @@ -450,22 +450,4 @@ The following event types may be sent with a message event: | Parameter | Type | Method | Description | Default | Required? | | --------- | ---- | ------ | ----------- | ------- | --------- | -| query | string | GET | Search query to match against users | N/A | NO | - - - - -# Websocket - -## Bidirectional websocket chat connection - -#### Method - -| URI | Protocol | Method | Authentication | -| --- | -------- | ------ | -------------- | -| `/websocket/client` | WebSockets | GET | Client ID, Session ID | - -#### Request parameters - -| Parameter | Type | Method | Description | Default | Required? | -| --------- | ---- | ------ | ----------- | ------- | --------- | \ No newline at end of file +| query | string | GET | Search query to match against users | N/A | NO | \ No newline at end of file diff --git a/sam/docs/src/spec.json b/sam/docs/src/spec.json index 19856ec49..7287f34f3 100644 --- a/sam/docs/src/spec.json +++ b/sam/docs/src/spec.json @@ -442,28 +442,5 @@ } } ] - }, - { - "title": "Websocket", - "package": "sam", - "entrypoint": "websocket", - "protocol": "WebSockets", - "authentication": ["Client ID", "Session ID"], - "struct": [ - { - "name": "Websocket", - "fields": [ - { "type": "uint64", "name": "UserID" }, - { "type": "User", "name": "User", "complex": true } - ] - } - ], - "apis": [ - { - "name": "client", - "method": "GET", - "title": "Bidirectional websocket chat connection" - } - ] } ] diff --git a/sam/docs/src/spec/websocket.json b/sam/docs/src/spec/websocket.json deleted file mode 100644 index b9629407a..000000000 --- a/sam/docs/src/spec/websocket.json +++ /dev/null @@ -1,36 +0,0 @@ -{ - "Title": "Websocket", - "Package": "sam", - "Interface": "Websocket", - "Struct": [ - { - "fields": [ - { - "name": "UserID", - "type": "uint64" - }, - { - "complex": true, - "name": "User", - "type": "User" - } - ], - "name": "Websocket" - } - ], - "Protocol": "WebSockets", - "Authentication": [ - "Client ID", - "Session ID" - ], - "Path": "/websocket", - "APIs": [ - { - "Name": "client", - "Method": "GET", - "Title": "Bidirectional websocket chat connection", - "Path": "/client", - "Parameters": null - } - ] -} \ No newline at end of file diff --git a/sam/rest/auth.go b/sam/rest/auth.go index 0380e9220..55adb5a04 100644 --- a/sam/rest/auth.go +++ b/sam/rest/auth.go @@ -29,7 +29,3 @@ func (*Message) Authenticator() func(http.Handler) http.Handler { func (*User) Authenticator() func(http.Handler) http.Handler { return pass } - -func (*Websocket) Authenticator() func(http.Handler) http.Handler { - return pass -} diff --git a/sam/rest/router.go b/sam/rest/router.go index 1353f68df..1861d9919 100644 --- a/sam/rest/router.go +++ b/sam/rest/router.go @@ -10,8 +10,8 @@ package rest 1. run [spec](https://github.com/titpetric/spec) in the same folder, 2. run `./_gen.php` in this folder. - You may edit `websocket.go`, `websocket.util.go` or `websocket_test.go` to - implement your API calls, helper functions and tests. The file `websocket.go` + You may edit `user.go`, `user.util.go` or `user_test.go` to + implement your API calls, helper functions and tests. The file `user.go` is only generated the first time, and will not be overwritten if it exists. */ @@ -30,7 +30,6 @@ func MountRoutes(r chi.Router) { organisation := &server.OrganisationHandlers{Organisation{}.New()} team := &server.TeamHandlers{Team{}.New()} user := &server.UserHandlers{User{}.New()} - websocket := &server.WebsocketHandlers{Websocket{}.New()} r.Group(func(r chi.Router) { r.Use(channel.Channel.Authenticator()) r.Route("/channel", func(r chi.Router) { @@ -84,12 +83,6 @@ func MountRoutes(r chi.Router) { r.Get("/search", user.Search) }) }) - r.Group(func(r chi.Router) { - r.Use(websocket.Websocket.Authenticator()) - r.Route("/websocket", func(r chi.Router) { - r.Get("/client", websocket.Client) - }) - }) var printRoutes func(chi.Routes, string, string) printRoutes = func(r chi.Routes, indent string, prefix string) { diff --git a/sam/rest/server/websocket.go b/sam/rest/server/websocket.go deleted file mode 100644 index 8c47aaacb..000000000 --- a/sam/rest/server/websocket.go +++ /dev/null @@ -1,39 +0,0 @@ -package server - -/* - 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 `websocket.go`, `websocket.util.go` or `websocket_test.go` to - implement your API calls, helper functions and tests. The file `websocket.go` - is only generated the first time, and will not be overwritten if it exists. -*/ - -import ( - "context" - "net/http" -) - -// HTTP handlers are a superset of internal APIs -type WebsocketHandlers struct { - Websocket WebsocketAPI -} - -// Internal API interface -type WebsocketAPI interface { - Client(context.Context, *WebsocketClientRequest) (interface{}, error) - - // Authenticate API requests - Authenticator() func(http.Handler) http.Handler -} - -// HTTP API interface -type WebsocketHandlersAPI interface { - Client(http.ResponseWriter, *http.Request) -} diff --git a/sam/rest/server/websocket_handlers.go b/sam/rest/server/websocket_handlers.go deleted file mode 100644 index f61457504..000000000 --- a/sam/rest/server/websocket_handlers.go +++ /dev/null @@ -1,27 +0,0 @@ -package server - -/* - 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 `websocket.go`, `websocket.util.go` or `websocket_test.go` to - implement your API calls, helper functions and tests. The file `websocket.go` - is only generated the first time, and will not be overwritten if it exists. -*/ - -import ( - "net/http" - - "github.com/titpetric/factory/resputil" -) - -func (wh *WebsocketHandlers) Client(w http.ResponseWriter, r *http.Request) { - params := WebsocketClientRequest{}.new() - resputil.JSON(w, params.Fill(r), func() (interface{}, error) { return wh.Websocket.Client(r.Context(), params) }) -} diff --git a/sam/rest/server/websocket_requests.go b/sam/rest/server/websocket_requests.go deleted file mode 100644 index 9b8f0c021..000000000 --- a/sam/rest/server/websocket_requests.go +++ /dev/null @@ -1,48 +0,0 @@ -package server - -/* - 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 `websocket.go`, `websocket.util.go` or `websocket_test.go` to - implement your API calls, helper functions and tests. The file `websocket.go` - is only generated the first time, and will not be overwritten if it exists. -*/ - -import ( - "github.com/go-chi/chi" - "net/http" -) - -var _ = chi.URLParam - -// Websocket client request parameters -type WebsocketClientRequest struct { -} - -func (WebsocketClientRequest) new() *WebsocketClientRequest { - return &WebsocketClientRequest{} -} - -func (w *WebsocketClientRequest) Fill(r *http.Request) error { - r.ParseForm() - 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 nil -} - -var _ RequestFiller = WebsocketClientRequest{}.new() diff --git a/sam/rest/websocket.go b/sam/rest/websocket.go deleted file mode 100644 index 343adb3d4..000000000 --- a/sam/rest/websocket.go +++ /dev/null @@ -1,21 +0,0 @@ -package rest - -import ( - "context" - "github.com/pkg/errors" - - "github.com/crusttech/crust/sam/rest/server" - _ "github.com/crusttech/crust/sam/types" -) - -var _ = errors.Wrap - -type Websocket struct{} - -func (Websocket) New() *Websocket { - return &Websocket{} -} - -func (*Websocket) Client(ctx context.Context, r *server.WebsocketClientRequest) (interface{}, error) { - return nil, errors.New("Not implemented: Websocket.client") -} diff --git a/sam/types/websocket.go b/sam/types/websocket.go deleted file mode 100644 index 5dc9d86f4..000000000 --- a/sam/types/websocket.go +++ /dev/null @@ -1,62 +0,0 @@ -package types - -/* - 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 `websocket.go`, `websocket.util.go` or `websocket_test.go` to - implement your API calls, helper functions and tests. The file `websocket.go` - is only generated the first time, and will not be overwritten if it exists. -*/ - -type ( - // Websocket - - Websocket struct { - UserID uint64 `db:"user_id"` - User User `db:"user"` - - changed []string - } -) - -// New constructs a new instance of Websocket -func (Websocket) New() *Websocket { - return &Websocket{} -} - -// Get the value of UserID -func (w *Websocket) GetUserID() uint64 { - return w.UserID -} - -// Set the value of UserID -func (w *Websocket) SetUserID(value uint64) *Websocket { - if w.UserID != value { - w.changed = append(w.changed, "UserID") - w.UserID = value - } - return w -} - -// Get the value of User -func (w *Websocket) GetUser() User { - return w.User -} - -// Set the value of User -func (w *Websocket) SetUser(value User) *Websocket { - w.changed = append(w.changed, "User") - w.User = value - return w -} - -// Changes returns the names of changed fields -func (w *Websocket) Changes() []string { - return w.changed -}