Giter VIP home page Giter VIP logo

zig-fermyon-router's Introduction

zig-fermyon-router

The purpose of this project is to provide a simple, Go-like interface for building applications on Fermyon Spin. This is achieved by leveraging WAGI (with plans to use the Spin WIT Worlds in the future) to handle requests and build an easy-to-use interface.

Requirements

  • Zig 0.12.0
  • Fermyon Spin

Note: You can also try this out using Nix but cloning the repo and then running nix develop

Getting Started

  1. Add your imports
const fermyon_router = @import("zig-fermyon-router");
const HttpRequest = fermyon_router.HttpRequest;
const HttpResponse = fermyon_router.HttpResponse;
const Router = fermyon_router.Router;
  1. Build a route handler like.
pub fn testHandler(_: HttpRequest, response: *HttpResponse) void {
    _ = response.write("Hello, world!") catch |err| {
        std.debug.print("Error writing to response: {any}\n", .{err});
    };
}
  1. Add the route to the router.
pub fn main() !void {
    var allocator = std.heap.wasm_allocator;
    var router = Router.new(allocator);
    defer router.deinit();

    try router.addRoute("/api/testing", testHandler);
    try router.routeRequest();
}
  1. Add the following to your build.zig to build your router and generate your spin.toml:
const std = @import("std");
const spin = @import("zig-fermyon-router");

pub fn build(b: *std.Build) void {
    const target = b.resolveTargetQuery(.{ .os_tag = .wasi, .cpu_arch = .wasm32 });
    const optimize = b.standardOptimizeOption(.{});

    const fsr = b.dependency("zig-fermyon-router", .{});

    const exe = b.addExecutable(.{
        .name = "<name of your executable>",
        .root_source_file = .{ .path = "src/main.zig" },
        .target = target,
        .optimize = optimize,
    });
    exe.root_module.addImport("zig-fermyon-router", fsr.module("zig-fermyon-router"));

    spin.generateSpinTOML(b, exe, .{
        .author = "<Your name>",
        .description = "<Your project description>"
        .version = "<Your version>"
    });
    b.installArtifact(exe);
}
  1. Run your app with Fermyon Spin:
cd zig-out
spin up

zig-fermyon-router's People

Contributors

ethanholz avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.