Git Product home page Git Product logo

goroot's Introduction

GoRoot

GoRoot (I AM GOROOT) is a simple and mini web framework for Golang.

How to use

1- define your handler ( business logic)

package main
   
import (
    "github.com/hashemzargari/goroot"
)

type SumNumbersRequest struct {
	Numbers []int `json:"numbers"`
}

type SumNumbersResponse struct {
	Result int `json:"result"`
}

type SumHandler struct{}

func (h SumHandler) Handle(ctx goroot.Context, request any) (any, error) {
	req := request.(SumNumbersRequest)

	result := 0
	for _, number := range req.Numbers {
		result += number
	}

	return SumNumbersResponse{Result: result}, nil
}

func (h SumHandler) GetRequestType() any {
	return SumNumbersRequest{}
}

func (h SumHandler) GetResponseType() any {
	return SumNumbersResponse{}
}

func (h SumHandler) GetPermissionClaims() []goroot.PermissionClaim {
	return []goroot.PermissionClaim{}
}

func (h SumHandler) GetAuthenticationClaims() goroot.AuthenticationClaim {
	return goroot.AllowAuthenticated | goroot.AllowAdmin
}

func (h SumHandler) GetApiRoute() string {
	return "/sum-numbers"
}

func (h SumHandler) GetMethod() goroot.ApiHttpMethod {
	return goroot.Post
}

2- create app, register your handler and Run

func main() {
    app := goroot.NewApp("MyService", uint(1))
    _ = app.RegisterHandlers(SumHandler{})
    err := app.Run(":9002")
    if err != nil {
    panic(err)
    }
}

GoRoot generate a proto file based on your handlers and use google.api.http annotation for routing.

// Code generated by GoRoot (github.com/hashemzargari/goroot). DO NOT EDIT.

syntax = "proto3";

package goroot.MyService.v1;


import "google/api/annotations.proto";
import "google/protobuf/timestamp.proto";
import "protoc-gen-openapiv2/options/annotations.proto";

option go_package = "goroot/MyService/v1;v1";
option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_swagger) = {
  info: {
    title: "MyService";
    version: "1.0";
  };
};



message SumNumbersRequest {

  repeated int32 Numbers = 1;

}

message SumNumbersResponse {

  int32 Result = 1;

}


service MyService {

  rpc SumHandler (SumNumbersRequest) returns (SumNumbersResponse) {
    option (google.api.http) = {
      post: "/sum-numbers";
      body: "*";
    };
  }

}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.