vcreate: migrate vweb model to veb#26745
Conversation
…g from vweb to veb
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 54cc8f6af7
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| defer { db.close() or { panic(err) } } | ||
|
|
||
| mut app := &App{ db: db } | ||
| app.handle_static('static', true)! |
There was a problem hiding this comment.
Resolve static dir independently of process cwd
The generated main.v now uses app.handle_static('static', true)!, but veb.StaticHandler.handle_static resolves that folder relative to the current working directory. This means the scaffolded app will panic at startup when launched from any other directory (for example via an absolute binary path from a shell/systemd), because static is not found there. The previous template used executable-relative paths, so this is a runtime regression in common deployment scenarios.
Useful? React with 👍 / 👎.
|
Close/re-open for CI. |
|
Need to run |
It will sometimes pick single quotes, sometimes double quotes in the Here's an example, this one is changed: c.files << ProjectFiles{
path: os.join_path(base, 'auth_controller.v')
content: 'module main
import crypto.bcrypt
import crypto.hmac
import encoding.base64
import crypto.sha256
import json
fn (app &App) do_auth(username string, password string) !string {
users := sql app.db {
select from User where username == username
}!
if users.len == 0 {
return error(\'user not found\')
}
user := users.first()
if !user.active {
return error(\'user is not active\')
}
bcrypt.compare_hash_and_password(password.bytes(), user.password.bytes()) or {
return error(\'Failed to auth user, \${err}\')
}
return make_token(user)
}
fn (app &App) verify_auth(token string) bool {
if token == \'\' {
return false
}
secret := \'SECRET_KEY\' // os.getenv(\'SECRET_KEY\')
token_split := token.split(\'.\')
signature_mirror := hmac.new(secret.bytes(), \'\${token_split[0]}.\${token_split[1]}\'.bytes(),
sha256.sum, sha256.block_size).bytestr().bytes()
signature_from_token := base64.url_decode(token_split[2])
return hmac.equal(signature_from_token, signature_mirror)
}
fn (app &App) user_auth(token string) !int {
if !app.verify_auth(token) {
return error("Invalid token")
}
jwt_payload_stringify := base64.url_decode_str(token.split(\'.\')[1])
jwt_payload := json.decode(JwtPayload, jwt_payload_stringify) or {
return err
}
return jwt_payload.sub.int()
}
'While this one is not: c.files << ProjectFiles{
path: os.join_path(base, 'user_controller.v')
content: "module main
import crypto.bcrypt
fn (app &App) add_user(username string, password string) ! {
hashed_password := bcrypt.generate_from_password(password.bytes(), bcrypt.min_cost)!
user_model := User{
username: username
password: hashed_password
active: true
}
sql app.db {
insert user_model into User
}!
}
fn (app &App) get_users() ![]User {
return sql app.db {
select from User
}!
}
fn (app &App) get_user(id int) !User {
results := sql app.db {
select from User where id == id
}!
if results.len == 0 {
return error('no results')
}
return results[0]
}
" |
|
That first example does look odd.
It should not create strings with Can you manually modify the first example to have |
|
That's actually what I did prior to running My current assessment is that the clause dictating, if there are I will be pushing the changes soon. This was my fault as this first example had a string with double quotes whereas As a side note, would it be of help if I developed a simple |
…iles inside model and on the model file itself
|
That's basically what One change you could make is to extract those strings into separate files (each template project as a separate subdir, perhaps), and use That way, you could format them once, then not have to worry about them again, unless the template files themselves change. |
|
Merging what's here... can always do a new PR for changes. |
See issue #24270 for context.
This PR migrates the example project used as a demo for the
vweblibrary into theveblibrary.The structure has been slightly altered to follow more consistent web development standards, and align more closely with the code shown in the
vdoc. However, functionality has been kept identical.As a side note, it would perhaps be useful in the future to have a better system for the creation of such example projects, rather than pasting the full code into one file. For simplicity, this is fine for now.