dendy

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2023 License: MIT Imports: 7 Imported by: 0

README

DENDY

(Which stands for Declarative ENdpoint Definition in Yaml.)

How to use

  1. Define API endpoints in YAML.

    hello:
      path: /
      type: GET
      handlerName: Hello
    auth:
      path: /auth
      type: POST
      handlerName: Auth
    
  2. Implement endpoints handler functions and put them in the map[string]http.HandlerFunc. Unfortunately, Go doesn't allow to call package functions by their names without binding them somehow — be it a map or a type method. That's the reason for the requirement.

    package handlers
    
    import "net/http"
    
    var Handlers = map[string]http.HandlerFunc{
        "Hello": Hello,
        "Auth":  Auth,
    }
    
    func Hello(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("Hello World!"))
    }
    
    func Auth(w http.ResponseWriter, r *http.Request) {
        w.WriteHeader(403)
        w.Write([]byte("You're not welcome here"))
    }
    
  3. Run server, passing previously created map[string]http.HandlerFunc as an argument.

    package main
    
    import (
        "github.com/alekseiadamov/dendy"
    
        "github.com/username/projectname/handlers"
    )
    
    func main() {
        dendy.Serve("localhost:3333", "./example.yaml", handlers.Handlers)
    }
    
  4. Check response.

    GET http://localhost:3333
    
    HTTP/1.1 200 OK
    Date: Thu, 14 Dec 2023 16:27:09 GMT
    Content-Length: 12
    Content-Type: text/plain; charset=utf-8
    Connection: close
    
    Hello World!
    
    POST http://localhost:3333/auth
    
    HTTP/1.1 403 Forbidden
    Date: Thu, 14 Dec 2023 16:28:59 GMT
    Content-Length: 23
    Content-Type: text/plain; charset=utf-8
    Connection: close
    
    You're not welcome here
    

Dependencies

See go.mod.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Serve

func Serve(address string, configPath string, handlers endpoints.Handlers)

Reads an endpoint configuration file, creates corresponding endpoints and runs a server.

Example:

package main

import (
	"github.com/alekseiadamov/dendy"

	"github.com/username/projectname/handlers"
)

func main() {
	dendy.Serve("localhost:3333", "./example.yaml", handlers.Handlers)
}

Types

type Router

type Router struct {
	*chi.Mux
}

func NewRouter

func NewRouter() Router

func (Router) CreateEndpoints

func (router Router) CreateEndpoints(endpoints endpoints.Endpoints)

func (Router) Serve

func (router Router) Serve(address string) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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