route/server/route.go

119 lines
2.8 KiB
Go
Raw Normal View History

2017-04-29 02:47:24 +00:00
package server
import (
2017-04-29 05:45:43 +00:00
"git.xeserv.us/xena/route/lib/elfs"
2017-04-29 02:47:24 +00:00
proto "git.xeserv.us/xena/route/proto"
"github.com/Xe/ln"
"golang.org/x/net/context"
)
// Route implements rout.RoutesServer for gRPC
type Route struct {
*Server
}
// interface assertions
var (
_ proto.RoutesServer = &Route{}
)
// Get fetches a route from the database.
func (r *Route) Get(ctx context.Context, req *proto.GetRouteRequest) (*proto.Route, error) {
2017-04-29 03:08:11 +00:00
clitok, err := r.getAuth(ctx, "route:get")
if err != nil {
return nil, handleError(ctx, clitok, err, ln.F{"action": "Route.Get_getAuth"})
}
2017-04-29 02:47:24 +00:00
val, err := r.db.GetRoute(ctx, req.Host)
if err != nil {
ln.Error(err, ln.F{"action": "Route.Get"})
return nil, err
}
2017-04-29 03:08:11 +00:00
if val.Creator != clitok.Owner {
return nil, handleError(ctx, clitok, ErrNotAuthorized, ln.F{"action": "Route.Get_wrong_ownership"})
}
2017-04-29 02:47:24 +00:00
return val.AsProto(), nil
}
// GetAll fetches all of the routes that you own.
func (r *Route) GetAll(ctx context.Context, req *proto.Nil) (*proto.GetAllRoutesResponse, error) {
2017-04-29 03:08:11 +00:00
clitok, err := r.getAuth(ctx, "route:getall")
if err != nil {
return nil, handleError(ctx, clitok, err, ln.F{"action": "Route.GetAll_getAuth"})
}
routes, err := r.db.GetAllRoutes(ctx, clitok.Owner)
2017-04-29 02:47:24 +00:00
if err != nil {
ln.Error(err, ln.F{"action": "Route.GetAll"})
return nil, err
}
result := []*proto.Route{}
// let result = apply routeAsProto routes
for _, rt := range routes {
result = append(result, rt.AsProto())
}
return &proto.GetAllRoutesResponse{
Routes: result,
}, nil
}
func (r *Route) Put(ctx context.Context, rt *proto.Route) (*proto.IDResponse, error) {
2017-04-29 03:08:11 +00:00
clitok, err := r.getAuth(ctx, "route:put")
if err != nil {
return nil, handleError(ctx, clitok, err, ln.F{"action": "Route.Put_getAuth"})
}
2017-04-29 05:45:43 +00:00
if rt.Host == "" {
rt.Host = elfs.MakeName() + r.cfg.DomainSuffix
}
2017-04-29 05:39:18 +00:00
drt, err := r.db.PutRoute(ctx, rt.Host, clitok.Owner)
2017-04-29 02:47:24 +00:00
if err != nil {
ln.Error(err, ln.F{"action": "Route.Put"})
return nil, err
}
2017-04-29 05:46:56 +00:00
ln.Log(drt.F(), ln.F{"action": "Route.Put_success"})
2017-04-29 02:47:24 +00:00
return &proto.IDResponse{
Id: drt.ID,
2017-04-29 03:08:11 +00:00
}, nil
2017-04-29 02:47:24 +00:00
}
func (r *Route) Delete(ctx context.Context, rt *proto.Route) (*proto.IDResponse, error) {
2017-04-29 03:08:11 +00:00
clitok, err := r.getAuth(ctx, "route:delete")
if err != nil {
return nil, handleError(ctx, clitok, err, ln.F{"action": "Route.Delete_getAuth"})
}
2017-04-29 02:47:24 +00:00
drt, err := r.db.GetRoute(ctx, rt.Host)
if err != nil {
ln.Error(err, ln.F{"action": "Route.Delete_getRoute_verify"})
return nil, err
}
2017-04-29 03:08:11 +00:00
if drt.Creator != clitok.Owner {
return nil, handleError(ctx, clitok, ErrNotAuthorized, ln.F{"action": "Route.Delete_not_authorized"})
}
err = r.db.DeleteRoute(ctx, rt.Id)
2017-04-29 02:47:24 +00:00
f := drt.F()
f["action"] = "Route.Delete_db.DeleteRoute"
if err != nil {
2017-04-29 03:08:11 +00:00
handleError(ctx, clitok, ErrNotAuthorized, f)
2017-04-29 02:47:24 +00:00
}
2017-04-29 03:08:11 +00:00
ln.Log(f, drt.F())
2017-04-29 02:47:24 +00:00
return &proto.IDResponse{Id: rt.Id}, nil
}