Skip to content

Instantly share code, notes, and snippets.

@takkyuuplayer
Created July 25, 2021 08:21
Show Gist options
  • Save takkyuuplayer/4887f27ec80245fc120d6aefc7636ea0 to your computer and use it in GitHub Desktop.
Save takkyuuplayer/4887f27ec80245fc120d6aefc7636ea0 to your computer and use it in GitHub Desktop.
module main
import vweb
struct App {
vweb.Context
alphabets []string
}
fn new_app(alphabets []string) &App {
assert alphabets == ['a', 'b', 'c']
return &App{
alphabets: alphabets
}
}
fn (mut app App) index() vweb.Result {
return app.text('alphabets: ${app.alphabets}')
}
fn main() {
vweb.run(new_app(['a', 'b', 'c']), 8080)
}
@takkyuuplayer
Copy link
Author

takkyuuplayer commented Jul 25, 2021

$ v run /tmp/foo.v
[Vweb] Running app on http://localhost:8080
$ curl http://localhost:8080
alphabets: []

@takkyuuplayer
Copy link
Author

takkyuuplayer commented Jul 25, 2021

I found changing alphabets to shared works, while I'm not sure if it's the right approach.

module main

import vweb

struct App {
	vweb.Context
mut:
	alphabets shared []string
}

fn new_app(alphabets []string) &App {
	assert alphabets == ['a', 'b', 'c']

	mut app := &App{}

	lock app.alphabets {
		app.alphabets = alphabets
	}

	return app
}

fn (mut app App) index() vweb.Result {
	text := rlock app.alphabets {
		'alphabets: $app.alphabets'
	}
	return app.text(text)
}

fn main() {
	vweb.run(new_app(['a', 'b', 'c']), 8080)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment