Go HTTP route management, with a structured API.

samb

Build Status GoDoc Go Report Card Maintainability Test Coverage

samb offers a structured language to build RESTful HTTP APIs. samb provides syntax support for languages similar to those used to write infrastructure as code. It offers:

  • A simple way to inject variables in request scope.
  • Nest API routes without long path prefixes.
  • Catch request runtime panics.
  • Generate Go code following guidelines.
  • A tested library to use samb as you wish.

Once you finish writing your code, you may then, deploy your project to your cloud provider of choice.

Editor Plugins :

Documentation

Learn more about samb code generation : here. Scroll down to find more samples.

Install

Requirements

  • samb requires Go +v1.8
  • dep (Dependency management)
  • $GOPATH environment variable set.
go get github.com/cheikhshift/samb/cmd/samb-cl

Starting a project

Create a new directory, then run the following command.

samb-cl -new -project=<NEW DIR PATH> <PACKAGE GO IMPORT PATH>

The command will add files in the new folder. Have a look at the files generated to give you an idea on how samb directives work. Some directives have comments further explaining their functionality.

Transpiling

Run the following command to convert your directives into Go code.

samb-cl -file=server.se -project=<NEW DIR PATH>

This will convert your directives into a Go library to handle your HTTP routes. A command will also be generated to launch your server, you can find the source code at <NEW DIR PATH>/cmd/server.

About parser

The following package is used to parse this Nginx like configuration language : github.com/recoye/config, Nginx configuration style parser with golang.

The following package is used to parse YAML : gopkg.in/yaml.v2

Additional packages

Parth : Path parsing for segment unmarshaling and slicing.


Tooling

Here is a list of tools that generate Go code for your SAMB projects.

samb-handler : Generate Go HTTP handlers with the specified parameters.

  • Install : go get github.com/cheikhshift/samb/cmd/samb-handler
  • Help : Run samb-handler -h

samb-provide: Generate and add a provider to your project. This will generate the Go source used with your provider as well.

  • Install : go get github.com/cheikhshift/samb/cmd/samb-provide
  • Help : Run samb-provide -h

samb-medic : Generate recover directive functions for your project.

  • Install : go get github.com/cheikhshift/samb/cmd/samb-medic
  • Help : Run samb-medic -h

Todo

  • Provide better documentation. Checkout the wiki.
  • Sublime/VSCode text plugins.
  • Write package tests.
  • Write tutorials/ guides.
  • A command line to help with adding new handler source.
  • Implement direct deployment to GCP App engine.

Samples

Find more sample projects here. Here is a sample server definition (in Nginx like language) :

server {
    host 127.0.0.1;
    port 8080;

    # Import web route definitions
    require "./endpoints.se";


    start {
    	do println("Hello");
    	do println("Hello again");
    }

    shutdown {
	# directive do will execute passed
	# golang code
    	do println("Bye");
    }  
}

In YAML:

# Go package import path of your project.
package: github.com/cheikhshift/samb-examples/yaml-example

# import providers
require: ["./providers.se"]


# Globals are exported via package 
# globals
global:
  - name: Foo
    type : bool
    # Adding comments to exported variable.
    comment: Foo decides if a process should run
    return : false
  - name: AnotherVariable
    type : bool
    return: true

server: 
  host: 127.0.0.1
  port: 8081
  # Import web route definitions
  require : ["./endpoints.yml"]

  start:
    do:
      - println("HelloWorld")
      - println("Starting...")
Sample Route (file named endpoints.se)
# Routes' definition
# Import Go packages with directive import
# For example import "net/http";
# samb source format checking.


routes {
    provide r;

    route {
	    method *;
	    # Defines route path.
	    # all sub routes have this path
	    # prepended.
	    path "/hello/";

	    # Provider variables
	    # within scope of entire 
	    # route.
	    provide w;
	    provide r;


	    go {
	    	do println("Hello");
	    	# The following commented
	    	# do directive will stop a
	    	# request :
	    	# do return;
	    }

	    route {
	    	method GET;
	    	path "Foo";

	    	go {
	    		do println("Hello");
	    	}

	    	# Handler can be any function.
	    	# Should be a function that handles the request
	    	# response. using provided variable r
		# with handler. This code will fail,
		# the referenced handler is not defined
	    	handler virtualPackage.Handle(r);
	    }


	}
}
Sample Providers
# Providers are used
# within endpoint requests.
provider {
	name r;
	type *http.Request;
    # directive return is not used here.
    # return can be used to define how your
    # provider is initialized. For example,
    # providing a variable with value "Foo"
    # : return string("Foo") 
}

