fix parsing
This commit is contained in:
38
goal-examples/webapp/main.bl
Normal file
38
goal-examples/webapp/main.bl
Normal file
@@ -0,0 +1,38 @@
|
||||
import net.http as http;
|
||||
import logging as logging;
|
||||
import json as json;
|
||||
|
||||
|
||||
type ExampleResponse struct {
|
||||
pub id: i32;
|
||||
pub name: str;
|
||||
pub email: str;
|
||||
}
|
||||
|
||||
type Router struct {
|
||||
logger: logging::Logger;
|
||||
|
||||
pub fn new(logger: logging::Logger): Router {
|
||||
return Self{logger: logger};
|
||||
}
|
||||
|
||||
pub fn get_user_data(self: Self, req: http.Request): IO[Result[http::Response, http::Error]] {
|
||||
let response_data = ExampleResponse{
|
||||
id: 4,
|
||||
name: "Andrew",
|
||||
email: "andrew@boringlang.com",
|
||||
};
|
||||
self.logger.info("getting user data")?;
|
||||
return ok(http::Response::ok(json::dumps(response_data)?));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn main(args: List[String], os: OS): IO[i32] {
|
||||
let logger = logging::ConsoleLogger::new(os.console.stdout());
|
||||
let router = Router::new(logger);
|
||||
let app = http::Router::new("").add_route("/myroute", router.get_user_data);
|
||||
let http_server = http::Server::new(os.net(), "localhost", 8080, app);
|
||||
let err = http_server.serve_forever()?;
|
||||
logger.info("error serving: ", err)?;
|
||||
return 1;
|
||||
}
|
||||
33
goal-examples/webapp/models.bl
Normal file
33
goal-examples/webapp/models.bl
Normal file
@@ -0,0 +1,33 @@
|
||||
import uuid as uuid;
|
||||
import orm as orm;
|
||||
|
||||
|
||||
#[derive(DeepCopy, Debug, PartialEq, DeriveEntityModel)]
|
||||
#[orm::model(table_name = "user")]
|
||||
pub type User struct {
|
||||
#[orm::model(primary_key)]
|
||||
pub id: uuid::UUID;
|
||||
#[orm::model(unique, column_type = "VARCHAR(256)")]
|
||||
pub email: String;
|
||||
#[orm::model(nullable)]
|
||||
pub password_hash: Option[String];
|
||||
}
|
||||
|
||||
#[derive(DeepCopy, Debug, PartialEq, DeriveEntityModel)]
|
||||
#[orm::model(table_name = "todo")]
|
||||
pub type Todo struct {
|
||||
#[orm::model(primary_key)]
|
||||
pub id: uuid::UUID;
|
||||
|
||||
#[orm::model(foreign_key = "user", on_delete="CASCADE")]
|
||||
pub user_id: uuid::UUID;
|
||||
|
||||
#[orm::model(column_type = "VARCHAR(256)")]
|
||||
pub title: String;
|
||||
|
||||
#[orm::model(column_type = "TEXT")]
|
||||
pub description: String;
|
||||
|
||||
pub created_at: DateTime;
|
||||
pub updated_at: DateTime;
|
||||
}
|
||||
45
goal-examples/webapp/router.bl
Normal file
45
goal-examples/webapp/router.bl
Normal file
@@ -0,0 +1,45 @@
|
||||
import logging as logging;
|
||||
import orm as orm;
|
||||
import uuid as uuid;
|
||||
import webapp.models as models;
|
||||
|
||||
#[derive(DeepCopy, Debug, PartialEq)]
|
||||
type TodoGetParams struct {
|
||||
#[validate(format="short")]
|
||||
pub id: uuid.UUID;
|
||||
}
|
||||
|
||||
#[derive(DeepCopy, Debug, PartialEq, Serialize)]
|
||||
type TodoResponse struct {
|
||||
#[validate(format="short")]
|
||||
pub id: uuid.UUID;
|
||||
#[validate(format="short")]
|
||||
pub user_id: uuid.UUID;
|
||||
#[validate(min_length=1, max_length=256)]
|
||||
pub title: String;
|
||||
#[validate(min_length=1, max_length=1000000)]
|
||||
pub description: String;
|
||||
pub created_at: DateTime;
|
||||
pub updated_at: DateTime;
|
||||
}
|
||||
|
||||
impl Into<TodoResponse> for models.Todo {
|
||||
fn into(self: Self): TodoResponse {
|
||||
return TodoResponse{..self};
|
||||
}
|
||||
}
|
||||
|
||||
type Router struct {
|
||||
logger: logging::Logger;
|
||||
db: orm::DB;
|
||||
|
||||
pub fn new(logger: logging::Logger, db: orm::DB): Router {
|
||||
return Self{logger: logger, db: db};
|
||||
}
|
||||
|
||||
pub fn get_todo(self: Self, req: http.Request[TodoGetParams, http::NoQuery, http::NoBody]): IO[Result[http::JsonResponse[TodoResponse], http::Error]] {
|
||||
let id = req.params.id;
|
||||
let instance = models::Todo::select().filter(models::Todo::Id::equals(id)).first(self.db)??;
|
||||
return http::JsonResponse<TodoResponse>::ok(instance.into());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user