script 0.3.1

barebones http scripting
index {
   text(":3\nwelcome to the root")
}

hello() {
   response("Hello World!", "text", 200)
}

#[route("/example")]
example() {
   html(http::get("https://example.org").body)
}

#[route("/db")]
db() {
   let db = kv::load("test.db");
   
   db.set("some.key", json::dump(#{name: "John", id: 50}));
   let data = json::parse(db.get("some.key"));
   
   json(data)
}

#[route("/redis")]
redis() {
   let db = redis::connect();
   
   db.set("some.key", json::dump(#{name: "John Doe", id: 50}));
   let data = json::parse(db.get("some.key"));
 
   json(data)
}

#[route("/mongo/list")]
mongo() {
   let conn = mongo::connect();
   
   json(conn.list())
}

#[route("/mongo/{name}")]
mongo(name) {
   let conn = mongo::connect();
   let db = conn.db(name);
   
   json(db.list())
}

#[route("/mongo/{name}/{collection}")]
mongo(name, collection) {
   let conn = mongo::connect().db(name).get(collection);
   let list = conn.find().collect();
      
   json(#{count: list.count(), items: list})
}

#[route("/mongo/test")]
test() {
   let conn = mongo::connect().db("app").create("users");
   
   conn.insert([
      #{firstname: "John", lastname: "Doe", id: 50},
      #{firstname: "John", lastname: "Doe", id: 51},
   ]);
   
   let list = conn.find(#{firstname: "John"}).collect();
   conn.delete_many(#{firstname: "John"});
   
   json(list)
}

#[route("/example/{id}.txt")]
example(id) {
   text("base: " + id)
}

#[route("/example/{id}/test")]
example_test(id) {
   text("sub: " + id)
}

#[route("/name/{id}/{name}")]
name(id, name) {
   json(#{name: name, id: id})
}

#[route("/proxy/{arg}")]
fetch_proxy(arg) {
   proxy(`https://internal.themackabu.dev/$(arg)`)
}

example/json() {
   let res = http::get("https://httpbin.org/json");
   let body = #{ 
      response: res.json(),
      info: #{
         length: res.length,
         status: res.status,
         error: res.error,
         body: res.body,
      },
   };
   
   json(body)
}

example/post/bin() {
   let body = #{ 
      hello: "world",
      url: url,
   };
   
   json(http::post("https://httpbin.org/post", body).raw())
}

test.json() {
   let res = #{
       hello: "world",
       info: #{
         path: request.path,
         url: request.url,
         ver: request.version,
         query: request.query,
       }
   };
   
   json(res)
}

test/loadfile() {
   let file = open_file("test.html");
   html(file.read_string())
}

// remove to test 404 route
* {
   text("this is a wildcard route\ncurrently on: " + request.path)
}

404 {
   text("404 page")
}