Code Examples
Practical examples showing how to use Flowa for real-world applications.
User Authentication System
let users = {};
func register_user(username, password, email) {
if (username in users) {
return false;
}
users[username] = {
"email": email,
"password": auth.hash(password)
};
return true;
}
func login_user(username, password) {
if (!(username in users)) {
return false;
}
let stored_hash = users[username]["password"];
return auth.verify(password, stored_hash);
}
func create_session_token(username) {
let payload = {"username": username};
return jwt.sign(payload);
}REST API Handler
func handle_get_users(req, res) {
let db = sqlite.open("app.db");
let users = sqlite.query(db, "SELECT id, name, email FROM users");
sqlite.close(db);
res.writeHead(200, {"Content-Type": "application/json"});
res.end(json.stringify(users));
}
let server = http.createServer();
server.on("GET", "/api/users", handle_get_users);
server.listen(3000);Performance Benchmark
Flowa execution speed can be measured using the time module. Here is a simple benchmark loop:
print("Starting intensive benchmark...")
run = 0
while (run < 5) {
sum = 0
i = 0
start = time.now_ms()
while (i < 100000000) {
sum = sum + i
i = i + 1
}
elapsed_s = time.since_s(start, 3)
print("Run:", run + 1, "Sum:", sum, "Time:", elapsed_s, "s")
run = run + 1
}
print("Benchmark complete!")More Examples
Check out the Flowa repository (opens in a new tab) for more examples:
examples/01_hello.flowa- Hello Worldexamples/14_server.flowa- HTTP Serverexamples/15_sqlite.flowa- Database operationstests/- Test suite with many examples