route/internal/server/route.go

110 lines
3.0 KiB
Go
Raw Normal View History

2017-04-29 02:47:24 +00:00
package server
import (
2017-10-01 15:23:08 +00:00
"git.xeserv.us/xena/route/internal/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) {
clitok, err := r.getAuth(ctx, "get single route", "route:get")
2017-04-29 03:08:11 +00:00
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 {
2017-10-01 15:06:27 +00:00
return nil, handleError(ctx, clitok, err, ln.F{"action": "get single route from database", "host": req.Host})
2017-04-29 02:47:24 +00:00
}
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) {
clitok, err := r.getAuth(ctx, "get all routes for user", "route:getall")
2017-04-29 03:08:11 +00:00
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 {
2017-10-01 15:06:27 +00:00
return nil, handleError(ctx, clitok, err, ln.F{"action": "get all routes for user from database"})
2017-04-29 02:47:24 +00:00
}
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.Route, error) {
clitok, err := r.getAuth(ctx, "put new route", "route:put")
2017-04-29 03:08:11 +00:00
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 {
2017-10-01 15:06:27 +00:00
return nil, handleError(ctx, clitok, err, ln.F{"action": "put route to database"})
2017-04-29 02:47:24 +00:00
}
2017-10-01 15:06:27 +00:00
ln.Log(ctx, drt, ln.Action("created new route"))
2017-04-29 05:46:56 +00:00
return drt.AsProto(), nil
2017-04-29 02:47:24 +00:00
}
func (r *Route) Delete(ctx context.Context, rt *proto.Route) (*proto.Nil, error) {
clitok, err := r.getAuth(ctx, "delete single route", "route:delete")
2017-04-29 03:08:11 +00:00
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 {
2017-10-01 15:06:27 +00:00
return nil, handleError(ctx, clitok, err, ln.F{"action": "fetch route from database", "host": rt.Host})
2017-04-29 02:47:24 +00:00
}
2017-04-29 03:08:11 +00:00
if drt.Creator != clitok.Owner {
2017-10-01 15:06:27 +00:00
return nil, handleError(ctx, clitok, ErrNotAuthorized, ln.F{"action": "user not authorized to delete this route", "host": rt.Host})
2017-04-29 03:08:11 +00:00
}
err = r.db.DeleteRoute(ctx, rt.Id)
2017-04-29 02:47:24 +00:00
f := drt.F()
2017-10-01 15:06:27 +00:00
f["action"] = "delete route from database"
2017-04-29 02:47:24 +00:00
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-10-01 15:06:27 +00:00
f["action"] = "deleted route from database"
2017-10-01 13:28:13 +00:00
ln.Log(ctx, f, drt)
2017-04-29 03:08:11 +00:00
return &proto.Nil{}, nil
2017-04-29 02:47:24 +00:00
}