GoRoot (I AM GOROOT) is a simple and mini web framework for Golang.
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
}
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: "*";
};
}
}