2016-04-03 21:48:57 +00:00
|
|
|
// Copyright © 2016 Abcum Ltd
|
|
|
|
//
|
|
|
|
// 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 web
|
|
|
|
|
|
|
|
import (
|
2017-02-20 01:25:39 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2016-04-03 21:48:57 +00:00
|
|
|
"github.com/abcum/fibre"
|
2017-02-20 01:56:29 +00:00
|
|
|
"github.com/abcum/fibre/mw"
|
2016-04-03 21:48:57 +00:00
|
|
|
"github.com/abcum/surreal/db"
|
2016-09-14 21:33:09 +00:00
|
|
|
"github.com/abcum/surreal/sql"
|
2017-02-22 01:41:52 +00:00
|
|
|
"github.com/abcum/surreal/util/build"
|
2017-02-20 10:26:49 +00:00
|
|
|
"github.com/abcum/surreal/util/show"
|
2016-04-03 21:48:57 +00:00
|
|
|
)
|
|
|
|
|
2017-02-20 01:25:39 +00:00
|
|
|
func limit(c *fibre.Context, i int64) int64 {
|
|
|
|
if s := c.Query("limit"); len(s) > 0 {
|
|
|
|
if x, err := strconv.ParseInt(s, 10, 64); err == nil {
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
func start(c *fibre.Context, i int64) int64 {
|
|
|
|
if s := c.Query("start"); len(s) > 0 {
|
|
|
|
if x, err := strconv.ParseInt(s, 10, 64); err == nil {
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2017-03-03 21:39:37 +00:00
|
|
|
func versn(c *fibre.Context, i time.Time) time.Time {
|
2017-02-20 01:25:39 +00:00
|
|
|
if s := c.Query("version"); len(s) > 0 {
|
|
|
|
if x, err := time.Parse(sql.RFCNano, s); err == nil {
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2016-04-03 21:48:57 +00:00
|
|
|
func routes(s *fibre.Fibre) {
|
|
|
|
|
2016-07-04 23:03:48 +00:00
|
|
|
s.Dir("/", "app/")
|
2016-04-03 21:48:57 +00:00
|
|
|
|
2016-04-04 09:14:16 +00:00
|
|
|
// --------------------------------------------------
|
2016-06-18 09:01:02 +00:00
|
|
|
// Endpoint for health checks
|
2016-04-04 09:14:16 +00:00
|
|
|
// --------------------------------------------------
|
2016-04-03 21:48:57 +00:00
|
|
|
|
2016-05-23 14:21:35 +00:00
|
|
|
s.Get("/info", func(c *fibre.Context) error {
|
2017-02-22 01:41:52 +00:00
|
|
|
return c.Send(200, build.GetInfo())
|
2016-05-23 14:21:35 +00:00
|
|
|
})
|
|
|
|
|
2016-06-18 09:01:02 +00:00
|
|
|
// --------------------------------------------------
|
|
|
|
// Endpoints for submitting rpc queries
|
|
|
|
// --------------------------------------------------
|
|
|
|
|
|
|
|
s.Rpc("/rpc", &rpc{})
|
|
|
|
|
2016-11-21 18:48:25 +00:00
|
|
|
// --------------------------------------------------
|
|
|
|
// Endpoints for authentication signup
|
|
|
|
// --------------------------------------------------
|
|
|
|
|
|
|
|
s.Options("/signup", func(c *fibre.Context) error {
|
|
|
|
return c.Code(200)
|
|
|
|
})
|
|
|
|
|
|
|
|
s.Post("/signup", func(c *fibre.Context) error {
|
|
|
|
return signup(c)
|
|
|
|
})
|
|
|
|
|
2017-03-03 21:41:06 +00:00
|
|
|
s.Use(mw.Quit(&mw.QuitOpts{
|
2017-11-16 20:41:29 +00:00
|
|
|
Timeout: 5 * time.Second,
|
2017-03-03 21:41:06 +00:00
|
|
|
}).PathIs("/signup"))
|
|
|
|
|
2017-02-20 01:56:29 +00:00
|
|
|
s.Use(mw.Type(&mw.TypeOpts{
|
|
|
|
AllowedContent: map[string]bool{
|
|
|
|
"application/json": true,
|
2017-11-16 20:42:42 +00:00
|
|
|
"application/cork": true,
|
2017-02-20 01:56:29 +00:00
|
|
|
"application/msgpack": true,
|
|
|
|
"application/x-www-form-urlencoded": true,
|
|
|
|
},
|
2017-11-16 20:42:42 +00:00
|
|
|
}).PathIs("/signup").MethodIsNot("OPTIONS"))
|
2017-02-20 01:56:29 +00:00
|
|
|
|
2016-11-21 18:48:25 +00:00
|
|
|
// --------------------------------------------------
|
|
|
|
// Endpoints for authentication signin
|
|
|
|
// --------------------------------------------------
|
|
|
|
|
|
|
|
s.Options("/signin", func(c *fibre.Context) error {
|
|
|
|
return c.Code(200)
|
|
|
|
})
|
|
|
|
|
|
|
|
s.Post("/signin", func(c *fibre.Context) error {
|
|
|
|
return signin(c)
|
|
|
|
})
|
|
|
|
|
2017-03-03 21:41:06 +00:00
|
|
|
s.Use(mw.Quit(&mw.QuitOpts{
|
2017-11-16 20:41:29 +00:00
|
|
|
Timeout: 5 * time.Second,
|
2017-03-03 21:41:06 +00:00
|
|
|
}).PathIs("/signin"))
|
|
|
|
|
2017-02-20 01:56:29 +00:00
|
|
|
s.Use(mw.Type(&mw.TypeOpts{
|
|
|
|
AllowedContent: map[string]bool{
|
|
|
|
"application/json": true,
|
2017-11-16 20:42:42 +00:00
|
|
|
"application/cork": true,
|
2017-02-20 01:56:29 +00:00
|
|
|
"application/msgpack": true,
|
|
|
|
"application/x-www-form-urlencoded": true,
|
|
|
|
},
|
2017-11-16 20:42:42 +00:00
|
|
|
}).PathIs("/signin").MethodIsNot("OPTIONS"))
|
2017-02-20 01:56:29 +00:00
|
|
|
|
2017-02-19 19:25:47 +00:00
|
|
|
// --------------------------------------------------
|
|
|
|
// Endpoints for import and exporting data
|
|
|
|
// --------------------------------------------------
|
|
|
|
|
|
|
|
s.Get("/export", func(c *fibre.Context) error {
|
|
|
|
return exporter(c)
|
|
|
|
})
|
|
|
|
|
|
|
|
s.Post("/import", func(c *fibre.Context) error {
|
|
|
|
return importer(c)
|
|
|
|
})
|
|
|
|
|
2017-02-20 01:56:29 +00:00
|
|
|
s.Use(mw.Type(&mw.TypeOpts{
|
|
|
|
AllowedContent: map[string]bool{
|
|
|
|
"application/octet-stream": true,
|
|
|
|
},
|
|
|
|
}).PathIs("/export", "/import"))
|
|
|
|
|
2016-05-23 14:21:35 +00:00
|
|
|
// --------------------------------------------------
|
|
|
|
// Endpoints for submitting sql queries
|
|
|
|
// --------------------------------------------------
|
|
|
|
|
2016-04-04 09:14:16 +00:00
|
|
|
s.Options("/sql", func(c *fibre.Context) error {
|
2016-04-03 21:48:57 +00:00
|
|
|
return c.Code(200)
|
|
|
|
})
|
|
|
|
|
|
|
|
s.Post("/sql", func(c *fibre.Context) error {
|
2016-09-14 21:32:52 +00:00
|
|
|
res, err := db.Execute(c, c.Request().Body, nil)
|
2016-04-03 21:48:57 +00:00
|
|
|
if err != nil {
|
2016-09-06 13:36:09 +00:00
|
|
|
return err
|
2016-04-03 21:48:57 +00:00
|
|
|
}
|
|
|
|
return c.Send(200, res)
|
2016-04-04 09:14:16 +00:00
|
|
|
})
|
|
|
|
|
2017-02-20 01:56:29 +00:00
|
|
|
s.Use(mw.Type(&mw.TypeOpts{
|
|
|
|
AllowedContent: map[string]bool{
|
2017-11-16 20:42:42 +00:00
|
|
|
"text/plain": true,
|
2017-02-20 01:56:29 +00:00
|
|
|
"application/json": true,
|
2017-11-16 20:42:42 +00:00
|
|
|
"application/cork": true,
|
2017-02-20 01:56:29 +00:00
|
|
|
"application/msgpack": true,
|
|
|
|
},
|
2017-11-16 20:42:42 +00:00
|
|
|
}).PathIs("/sql").MethodIsNot("OPTIONS"))
|
2017-02-20 01:56:29 +00:00
|
|
|
|
2017-02-20 01:57:49 +00:00
|
|
|
// --------------------------------------------------
|
|
|
|
// Endpoints for submitting websocket sql
|
|
|
|
// --------------------------------------------------
|
2017-02-20 01:21:45 +00:00
|
|
|
|
2017-02-20 01:57:49 +00:00
|
|
|
s.Get("/sql", func(c *fibre.Context) error {
|
2017-02-20 01:21:45 +00:00
|
|
|
if err := c.Upgrade(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
_, msg, err := c.Socket().Read()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if res, err := db.Execute(c, msg, nil); err != nil {
|
|
|
|
c.Socket().SendText(err.Error())
|
|
|
|
} else {
|
|
|
|
c.Socket().SendJSON(res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2016-04-04 09:14:16 +00:00
|
|
|
// --------------------------------------------------
|
|
|
|
// Endpoints for manipulating multiple records
|
|
|
|
// --------------------------------------------------
|
2016-04-03 21:48:57 +00:00
|
|
|
|
2016-04-04 09:14:16 +00:00
|
|
|
s.Options("/key/:class", func(c *fibre.Context) error {
|
|
|
|
return c.Code(200)
|
2016-04-03 21:48:57 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
s.Get("/key/:class", func(c *fibre.Context) error {
|
2016-09-14 21:32:52 +00:00
|
|
|
|
2017-11-16 20:43:26 +00:00
|
|
|
txt := "SELECT * FROM $class LIMIT $limit START $start VERSION $versn"
|
2016-09-14 21:32:52 +00:00
|
|
|
|
|
|
|
res, err := db.Execute(c, txt, map[string]interface{}{
|
|
|
|
"class": sql.NewTable(c.Param("class")),
|
2017-02-20 01:25:39 +00:00
|
|
|
"limit": limit(c, 100),
|
|
|
|
"start": start(c, 0),
|
2017-11-16 20:43:26 +00:00
|
|
|
"versn": versn(c, time.Now()),
|
2016-09-14 21:32:52 +00:00
|
|
|
})
|
|
|
|
|
2017-02-20 10:26:49 +00:00
|
|
|
return show.Output(c, c.Param("class"), show.Many, show.Select, res, err)
|
2016-09-14 21:32:52 +00:00
|
|
|
|
2016-04-04 09:14:16 +00:00
|
|
|
})
|
2016-04-03 21:48:57 +00:00
|
|
|
|
2016-04-04 09:14:16 +00:00
|
|
|
s.Post("/key/:class", func(c *fibre.Context) error {
|
2016-09-14 21:32:52 +00:00
|
|
|
|
|
|
|
var data interface{}
|
|
|
|
|
2017-02-20 01:25:39 +00:00
|
|
|
if err := c.Bind(&data); err != nil {
|
2016-09-14 21:32:52 +00:00
|
|
|
return fibre.NewHTTPError(422)
|
2016-04-03 21:48:57 +00:00
|
|
|
}
|
2016-09-14 21:32:52 +00:00
|
|
|
|
|
|
|
txt := "CREATE $class CONTENT $data RETURN AFTER"
|
|
|
|
|
|
|
|
res, err := db.Execute(c, txt, map[string]interface{}{
|
|
|
|
"class": sql.NewTable(c.Param("class")),
|
|
|
|
"data": data,
|
|
|
|
})
|
|
|
|
|
2017-02-20 10:26:49 +00:00
|
|
|
return show.Output(c, c.Param("class"), show.Many, show.Create, res, err)
|
2016-09-14 21:32:52 +00:00
|
|
|
|
2016-04-04 09:14:16 +00:00
|
|
|
})
|
2016-04-03 21:48:57 +00:00
|
|
|
|
2016-04-04 09:14:16 +00:00
|
|
|
s.Delete("/key/:class", func(c *fibre.Context) error {
|
2016-09-14 21:32:52 +00:00
|
|
|
|
|
|
|
txt := "DELETE FROM $class"
|
|
|
|
|
|
|
|
res, err := db.Execute(c, txt, map[string]interface{}{
|
|
|
|
"class": sql.NewTable(c.Param("class")),
|
|
|
|
})
|
|
|
|
|
2017-02-20 10:26:49 +00:00
|
|
|
return show.Output(c, c.Param("class"), show.Many, show.Delete, res, err)
|
2016-09-14 21:32:52 +00:00
|
|
|
|
2016-04-04 09:14:16 +00:00
|
|
|
})
|
2016-04-03 21:48:57 +00:00
|
|
|
|
2016-04-04 09:14:16 +00:00
|
|
|
// --------------------------------------------------
|
|
|
|
// Endpoints for manipulating a single record
|
|
|
|
// --------------------------------------------------
|
|
|
|
|
|
|
|
s.Options("/key/:class/:id", func(c *fibre.Context) error {
|
|
|
|
return c.Code(200)
|
2016-04-03 21:48:57 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
s.Get("/key/:class/:id", func(c *fibre.Context) error {
|
2016-09-14 21:32:52 +00:00
|
|
|
|
2017-03-03 21:39:37 +00:00
|
|
|
txt := "SELECT * FROM $thing VERSION $versn"
|
2016-09-14 21:32:52 +00:00
|
|
|
|
|
|
|
res, err := db.Execute(c, txt, map[string]interface{}{
|
|
|
|
"thing": sql.NewThing(c.Param("class"), c.Param("id")),
|
2017-03-03 21:39:37 +00:00
|
|
|
"versn": versn(c, time.Now()),
|
2016-09-14 21:32:52 +00:00
|
|
|
})
|
|
|
|
|
2017-02-20 10:26:49 +00:00
|
|
|
return show.Output(c, c.Param("class"), show.One, show.Select, res, err)
|
2016-09-14 21:32:52 +00:00
|
|
|
|
2016-04-03 21:48:57 +00:00
|
|
|
})
|
|
|
|
|
2017-02-20 01:25:39 +00:00
|
|
|
s.Post("/key/:class/:id", func(c *fibre.Context) error {
|
2016-09-14 21:32:52 +00:00
|
|
|
|
|
|
|
var data interface{}
|
|
|
|
|
2017-02-20 01:25:39 +00:00
|
|
|
if err := c.Bind(&data); err != nil {
|
2016-09-14 21:32:52 +00:00
|
|
|
return fibre.NewHTTPError(422)
|
2016-04-03 21:48:57 +00:00
|
|
|
}
|
2016-09-14 21:32:52 +00:00
|
|
|
|
|
|
|
txt := "CREATE $thing CONTENT $data RETURN AFTER"
|
|
|
|
|
|
|
|
res, err := db.Execute(c, txt, map[string]interface{}{
|
|
|
|
"thing": sql.NewThing(c.Param("class"), c.Param("id")),
|
|
|
|
"data": data,
|
|
|
|
})
|
|
|
|
|
2017-02-20 10:26:49 +00:00
|
|
|
return show.Output(c, c.Param("class"), show.One, show.Create, res, err)
|
2016-09-14 21:32:52 +00:00
|
|
|
|
2016-04-03 21:48:57 +00:00
|
|
|
})
|
|
|
|
|
2017-02-20 01:25:39 +00:00
|
|
|
s.Put("/key/:class/:id", func(c *fibre.Context) error {
|
2016-09-14 21:32:52 +00:00
|
|
|
|
|
|
|
var data interface{}
|
|
|
|
|
2017-02-20 01:25:39 +00:00
|
|
|
if err := c.Bind(&data); err != nil {
|
2016-09-14 21:32:52 +00:00
|
|
|
return fibre.NewHTTPError(422)
|
2016-04-03 21:48:57 +00:00
|
|
|
}
|
2016-09-14 21:32:52 +00:00
|
|
|
|
|
|
|
txt := "UPDATE $thing CONTENT $data RETURN AFTER"
|
|
|
|
|
|
|
|
res, err := db.Execute(c, txt, map[string]interface{}{
|
|
|
|
"thing": sql.NewThing(c.Param("class"), c.Param("id")),
|
|
|
|
"data": data,
|
|
|
|
})
|
|
|
|
|
2017-02-20 10:26:49 +00:00
|
|
|
return show.Output(c, c.Param("class"), show.One, show.Update, res, err)
|
2016-09-14 21:32:52 +00:00
|
|
|
|
2016-04-03 21:48:57 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
s.Patch("/key/:class/:id", func(c *fibre.Context) error {
|
2016-09-14 21:32:52 +00:00
|
|
|
|
|
|
|
var data interface{}
|
|
|
|
|
2017-02-20 01:25:39 +00:00
|
|
|
if err := c.Bind(&data); err != nil {
|
2016-09-14 21:32:52 +00:00
|
|
|
return fibre.NewHTTPError(422)
|
2016-04-03 21:48:57 +00:00
|
|
|
}
|
2016-09-14 21:32:52 +00:00
|
|
|
|
2017-02-20 01:25:39 +00:00
|
|
|
txt := "UPDATE $thing DIFF $data RETURN DIFF"
|
2016-09-14 21:32:52 +00:00
|
|
|
|
|
|
|
res, err := db.Execute(c, txt, map[string]interface{}{
|
|
|
|
"thing": sql.NewThing(c.Param("class"), c.Param("id")),
|
|
|
|
"data": data,
|
|
|
|
})
|
|
|
|
|
2017-02-20 10:26:49 +00:00
|
|
|
return show.Output(c, c.Param("class"), show.One, show.Modify, res, err)
|
2016-09-14 21:32:52 +00:00
|
|
|
|
2016-04-03 21:48:57 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
s.Trace("/key/:class/:id", func(c *fibre.Context) error {
|
2016-09-14 21:32:52 +00:00
|
|
|
|
|
|
|
txt := "SELECT HISTORY FROM $thing"
|
|
|
|
|
|
|
|
res, err := db.Execute(c, txt, map[string]interface{}{
|
|
|
|
"thing": sql.NewThing(c.Param("class"), c.Param("id")),
|
|
|
|
})
|
|
|
|
|
2017-02-20 10:26:49 +00:00
|
|
|
return show.Output(c, c.Param("class"), show.One, show.Trace, res, err)
|
2016-09-14 21:32:52 +00:00
|
|
|
|
2016-04-03 21:48:57 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
s.Delete("/key/:class/:id", func(c *fibre.Context) error {
|
2016-09-14 21:32:52 +00:00
|
|
|
|
|
|
|
txt := "DELETE $thing"
|
|
|
|
|
|
|
|
res, err := db.Execute(c, txt, map[string]interface{}{
|
|
|
|
"thing": sql.NewThing(c.Param("class"), c.Param("id")),
|
|
|
|
})
|
|
|
|
|
2017-02-20 10:26:49 +00:00
|
|
|
return show.Output(c, c.Param("class"), show.One, show.Delete, res, err)
|
2016-09-14 21:32:52 +00:00
|
|
|
|
2016-04-04 09:14:16 +00:00
|
|
|
})
|
2016-04-03 21:48:57 +00:00
|
|
|
|
2017-02-20 01:56:29 +00:00
|
|
|
s.Use(mw.Type(&mw.TypeOpts{
|
|
|
|
AllowedContent: map[string]bool{
|
|
|
|
"application/json": true,
|
2017-11-16 20:42:42 +00:00
|
|
|
"application/cork": true,
|
2017-02-20 01:56:29 +00:00
|
|
|
"application/msgpack": true,
|
|
|
|
"application/vnd.api+json": true,
|
|
|
|
},
|
2017-11-16 20:42:42 +00:00
|
|
|
}).PathBegsWith("/key/").MethodIsNot("OPTIONS"))
|
2017-02-20 01:56:29 +00:00
|
|
|
|
2016-04-03 21:48:57 +00:00
|
|
|
}
|