Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support placing API files in different folders #21581

Closed
1 of 2 tasks
Avey777 opened this issue May 27, 2024 · 17 comments
Closed
1 of 2 tasks

Support placing API files in different folders #21581

Avey777 opened this issue May 27, 2024 · 17 comments
Labels
Feature/Enhancement Request This issue is made to request a feature or an enhancement to an existing one.

Comments

@Avey777
Copy link

Avey777 commented May 27, 2024

Describe the feature

#21196

image

Use Case

If I store all api files in a single folder, as the number of apis increases, the organization and management of the files will become very confusing.

Proposed Solution

image

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

Version used

0.4.6

Environment details (OS name and version, etc.)

macos
linux
windows

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

@Avey777 Avey777 added the Feature/Enhancement Request This issue is made to request a feature or an enhancement to an existing one. label May 27, 2024
@JalonSolov
Copy link
Contributor

This would definitely be a breaking change.

A fundamental aspect of V is that you cannot create methods on structs in other modules.

This is why all your api methods on the App struct must be in the same module as the file where your App struct is defined.

Those api methods could be tiny wrappers that simply call functions in other modules, but the main api method definition must be in the same module.

@Avey777
Copy link
Author

Avey777 commented May 28, 2024

This would definitely be a breaking change.

A fundamental aspect of V is that you cannot create methods on structs in other modules.

This is why all your api methods on the App struct must be in the same module as the file where your App struct is defined.

Those api methods could be tiny wrappers that simply call functions in other modules, but the main api method definition must be in the same module.

I understand your concerns, but we can consider other solutions to achieve file organization and management in this way. For large projects this is very important. No one wants their code directory to be a mess

@Avey777
Copy link
Author

Avey777 commented May 28, 2024

image

image
I imported the App and Context structures in the api module, allowing the code and no error message; but when I access this api it will return “404 Not Found”

@spytheman
Copy link
Member

I understand your concerns, but we can consider other solutions to achieve file organization and management in this way. For large projects this is very important. No one wants their code directory to be a mess

I understand your desire to organize your code in that way, but it has many disadvantages, in terms of complexity for the compiler.

@spytheman spytheman closed this as not planned Won't fix, can't repro, duplicate, stale May 29, 2024
@Avey777
Copy link
Author

Avey777 commented May 30, 2024

I understand your concerns, but we can consider other solutions to achieve file organization and management in this way. For large projects this is very important. No one wants their code directory to be a mess

I understand your desire to organize your code in that way, but it has many disadvantages, in terms of complexity for the compiler.

But I did this and the compiler didn't complain; I think this should be fixed.

@Avey777
Copy link
Author

Avey777 commented May 30, 2024

@spytheman
Are there any other roundabout ways to achieve similar code organization and management?

@medvednikov
Copy link
Member

I think this was suggested before. "Virtual submodules" or something. There was a long discussion on GitHub.

@medvednikov
Copy link
Member

It can be set up via v.mod. I'm personally not against this feature, but many are against due to complications.

@Avey777
Copy link
Author

Avey777 commented Jun 1, 2024

It can be set up via v.mod. I'm personally not against this feature, but many are against due to complications.

Can you give me an example to show me how to do it

@Avey777
Copy link
Author

Avey777 commented Jun 12, 2024

It can be set up via v.mod. I'm personally not against this feature, but many are against due to complications.

image
Is it an implementation similar to Rust? Can you give an example?

@Avey777
Copy link
Author

Avey777 commented Jun 12, 2024

This is not possible
image

This is ok(This is possible, but it still doesn't achieve the purpose of putting V files in different folders)
image

@Avey777
Copy link
Author

Avey777 commented Jun 24, 2024

image
Is it possible to encapsulate it in a very concise way like go?

@Avey777
Copy link
Author

Avey777 commented Jun 25, 2024

@["/1688_form/";post]
pub fn (app &App) pf1688_form(mut ctx Context, offer_id_i64 i64) veb.Result {
	log.info("执行函数:${@METHOD} ${@MOD}.${@FILE_LINE}")

	if offer_id_i64 <= 0 {
		eprintln(offer_id_i64)
		return ctx.json({"code": "100", "msg": "offer_id 必须是整数"})
	}

	sqldata := mall_1688(offer_id_i64) or { return ctx.json({"code": "102", "msg": "数据库链接错误"}) }
	if sqldata.len == 0 {
		return ctx.json({"code": "103", "msg": "数据库查询失败"})
	}

	return ctx.json(sqldata[0])
}

Is this type of encapsulated code block still too large, can it be encapsulated the same as the one mentioned above? One line is an API
@JalonSolov

@nkev
Copy link

nkev commented Jan 23, 2025

This issue is the only reason we don't build website backends and APIs with V. We use Go instead.
It's a fundamental requirement to organize handlers into own logical modules, like we can in any other language.

@medvednikov
Copy link
Member

@nkev V and Go have a very similar modular system. Can you please elaborate what Go allows you to do that V doesn't?

@nkev
Copy link

nkev commented Jan 24, 2025

@medvednikov Sure. The Go code from Avey777 above shows the issue well with gt_path.Translation whereTranslation is the handler and gt_path is a separate imported Go package.

It's very convenient to separate all the routes from main.v into a separate package, but I could never do it in vweb when I last tried last year.

So from main.v I would like to call the package routes.init_router() to initialize all routes, like this:

Image

If this is possible with veb we would love to see an example.

@medvednikov
Copy link
Member

I see, yes it's now possible in veb using controllers:

    mut api_controller := &other_module.Api{
        db: app.db
    }
    app.register_controller[Api, Context]('/api', mut api_controller)!
    app.use(veb.cors[Context](cors_options))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature/Enhancement Request This issue is made to request a feature or an enhancement to an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants