hub

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2023 License: GPL-2.0 Imports: 4 Imported by: 1

README

Hub

Hello, my name is Mykhailo, one evening I decided to make a home ecosystem. And for that I needed to create a network between all the devices in my house. Therefore, I set out to make a small framework that will help me do this easily. Since I really like p2p technologies, I want to build a p2p network.

Documentation

I've created a custom handler system that runs on top of libp2p:Stream. I used protobuf for data transport.

Request
Parameter Type Description
User string Required. Sender ID
Payload string Required. Data (I think i will use json)
Handler string Required. Handler name
Response
Parameter Type Description
Payload string Required. Id of item to fetch
Status int64 Required. Status code "Like http :з"
Example

Server

package main

import (
	hub "github.com/Mihalic2040/Hub"
	"github.com/Mihalic2040/Hub/src/proto/api"
	"github.com/Mihalic2040/Hub/src/server"
	"github.com/Mihalic2040/Hub/src/types"
	"github.com/Mihalic2040/Hub/src/utils"
)

func MyHandler(input *api.Request) (response api.Response, err error) {
	// Do some processing with the input data
	// ...

	// Return the output data and no error
	return server.Response(input.Payload, 200), nil
}

func main() {
	// ctx := context.Background()
	// //fake config
	config := types.Config{
		Host: "0.0.0.0",
		Port: "0",
		Secret:     "SERVER",
		Rendezvous: "Hub",
		DHTServer:  true,
		ProtocolId: "/hub/0.0.1",
		Bootstrap:  "/ip4/0.0.0.0/tcp/6666/p2p/12D3KooWQd1K1k8XA9xVEzSAu7HUCodC7LJB6uW5Kw4VwkRdstPE",
	}

	app := &hub.App{}

	app.Settings(config)

	app.Handler(utils.GetFunctionName(MyHandler), MyHandler)

	app.Start(true)


}


Request

package main

import (
	"fmt"
	"net/http"

	hub "github.com/Mihalic2040/Hub"
	"github.com/Mihalic2040/Hub/src/proto/api"
	"github.com/Mihalic2040/Hub/src/server"
	"github.com/Mihalic2040/Hub/src/types"
	"github.com/Mihalic2040/Hub/src/utils"
)


func handleRequest(w http.ResponseWriter, r *http.Request) {

	fmt.Fprintln(w, app.Host.Peerstore().Peers())
}

var app hub.App

func main() {
	// ctx := context.Background()
	// //fake config
	config := types.Config{
		Host: "0.0.0.0",
		Port: "0",
		//Secret:     "SERVER",
		Rendezvous: "Hub",
		DHTServer:  true,
		ProtocolId: "/hub/0.0.1",
		Bootstrap:  "/ip4/0.0.0.0/tcp/6666/p2p/12D3KooWQd1K1k8XA9xVEzSAu7HUCodC7LJB6uW5Kw4VwkRdstPE",
	}

	//app := &hub.App{}

	app.Settings(config)


	app.Start(false)

	http.HandleFunc("/", handleRequest)
	http.ListenAndServe(":8080", nil)

}

ToDo

  • Dynamic serialization (For support json and other cool type)
  • Encryption
  • Relays
  • Cool logo

Feedback

Discord: Mihalic2040#6533

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type App

type App struct {
	types.App
}

func (*App) Handler

func (app *App) Handler(name string, handlerFunc server.Handler)

AddHandler adds a handler to the Handlers map in the Host struct

func (*App) Settings

func (app *App) Settings(conf types.Config)

Conf sets the configuration in the App struct

func (*App) Start

func (app *App) Start(serve bool) *types.App

Directories

Path Synopsis
examples
src

Jump to

Keyboard shortcuts

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