Skip to content

Commit

Permalink
feat(playground): playground back-end (#222)
Browse files Browse the repository at this point in the history
This allows for sharing, loading and flagging playgrounds on MDN. We use gists as the storage back-end but track them in our db to mitigate and add features in the future.
  • Loading branch information
fiji-flo authored Jun 6, 2023
1 parent 0c87082 commit 04a67ea
Show file tree
Hide file tree
Showing 21 changed files with 722 additions and 4 deletions.
7 changes: 6 additions & 1 deletion .settings.test.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,9 @@ statsd_port = 8125

[basket]
api_key = "foobar"
basket_url = "http://localhost:4321"
basket_url = "http://localhost:4321"

[playground]
github_token = "foobar"
crypt_key = "IXAe2h1MekK4LKysmMvxomja69PT6c20A3nmcDHQ2eQ="
flag_repo = "flags"
198 changes: 196 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ serde_with = { version = "3", features = ["base64"]}
serde_urlencoded = "0.7"
form_urlencoded = "1"
serde_path_to_error = "0.1"
percent-encoding = "2"

config = "0.13"
hostname = "0.3"
Expand Down Expand Up @@ -69,6 +70,9 @@ sentry-actix = "0.31"

basket = "0.0.5"

octocrab = "0.25"
aes-gcm = { version = "0.10", features = ["default", "std"] }

[dev-dependencies]
stubr = "0.5"
stubr-attributes = "0.5"
Expand Down
3 changes: 3 additions & 0 deletions migrations/2023-06-02-151013_playground-share/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
DROP TRIGGER set_deleted_user_id ON playground;
DROP FUNCTION set_deleted_user_id;
DROP TABLE playground;
25 changes: 25 additions & 0 deletions migrations/2023-06-02-151013_playground-share/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
CREATE TABLE playground (
id BIGSERIAL PRIMARY KEY,
user_id BIGINT NULL REFERENCES users (id) ON DELETE SET NULL,
gist TEXT NOT NULL UNIQUE,
active BOOLEAN NOT NULL DEFAULT TRUE,
flagged BOOLEAN NOT NULL DEFAULT FALSE,
deleted_user_id BIGINT DEFAULT NULL
);

CREATE INDEX playground_gist ON playground (gist);

CREATE FUNCTION set_deleted_user_id() RETURNS trigger AS $$
BEGIN
IF OLD.user_id IS NOT NULL AND NEW.user_id IS NULL THEN
NEW.deleted_user_id := OLD.user_id;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER set_deleted_user_id
BEFORE UPDATE ON playground
FOR EACH ROW
WHEN (OLD.user_id IS NOT NULL AND NEW.user_id IS NULL)
EXECUTE FUNCTION set_deleted_user_id();
11 changes: 11 additions & 0 deletions src/api/api_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::api::newsletter::{
is_subscribed, subscribe_anonymous_handler, subscribe_handler, unsubscribe_handler,
};
use crate::api::ping::ping;
use crate::api::play::{flag, load, save};
use crate::api::root::root_service;
use crate::api::search::search;
use crate::api::settings::update_settings;
Expand All @@ -10,6 +11,9 @@ use actix_web::dev::HttpServiceFactory;
use actix_web::web;

pub fn api_v1_service() -> impl HttpServiceFactory {
let json_cfg_1mb_limit = web::JsonConfig::default()
// limit request payload size to 1MB
.limit(1_048_576);
web::scope("/api/v1")
.service(
web::scope("/plus")
Expand All @@ -25,5 +29,12 @@ pub fn api_v1_service() -> impl HttpServiceFactory {
.service(web::resource("/whoami").route(web::get().to(whoami)))
.service(web::resource("/ping").route(web::post().to(ping)))
.service(web::resource("/newsletter").route(web::post().to(subscribe_anonymous_handler)))
.service(
web::scope("/play")
.app_data(json_cfg_1mb_limit)
.service(web::resource("/").route(web::post().to(save)))
.service(web::resource("/flag").route(web::post().to(flag)))
.service(web::resource("/{gist_id}").route(web::get().to(load))),
)
.service(root_service())
}
Loading

0 comments on commit 04a67ea

Please sign in to comment.