Skip to content

Commit

Permalink
Dialyzed the hell out of stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
Licenser committed Dec 2, 2012
1 parent 7b50669 commit 943fb74
Show file tree
Hide file tree
Showing 13 changed files with 373 additions and 208 deletions.
97 changes: 60 additions & 37 deletions Makefile
@@ -1,53 +1,76 @@
APP_NAME=wiggle
APP_DIR=apps/$(APP_NAME)
OBJ=$(shell ls $(APP_DIR)/src/*.erl | sed -e 's/\.erl$$/.beam/' | sed -e 's;^$(APP_DIR)/src;$(APP_DIR)/ebin;g') $(shell ls $(APP_DIR)/src/*.app.src | sed -e 's/\.src$$//g' | sed -e 's;^$(APP_DIR)/src;$(APP_DIR)/ebin;g')
DEPS=$(shell cat rebar.config |sed -e 's/%.*//'| sed -e '/{\(\w\+\), [^,]\+, {\w\+, [^,]\+, {[^,]\+, [^}]\+}}},\?/!d' | sed -e 's;{\(\w\+\), [^,]\+, {\w\+, [^,]\+, {[^,]\+, [^}]\+}}},\?;deps/\1/rebar.config;')
ERL=erl
PA=$(shell pwd)/$(APP_DIR)/ebin
ERL_LIBS=`pwd`/deps/
REBAR=./rebar
REBAR = $(shell pwd)/rebar

.PHONY: rel clean package deps
.PHONY: deps rel stagedevrel

all: $(DEPS) $(OBJ)
all: deps compile

compile:
$(REBAR) compile

deps:
$(REBAR) get-deps
$(REBAR) update-deps

rel: all
-rm -r rel/$(APP_NAME)
cd rel; ../rebar generate
echo:
echo $(DEPS)

tar: rel
cd rel; tar jcvf $(APP_NAME).tar.bz2 $(APP_NAME)

test:
$(REBAR) skip_deps=true eunit

clean:
$(REBAR) clean
make -C rel/pkg clean
-rm *.beam erl_crash.dump
-rm -r rel/$(APP_NAME)
-rm rel/$(APP_NAME).tar.bz2

$(DEPS):
$(REBAR) get-deps
$(REBAR) compile
distclean: clean devclean relclean
$(REBAR) delete-deps

$(APP_DIR)/ebin/%.app: $(APP_DIR)/src/%.app.src
$(REBAR) compile
test: all
$(REBAR) skip_deps=true xref
$(REBAR) skip_deps=true eunit

$(APP_DIR)/ebin/%.beam: $(APP_DIR)/src/%.erl
$(REBAR) compile
rel: all
$(REBAR) generate

shell: all
ERL_LIBS="$(ERL_LIBS)" $(ERL) -pa $(PA) -config standalone -sname $(APP_NAME)
[ -f *.beam ] && rm *.beam || true
[ -f erl_crash.dump ] && rm erl_crash.dump || true
relclean:
rm -rf rel/wiggle

package: rel
make -C rel/pkg package

###
### Docs
###
docs:
$(REBAR) skip_deps=true doc

##
## Developer targets
##