provider {
	name w;
	type *http.ResponseWriter;
}
Similar Resources

CDN for Open Source, Non-commercial CDN management

CDN for Open Source, Non-commercial CDN management

CDN Control Official Website: https://cluckcdn.buzz Documentation (Traditional Chinese): https://cluckcdn.buzz/docs/ 简体中文 README: README_CN.md Please

Dec 20, 2021

COGS: COnfiguration manaGement S.

COGS: COnfiguration manaGement S cogs is a cli tool that allows generation of configuration files through different references sources. Sources of ref

Dec 17, 2021

A client management system with go

A client management system with go

Client-management-system 一个简单客户端管理系统,类似电子教室管理软件,教师端和学生端均使用Golang编写,用Redis数据库储存信息。 功能 学生端 使用账号密码登入登出 在聊天室中畅所欲言 与其他在线学生或老师发送私聊消息 向老师发出提问 提交老师布置的作业并查询自己的

Oct 7, 2021

OCM events management service

Events Service This project provides a service that gives access to events generated by the OCM services. Building Locally Cloning There is nothing sp

Oct 15, 2021

gRPC User Management Service

Installation Clone the repo git clone https://github.com/geekbim/Go-gRPC.git Generate Protobuf sh run-proto.sh Running PostgreSQL Container sh postgre

Oct 11, 2021

LNC is a lightning network capital management tool built for routing nodes.

LNC is a lightning network capital management tool built for routing nodes.

Dec 21, 2021

DNS/DoT to DoH proxy with load-balancing, fail-over and SSL certificate management

dns-proxy Configuration Variable Example Description TLS_DOMAIN my.duckdns.org Domain name without wildcards. Used to create wildcard certificate and

Oct 26, 2022

Service Management App for Caddy v2

caddy-systemd Service Management App for Caddy v2. Please ask questions either here or via LinkedIn. I am happy to help you! @greenpau Please see othe

Sep 1, 2022

Msignal - Basic Utilities for OS Signal Management

msignal 📶 - Basic Utilities for OS Signal Management Usage func main() { sysca

Mar 6, 2022
Fast HTTP package for Go. Tuned for high performance. Zero memory allocations in hot paths. Up to 10x faster than net/http
Fast HTTP package for Go. Tuned for high performance. Zero memory allocations in hot paths. Up to 10x faster than net/http

fasthttp Fast HTTP implementation for Go. Currently fasthttp is successfully used by VertaMedia in a production serving up to 200K rps from more than

Jan 5, 2023
Go HTTP tunnel is a reverse tunnel based on HTTP/2.

Go HTTP tunnel is a reverse tunnel based on HTTP/2. It enables you to share your localhost when you don't have a public IP.

Dec 28, 2022
Simple GUI to convert Charles headers to golang's default http client (net/http)

Charles-to-Go Simple GUI to convert Charles headers to golang's default http client (net/http) Usage Compile code to a binary, go build -ldflags -H=wi

Dec 14, 2021
Go-http-sleep: Delayed response http server, useful for testing various timeout issue for application running behind proxy

delayed response http server, useful for testing various timeout issue for application running behind proxy

Jan 22, 2022
Http-logging-proxy - A HTTP Logging Proxy For Golang

http-logging-proxy HTTP Logging Proxy Description This project builds a simple r

Aug 1, 2022
Http-recorder - Application for record http response as static files
Http-recorder - Application for record http response as static files

http-recorder This is a application for record http response as static files. Th

Mar 21, 2022
Http-server - A HTTP server and can be accessed via TLS and non-TLS mode

Application server.go runs a HTTP/HTTPS server on the port 9090. It gives you 4

Feb 3, 2022
gNXI Tools - gRPC Network Management/Operations Interface Tools

gNxI Tools gNMI - gRPC Network Management Interface gNOI - gRPC Network Operations Interface A collection of tools for Network Management that use the

Dec 15, 2022
Simple hosts file management in Golang (deprecated).
Simple hosts file management in Golang (deprecated).

Goodhosts (deprecated) This library is now deprecated. See the goodhosts organisation for the current maintained version. Simple hosts file (/etc/host

Mar 17, 2022