New rocket.rs app

This commit is contained in:
Carolyn Knight-Serrano 2019-12-18 10:24:53 -08:00
parent 268dd63423
commit 14641b7696
No known key found for this signature in database
GPG Key ID: 50858748146544CB
10 changed files with 1480 additions and 84 deletions

17
.gitignore vendored
View File

@ -34,3 +34,20 @@ _testmain.go
*.exe
*.test
*.prof
#Added by cargo
#
#already existing elements are commented out
/target
**/*.rs.bk
#Added by cargo
#
#already existing elements were commented out
#/target
TAGS

1327
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

22
Cargo.toml Normal file
View File

@ -0,0 +1,22 @@
[package]
name = "pinkblackrose"
version = "0.1.0"
authors = ["Carolyn Knight-Serrano <gigavinyl@riseup.net>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rocket = "0.4.2"
juniper = "0.14.2"
dotenv = "0.9.0"
rincon = "0.1.0"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
[dependencies.rocket_contrib]
version = "0.4.2"
default-features = false
features = ["handlebars_templates", "tera_templates"]

25
Gopkg.lock generated
View File

@ -1,25 +0,0 @@
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
[[projects]]
digest = "1:cca84f57a4b9505914704f7c93937573763bb9a656eb006e144e81615b5f3cb6"
name = "gitea.com/lunny/log"
packages = ["."]
pruneopts = "UT"
revision = "01b5df579c4e3c867d69e733e6e532b2923e711c"
version = "v0.2"
[[projects]]
digest = "1:7d6017c139cce637f0dbe70361addf48a5a8c132b062bd816b17dfbcba8e5530"
name = "gitea.com/lunny/tango"
packages = ["."]
pruneopts = "UT"
revision = "af459469e09da76b00b310365998396b33ea4aa9"
version = "v0.6.2"
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
input-imports = ["gitea.com/lunny/tango"]
solver-name = "gps-cdcl"
solver-version = 1

View File

@ -1,34 +0,0 @@
# Gopkg.toml example
#
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true
[prune]
go-tests = true
unused-packages = true
[[constraint]]
name = "gitea.com/lunny/tango"
version = "0.6.2"

View File

@ -1,25 +0,0 @@
package main
import (
"errors"
"gitea.com/lunny/tango"
)
type Action struct {
tango.JSON
}
func (Action) Get() interface{} {
if true {
return map[string]string{
"say": "Hello tango!",
}
}
return errors.New("something error")
}
func main() {
t := tango.Classic()
t.Get("/", new(Action))
t.Run()
}

73
src/main.rs Normal file
View File

@ -0,0 +1,73 @@
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use] extern crate rocket;
#[macro_use] extern crate serde_derive;
use rocket::Request;
use rocket::response::Redirect;
use rocket_contrib::templates::{Template, handlebars};
#[derive(Serialize)]
struct TemplateContext {
title: &'static str,
name: Option<String>,
items: Vec<&'static str>,
// This key tells handlebars which template is the parent.
parent: &'static str,
}
#[get("/")]
fn index() -> Redirect {
Redirect::to("/hello/Unknown")
}
#[get("/hello/<name>")]
fn hello(name: String) -> Template {
Template::render("index", &TemplateContext {
title: "Hello",
name: Some(name),
items: vec!["One", "Two", "Three"],
parent: "layout",
})
}
#[catch(404)]
fn not_found(req: &Request<'_>) -> Template {
let mut map = std::collections::HashMap::new();
map.insert("path", req.uri().path());
Template::render("error/404", &map)
}
use self::handlebars::{Helper, Handlebars, Context, RenderContext, Output, HelperResult, JsonRender};
fn wow_helper(
h: &Helper<'_, '_>,
_: &Handlebars,
_: &Context,
_: &mut RenderContext<'_>,
out: &mut dyn Output
) -> HelperResult {
if let Some(param) = h.param(0) {
out.write("<b><i>")?;
out.write(&param.value().render())?;
out.write("</b></i>")?;
}
Ok(())
}
fn rocket() -> rocket::Rocket {
rocket::ignite()
.mount("/", routes![index, hello])
.register(catchers![not_found])
.attach(Template::custom(|engines| {
engines.handlebars.register_helper("wow", Box::new(wow_helper));
}))
}
fn main() {
rocket().launch();
}

11
templates/error/404.hbs Normal file
View File

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>404</title>
</head>
<body>
<h1>404: Hey! There's nothing here.</h1>
The page at {{ path }} does not exist!
</body>
</html>

19
templates/index.hbs Normal file
View File

@ -0,0 +1,19 @@
{{#*inline "page"}}
<section id="hello">
<h1>Hi {{ name }}!</h1>
<h3>Here are your items:</h3>
<ul>
{{#each items}}
<li>{{ this }}</li>
{{/each}}
</ul>
</section>
<section id="custom-helper">
<p>Try going to <a href="/hello/YourName">/hello/YourName</a>.</p>
<p>Also, check {{ wow "this" }} (custom helper) out!</p>
</section>
{{/inline}}
{{~> (parent)~}}

11
templates/layout.hbs Normal file
View File

@ -0,0 +1,11 @@
<!doctype html>
<html>
<head>
<title>Rocket Example - {{ title }}</title>
</head>
<body>
{{> nav}}
{{~> page}}
{{> footer}}
</body>
</html>