You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 7-8.RSVP
Socket
Socket
Sign inDemoInstall

github.com/enorith/http

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/enorith/http


Version published
Created

Readme

Source

Http component for Enorith

Usage

Basic example

package main

import (
	"github.com/enorith/container"
	"github.com/enorith/http"
	"github.com/enorith/http/content"
	"github.com/enorith/http/contracts"
	"github.com/enorith/http/router"
	"log"
	"fmt"
)

type FooRequest struct {
	content.Request
    // input injection and validation
	Foo string `input:"foo" validate:"required"`
	File contracts.UploadFile `file:"file"`
}

func main() {
	s := http.NewServer(func() *container.Container {
		return container.New() // runtime IoC container
	}, true)
	
	e := s.Serve(":10800", func(ro *router.Wrapper, k *http.Kernel) {
		//k.OutputLog = true
		ro.HandleGet("/", func(r contracts.RequestContract) contracts.ResponseContract {

			return content.TextResponse("ok", 200)
		})

		// request injection
		ro.Get("/foo", func(r FooRequest) contracts.ResponseContract {

			return content.TextResponse("input foo: " + r.Foo, 200)
		})

		// param injection
		ro.Get("/:id", func(id content.ParamUint64) string {

			return fmt.Sprintf("input id: %d", id)
		})
	})

	if e != nil {
		log.Fatalf("serve error: %v", e)
	}
}

Middleware

package foo

import (
	"github.com/enorith/http"
	"github.com/enorith/http/contracts"
	"github.com/enorith/http/router"
)

type FooMiddleware struct {

}

func (FooMiddleware) Handle(r contracts.RequestContract, next http.PipeHandler) contracts.ResponseContract {
	//TODO: before request handled

	resp := next(r)

	//TOD: after request handled
	return resp
}


func Handler(ro *router.Wrapper, k *http.Kernel) {
	k.SetMiddleware([]http.RequestMiddleware{ // global middleware
		FooMiddleware{},
	})

	k.SetMiddlewareGroup(map[string][]http.RequestMiddleware{
		"foo.mid": {FooMiddleware{}}
	})

	ro.Get("foo", fun() string { return "bar"}).Middleware("foo.mid")
}

Working with Container

package main

import (
	"reflect"

	"github.com/enorith/container"
	"github.com/enorith/http"
	"github.com/enorith/http/contracts"
	"github.com/enorith/http/router"
)

type FooStruct struct {
	Bar string
}

type FooService struct {
	Foo FooStruct // construct injection
}


func (fs FooService) GetBar() string {
	return fs.Foo.Bar
}

func main() {
	srv := http.NewServer(func(request contracts.RequestContract) container.Interface {
		con := container.New()

		con.BindFunc(FooStruct{}, func(c container.Interface) (reflect.Value, error) { // bind instance
			return reflect.ValueOf(FooStruct{Bar: "baz"}), nil
		}, false)
		return con
	}, true)

	srv.Serve(":8000", func(rw *router.Wrapper, k *http.Kernel) {


		rw.Get("foo", func(fs FooService) string { // parameter injection
			return fs.GetBar()
		})
	})
}

TODO

  • Get client ip behand proxy
  • Validation (incomplete)
  • Logging

FAQs

Package last updated on 12 Feb 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc