-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update the v-server, enable cors (#1548)
* Update the v-server, enable cors * Removed the validation and printed the payload. * Removed unused comment. * Added the Vserver to the server docs.
- Loading branch information
1 parent
24968cb
commit 1535093
Showing
3 changed files
with
46 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 18 additions & 16 deletions
34
packages/UI/examples/server-example/vlang-server/local_server/handlers.v
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,31 @@ | ||
module local_server | ||
|
||
import vweb | ||
import time | ||
import x.json2 | ||
|
||
|
||
pub fn (app &VServerApp) before_request() { | ||
pub fn (mut app VServerApp) before_request() { | ||
eprintln('${time.now()}: ${app.req.method} ${app.req.url}') | ||
} | ||
|
||
@['/api/verify'; post] | ||
@[options;'/:path...'] | ||
pub fn (mut app VServerApp) preflight(path string) vweb.Result { | ||
app.add_header('Vary', 'Access-Control-Request-Headers') | ||
app.add_header('Access-Control-Allow-Methods', 'GET,HEAD,PUT,PATCH,POST,DELETE') | ||
app.add_header('Access-Control-Allow-Headers', 'Content-Type') | ||
app.add_header('Access-Control-Allow-Origin', '*') | ||
|
||
return app.text('options are ok') | ||
} | ||
|
||
@['/api/verify'; POST] | ||
pub fn (mut app VServerApp) verify() vweb.Result { | ||
data := app.req.data | ||
payload := json2.decode[Payload](data) or { | ||
app.set_status(400, "") | ||
return app.json({ | ||
"Error": "true", | ||
"message": "Please make sure that you sent a valid date." | ||
"required_fields": Payload{}.str() | ||
"status": '400' | ||
}) | ||
} | ||
|
||
println(payload) | ||
app.add_header('Access-Control-Allow-Origin', '*') | ||
app.set_status(200, "") | ||
data := app.req.data | ||
println("Payload: ${data}") | ||
return app.json({ | ||
"data": "id" | ||
"data": data | ||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters