surrealpatch/web/routes.go

160 lines
4.4 KiB
Go
Raw Normal View History

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 (
"github.com/abcum/fibre"
"github.com/abcum/surreal/db"
)
func output(c *fibre.Context, res interface{}) error {
switch ret := res.(*db.Response); ret.Status {
case "OK":
return c.Send(200, ret.Result)
2016-05-23 14:20:36 +00:00
case "ERR_DB":
return fibre.NewHTTPError(500)
2016-04-03 21:48:57 +00:00
case "ERR_EXISTS":
return fibre.NewHTTPError(409)
default:
2016-05-23 14:20:36 +00:00
return fibre.NewHTTPError(400)
2016-04-03 21:48:57 +00:00
}
}
func routes(s *fibre.Fibre) {
2016-05-23 14:21:18 +00:00
s.Dir("/", "gui/dist/")
2016-04-03 21:48:57 +00:00
s.Rpc("/rpc", &rpc{})
2016-04-04 09:14:16 +00:00
// --------------------------------------------------
// Endpoints for submitting sql queries
// --------------------------------------------------
2016-04-03 21:48:57 +00:00
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 {
res, err := db.Execute(c, c.Request().Body)
if err != nil {
2016-05-23 14:20:36 +00:00
return c.Send(400, map[string]interface{}{
"code": 400,
"details": "Request problems detected",
"documentation": "https://docs.surreal.io/",
"information": err.Error(),
})
2016-04-03 21:48:57 +00:00
}
return c.Send(200, 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 {
sql := db.Prepare("SELECT * FROM %v", c.Param("class"))
2016-04-04 09:14:16 +00:00
res, err := db.Execute(c, sql)
if err != nil {
return fibre.NewHTTPError(400)
}
return output(c, res[0])
})
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 {
sql := db.Prepare("CREATE %v CONTENT %v RETURN AFTER", c.Param("class"), string(c.Body()))
2016-04-03 21:48:57 +00:00
res, err := db.Execute(c, sql)
if err != nil {
return fibre.NewHTTPError(400)
}
2016-04-04 09:14:16 +00:00
return output(c, res[0])
})
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 {
sql := db.Prepare("DELETE FROM %v", c.Param("class"))
res, err := db.Execute(c, sql)
if err != nil {
return fibre.NewHTTPError(400)
}
2016-04-03 21:48:57 +00:00
return output(c, res[0])
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 {
sql := db.Prepare("SELECT * FROM @%v:%v", c.Param("class"), c.Param("id"))
res, err := db.Execute(c, sql)
if err != nil {
return fibre.NewHTTPError(400)
}
return output(c, res[0])
})
s.Put("/key/:class/:id", func(c *fibre.Context) error {
sql := db.Prepare("CREATE @%v:%v CONTENT %v RETURN AFTER", c.Param("class"), c.Param("id"), string(c.Body()))
res, err := db.Execute(c, sql)
if err != nil {
return fibre.NewHTTPError(400)
}
return output(c, res[0])
})
s.Post("/key/:class/:id", func(c *fibre.Context) error {
sql := db.Prepare("UPDATE @%v:%v CONTENT %v RETURN AFTER", c.Param("class"), c.Param("id"), string(c.Body()))
res, err := db.Execute(c, sql)
if err != nil {
return fibre.NewHTTPError(400)
}
return output(c, res[0])
})
s.Patch("/key/:class/:id", func(c *fibre.Context) error {
sql := db.Prepare("MODIFY @%v:%v DIFF %v RETURN AFTER", c.Param("class"), c.Param("id"), string(c.Body()))
res, err := db.Execute(c, sql)
if err != nil {
return fibre.NewHTTPError(400)
}
return output(c, res[0])
})
s.Trace("/key/:class/:id", func(c *fibre.Context) error {
sql := db.Prepare("SELECT HISTORY FROM @%v:%v", c.Param("class"), c.Param("id"))
res, err := db.Execute(c, sql)
if err != nil {
return fibre.NewHTTPError(400)
}
return output(c, res[0])
})
s.Delete("/key/:class/:id", func(c *fibre.Context) error {
sql := db.Prepare("DELETE @%v:%v", c.Param("class"), c.Param("id"))
res, err := db.Execute(c, sql)
if err != nil {
return fibre.NewHTTPError(400)
}
return output(c, res[0])
2016-04-04 09:14:16 +00:00
})
2016-04-03 21:48:57 +00:00
}