teodevgroup / teo

Schema-driven web server framework.
https://teodev.io
Apache License 2.0
1.39k stars 46 forks source link

Add cookies to the Request and Response object #58

Open akelyasir opened 4 months ago

akelyasir commented 4 months ago

Hello, I had a few questions about the system.

  1. Can we add HTML template support like Tera?
  2. Is there a cookie-based session?
  3. What method should we follow to send an e-mail?
  4. Can we add a job queue process?
  5. Generated customizable admin Dashboard. How will we create it?

Thanks

victorteokw commented 4 months ago

Hi @akelyasir

Answer your questions.

  1. Integrate by yourself. Teo with custom programming code behaves just like any vanilla web frameworks.
  2. Temporarily cookie and session can't be accessed directly on the Request object. If you need it, I will add this feature in one day.
  3. Integrate by yourself. None of web frameworks provide email sender. Use an open source package to send.
  4. Use custom program feature of Teo.
  5. You can try it out with teo generate admin.
akelyasir commented 4 months ago

Hi @victorteokw

Thank you very much for your answers. You have done a great job, congratulations. I'm still very new to the Rust programming language. I saw that you use actix-web based on Teo. I'm looking forward to your example and plugin about using cookie sessions. The reason I asked about Tera was because you were basically using actix-web, I was just wondering if it was possible to add it and it would be nice if it were. Of course, you know the system much better, I have just discovered Teo. I am thinking of integrating Rust Lettre regarding mail, but I would appreciate it if you could give me some information on where to start. Finally, I would be happy if you share whether it is possible to add all the features of Actix-web or everything integrated to Teo.

Thanks and regards

victorteokw commented 4 months ago

Hi @akelyasir, if you send emails inside a route, just write route handler to handle email sending. Otherwise, use custom pipeline item. Follow the 4 tutorials from the official docs: https://docs.teodev.io/getting-started/beginner-tutorial/write-a-schema-only-app, and you will be able to write custom pipeline item.

victorteokw commented 4 months ago

I will add sessions and cookies tomorrow. A new version will be published.

akelyasir commented 4 months ago

@victorteokw

thank you for your interest I tried to create it with the cargo teo generate admin command, but there was no result. Do I need to make any extra settings? How can I reach

victorteokw commented 4 months ago

Hi @akelyasir, try this demo, replace schema.teo with this:

connector {
  // SQLite
  // provider: .sqlite,
  // url: "sqlite:./database.sqlite",

  // MySQL
  // provider: .mysql,
  // url: "mysql://localhost:3306/adminbackend",

  // PostgreSQL
  provider: .postgres,
  url: "postgres://localhost:5432/adminbackend",
}

server {
  bind: ("0.0.0.0", 5052)
}

admin {
  dest: "../teo-admin-dev",
  host: .inject("process.env.TEO_HOST"),
  languages: [.enUs, .enUk, .de, .es, .fr, .he, .hi, .ja, .ko, .zhCn, .zhTw]
}

@identity.tokenIssuer($identity.jwt(expired: 3600 * 24 * 365))
@identity.jwtSecret(ENV["JWT_SECRET"]!) @admin.administrator
model Root {
  @id @autoIncrement @readonly
  id: Int
  @unique @onSet($if($presents, $isEmail)) @identity.id
  email: String
  @writeonly @onSet($presents.bcrypt.salt) @admin.secureInput
  @identity.checker($get(.value).presents.bcrypt.verify($self.get(.password).presents))
  password: String

  include handler identity.signIn
  include handler identity.identity
}

@identity.tokenIssuer($identity.jwt(expired: 3600 * 24 * 365))
@identity.jwtSecret(ENV["JWT_SECRET"]!) @admin.administrator
model Admin {
  @id @autoIncrement @readonly
  id: Int
  @unique @onSet($if($presents, $isEmail)) @identity.id
  email: String
  @unique @identity.id
  phoneNo: String
  @writeonly @onSet($presents.bcrypt.salt) @admin.secureInput
  @identity.checker($get(.value).presents.bcrypt.verify($self.get(.password).presents))
  password: String

  include handler identity.signIn
  include handler identity.identity
}

model Item {
  @id @autoIncrement @readonly
  id: Int
  @admin.title
  name: String
}

model Product {
  @id @autoIncrement @readonly
  id: Int
  name: String
  stock: Int
  @foreignKey
  categoryId: Int
  @relation(fields: .categoryId, references: .id)
  category: Category
}

model Category {
  @id @autoIncrement @readonly
  id: Int
  name: String
  @relation(fields: .id, references: .categoryId)
  products: Product[]
}

enum Sex {
  male
  female
}

model Record {
  @id @autoIncrement @readonly
  id: Int
  string: String
  bool: Bool
  int: Int
  float: Float
  decimal: Decimal
  date: Date
  dateTime: DateTime
  sex: Sex
  strings: String[]
  genders: Sex[]
}

middlewares [identity.identityFromJwt(secret: ENV["JWT_SECRET"]!)]

autoseed dataset default {
  group Admin {
    record admin {
      email: "admin@gmail.com",
      phoneNo: "13588888888",
      password: "Aa123456"
    }
  }
  group Root {
    record root {
      email: "root@gmail.com",
      password: "Aa123456"
    }
  }
}
victorteokw commented 4 months ago

Remove array type fields if you are not using PostgreSQL.

victorteokw commented 4 months ago

Hi @akelyasir, I added cookies to the Rust Request object. However, it's a bit cumbersome to add it to the Response object. In the future 0.3.0 version, we will use hyper to rewrite the HTTP layer. At that time, full features of cookies will be supported.

akelyasir commented 4 months ago

Hi @victorteokw

Thank you for your attention. I couldn't run the schema you sent and couldn't find the problem.

Error: Error { code: 500, message: "teo_result::Error: {\"code\":500,\"message\":\"Cannot convert Null into String\",\"errors\":null}", errors: None, platform_native_object: None }

I am getting an error like this. I am using Postgresql.

connector {
  provider: .postgres,
  url: "postgres://username:password@localhost:5432/database",
}
victorteokw commented 4 months ago

Hi @akelyasir, create a file named .env in the same directory: JWT_SECRET=mytopsecret

akelyasir commented 4 months ago

Hi @victorteokw

Thank you very much. I forgot to create the env file.

victorteokw commented 2 months ago

Hi @akelyasir, Cookie APIs are added to the Rust Response API in the 0.3.0-alpha.0 version.