stage : rel
$(foreach dep,$(wildcard deps/* wildcard apps/*), rm -rf rel/wiggle/lib/$(shell basename $(dep))-* && ln -sf $(abspath $(dep)) rel/wiggle/lib;)

##
## Dialyzer
##
APPS = kernel stdlib sasl erts ssl tools os_mon runtime_tools crypto inets \
xmerl webtool snmp public_key mnesia eunit syntax_tools compiler
COMBO_PLT = $(HOME)/.wiggle_combo_dialyzer_plt

check_plt: deps compile
dialyzer --check_plt --plt $(COMBO_PLT) --apps $(APPS) \
deps/*/ebin apps/*/ebin

build_plt: deps compile
dialyzer --build_plt --output_plt $(COMBO_PLT) --apps $(APPS) \
deps/*/ebin apps/*/ebin

dialyzer: deps compile
@echo
@echo Use "'make check_plt'" to check PLT prior to using this target.
@echo Use "'make build_plt'" to build PLT prior to using this target.
@echo
@sleep 1
dialyzer -Wno_return --plt $(COMBO_PLT) deps/*/ebin apps/*/ebin | grep -v -f dialyzer.mittigate


cleanplt:
@echo
@echo "Are you sure? It takes about 1/2 hour to re-build."
@echo Deleting $(COMBO_PLT) in 5 seconds.
@echo
sleep 5
rm $(COMBO_PLT)
2 changes: 2 additions & 0 deletions apps/wiggle/src/wiggle.erl
Expand Up @@ -2,6 +2,8 @@

-export([start/0]).

-ignore_xref([start/0]).

start() ->
application:start(mdns_client_lib),
application:start(libsnarl),
Expand Down
56 changes: 35 additions & 21 deletions apps/wiggle/src/wiggle_dataset_handler.erl
Expand Up @@ -3,42 +3,56 @@
%% @doc Hello world handler.
-module(wiggle_dataset_handler).

-export([init/3,
-export([init/3,
rest_init/2]).

-export([content_types_provided/2,
-export([content_types_provided/2,
content_types_accepted/2,
allowed_methods/2,
resource_exists/2,
forbidden/2,
options/2,
is_authorized/2]).

-export([to_json/2,
from_json/2]).


-ignore_xref([to_json/2,
from_json/2,
allowed_methods/2,
content_types_accepted/2,
content_types_provided/2,
delete_resource/2,
forbidden/2,
init/3,
is_authorized/2,
options/2,
resource_exists/2,
rest_init/2]).
-record(state, {path, method, version, token, content, reply}).

init(_Transport, _Req, []) ->
{upgrade, protocol, cowboy_http_rest}.

rest_init(Req, _) ->
{Method, Req1} = cowboy_http_req:method(Req),
{[<<"api">>, Version, <<"datasets">> | Path], Req2} = cowboy_http_req:path(Req1),
{Token, Req3} = case cowboy_http_req:header(<<"X-Snarl-Token">>, Req2) of
{undefined, ReqX} ->
{undefined, ReqX} ->
{undefined, ReqX};
{TokenX, ReqX} ->
{ok, ReqX1} = cowboy_http_req:set_resp_header(<<"X-Snarl-Token">>, TokenX, ReqX),
{TokenX, ReqX1}
end,
{ok, Req4} = cowboy_http_req:set_resp_header(<<"Access-Control-Allow-Origin">>, <<"*">>, Req3),
{ok, Req5} = cowboy_http_req:set_resp_header(
<<"Access-Control-Allow-Headers">>,
<<"Access-Control-Allow-Headers">>,
<<"Content-Type, X-Snarl-Token">>, Req4),
{ok, Req6} = cowboy_http_req:set_resp_header(
<<"Access-Control-Expose-Headers">>,
<<"Access-Control-Expose-Headers">>,
<<"X-Snarl-Token">>, Req5),
State = #state{version = Version,
State = #state{version = Version,
method = Method,
token = Token,
path = Path},
Expand All @@ -47,10 +61,10 @@ rest_init(Req, _) ->
options(Req, State) ->
Methods = allowed_methods(Req, State, State#state.path),
{ok, Req1} = cowboy_http_req:set_resp_header(
<<"Access-Control-Allow-Methods">>,
<<"Access-Control-Allow-Methods">>,
string:join(
lists:map(fun erlang:atom_to_list/1,
['HEAD', 'OPTIONS' | Methods]), ", "), Req),
['HEAD', 'OPTIONS' | Methods]), ", "), Req),
{ok, Req1, State}.

content_types_provided(Req, State) ->
Expand All @@ -77,25 +91,25 @@ resource_exists(Req, State = #state{path = []}) ->

resource_exists(Req, State = #state{path = [Dataset]}) ->
case libsniffle:dataset_attribute_get(Dataset) of
{reply, not_found} ->
not_found ->
{false, Req, State};
{reply, _} ->
{ok, _} ->
{true, Req, State}
end.

is_authorized(Req, State = #state{method = 'OPTIONS'}) ->
is_authorized(Req, State = #state{method = 'OPTIONS'}) ->
{true, Req, State};

is_authorized(Req, State = #state{token = undefined}) ->
is_authorized(Req, State = #state{token = undefined}) ->
{{false, <<"X-Snarl-Token">>}, Req, State};

is_authorized(Req, State) ->
is_authorized(Req, State) ->
{true, Req, State}.

forbidden(Req, State = #state{method = 'OPTIONS'}) ->
forbidden(Req, State = #state{method = 'OPTIONS'}) ->
{false, Req, State};

forbidden(Req, State = #state{token = undefined}) ->
forbidden(Req, State = #state{token = undefined}) ->
{true, Req, State};

forbidden(Req, State = #state{path = []}) ->
Expand All @@ -116,11 +130,11 @@ to_json(Req, State) ->
{jsx:encode(Reply), Req1, State1}.

handle_request(Req, State = #state{path = []}) ->
{reply, {ok, Res}} = libsniffle:dataset_list(),
{ok, Res} = libsniffle:dataset_list(),
{Res, Req, State};

handle_request(Req, State = #state{path = [Dataset]}) ->
{reply, Res} = libsniffle:dataset_attribute_get(Dataset),
{ok, Res} = libsniffle:dataset_attribute_get(Dataset),
{Res, Req, State}.


Expand Down Expand Up @@ -148,10 +162,10 @@ handle_write(Req, State, _Body) ->

allowed(Token, Perm) ->
case libsnarl:allowed({token, Token}, Perm) of
{reply,not_found} ->
not_found ->
true;
{reply, true} ->
true ->
false;
{reply, false} ->
false ->
true
end.
44 changes: 29 additions & 15 deletions apps/wiggle/src/wiggle_group_handler.erl
Expand Up @@ -17,9 +17,23 @@
forbidden/2,
options/2,
is_authorized/2]).

-export([to_json/2,
from_json/2]).

-ignore_xref([to_json/2,
from_json/2,
allowed_methods/2,
content_types_accepted/2,
content_types_provided/2,
delete_resource/2,
forbidden/2,
init/3,
is_authorized/2,
options/2,
resource_exists/2,
rest_init/2]).

-record(state, {path, method, version, token, content, reply}).

init(_Transport, _Req, []) ->
Expand Down Expand Up @@ -86,11 +100,11 @@ allowed_methods(_Version, _Token, [_Group, <<"permissions">> | _Permission]) ->

resource_exists(Req, State = #state{path = [Group, <<"permissions">> | Permission]}) ->
case {erlangify_permission(Permission), libsnarl:group_get(Group)} of
{_, {reply, {ok, not_found}}} ->
{_, not_found} ->
{false, Req, State};
{[], {reply, {ok, _}}} ->
{[], {ok, _}} ->
{true, Req, State};
{P, {reply, {ok, {group, _Name, Permissions, _}}}} ->
{P, {ok, {group, _Name, Permissions, _}}} ->
{lists:member(P, Permissions), Req, State}
end;

Expand All @@ -99,9 +113,9 @@ resource_exists(Req, State = #state{path = []}) ->

resource_exists(Req, State = #state{path = [Group | _]}) ->
case libsnarl:group_get(Group) of
{reply, {ok, not_found}} ->
{ok, not_found} ->
{false, Req, State};
{reply, {ok, _}} ->
{ok, _} ->
{true, Req, State}
end.

Expand Down Expand Up @@ -163,16 +177,16 @@ to_json(Req, State) ->
{jsx:encode(Reply), Req1, State1}.

handle_request(Req, State = #state{path = []}) ->
{reply, {ok, Res}} = libsnarl:group_list(),
{ok, Res} = libsnarl:group_list(),
{Res, Req, State};

handle_request(Req, State = #state{path = [Group]}) ->
{reply, {ok, {group, Name, Permissions, _}}} = libsnarl:group_get(Group),
{ok, {group, Name, Permissions, _}} = libsnarl:group_get(Group),
{[{name, Name},
{permissions, lists:map(fun jsonify_permissions/1, Permissions)}], Req, State};

handle_request(Req, State = #state{path = [Group, <<"permissions">>]}) ->
{reply, {ok, {group, _Name, Permissions, _}}} = libsnarl:group_get(Group),
{ok, {group, _Name, Permissions, _}} = libsnarl:group_get(Group),
{lists:map(fun jsonify_permissions/1, Permissions), Req, State}.

%%--------------------------------------------------------------------
Expand All @@ -191,12 +205,12 @@ from_json(Req, State) ->
{Reply, Req2, State1}.

handle_write(Req, State = #state{path = [Group]}, _Body) ->
libsnarl:group_add(Group),
ok = libsnarl:group_add(Group),
{true, Req, State};

handle_write(Req, State = #state{path = [Group, <<"permissions">> | Permission]}, _Body) ->
P = erlangify_permission(Permission),
{reply, ok} = libsnarl:group_grant(Group, P),
ok = libsnarl:group_grant(Group, P),
{true, Req, State}.


Expand All @@ -206,11 +220,11 @@ handle_write(Req, State = #state{path = [Group, <<"permissions">> | Permission]}

delete_resource(Req, State = #state{path = [Group, <<"permissions">> | Permission]}) ->
P = erlangify_permission(Permission),
{reply, ok} = libsnarl:group_revoke(Group, P),
ok = libsnarl:group_revoke(Group, P),
{true, Req, State};

delete_resource(Req, State = #state{path = [Group]}) ->
{reply, ok} = libsnarl:group_delete(Group),
ok = libsnarl:group_delete(Group),
{true, Req, State}.

%% Internal Functions
Expand All @@ -235,11 +249,11 @@ jsonify_permissions(P) ->

allowed(Token, Perm) ->
case libsnarl:allowed({token, Token}, Perm) of
{reply,not_found} ->
not_found ->
true;
{reply, true} ->
true ->
false;
{reply, false} ->
false ->
true
end.

Expand Down

0 comments on commit 943fb74

Please sign in to comment.