clay

package module
v0.0.0-...-1a9f1ff Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 18, 2024 License: MIT Imports: 2 Imported by: 0

README

clay

[Experimental] lightweight framework for writing cloudflare workers with go

//go:build js && wasm
// +build js,wasm

package main

import (
 "github.com/uchexgod/clay"
)

func main() {

  app := clay.NewApp()

 //send html response
  app.Get("/", func(ctx *clay.Context, next clay.NextFunc) interface{} {

  htmlContent := `
    <!DOCTYPE html>
    <html>
    <head>
    <title>To-Do List</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; }
        #todoInput { padding: 10px; width: 200px; margin-right: 10px; }
        #addButton { padding: 10px; }
        #todoList { margin-top: 20px; }
    </style>
    </head>
    <body>
        <input type="text" id="todoInput" placeholder="Add a new task">
        <button id="addButton">Add Task</button>
        <ul id="todoList"></ul>
        
        <script>
            document.getElementById("addButton").onclick = function() {
                var input = document.getElementById("todoInput");
                var newTodo = input.value;
                if (newTodo) {
                    var li = document.createElement("li");
                    li.textContent = newTodo;
                    document.getElementById("todoList").appendChild(li);
                    input.value = ""; // Clear the input
                }
            };
        </script>
    </body>
    </html>
    `
  return ctx.HTML(htmlContent)
 })

 //send text response
 app.Post("/todo", func(ctx *clay.Context, next clay.NextFunc) interface{} {
  return ctx.Text("Todo Created", 201)
 })


 app.Listen()

}

Documentation

Rendered for js/wasm

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewApp

func NewApp() *app

Types

type Context

type Context struct {
	Request *Request
}

func (*Context) HTML

func (res *Context) HTML(htmlString string) interface{}

func (*Context) JSON

func (ctx *Context) JSON(v map[string]string, status int) interface{}

func (*Context) Redirect

func (ctx *Context) Redirect(destination string, status ...int) interface{}

func (*Context) Text

func (ctx *Context) Text(content string, status ...int) interface{}

type Handler

type Handler func(ctx *Context, next NextFunc) interface{}

type NextFunc

type NextFunc func(ctx *Context)

type RegexpRouter

type RegexpRouter struct {
	// contains filtered or unexported fields
}

func NewRegexpRouter

func NewRegexpRouter() *RegexpRouter

func (*RegexpRouter) Register

func (r *RegexpRouter) Register(method, pattern string, handler Handler)

func (*RegexpRouter) ServeHTTP

func (r *RegexpRouter) ServeHTTP(this js.Value, args []js.Value) interface{}

type Request

type Request struct {
	// contains filtered or unexported fields
}

func (*Request) Param

func (r *Request) Param(name string) string

Param retrieves path parameters by name.

type Response

type Response interface {
	func() interface{}
}

type Route

type Route struct {
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL