github.com/lastbackend/toolkit@v0.0.0-20241020043710-cafa37b95aad/examples/http/gen/server/server_service.pb.toolkit.go (about)

     1  // Code generated by protoc-gen-toolkit. DO NOT EDIT.
     2  // source: github.com/lastbackend/toolkit/examples/http/apis/server.proto
     3  
     4  package serverpb
     5  
     6  import (
     7  	"context"
     8  	"encoding/json"
     9  	"io"
    10  	"net/http"
    11  
    12  	toolkit "github.com/lastbackend/toolkit"
    13  	client "github.com/lastbackend/toolkit/pkg/client"
    14  	runtime "github.com/lastbackend/toolkit/pkg/runtime"
    15  	controller "github.com/lastbackend/toolkit/pkg/runtime/controller"
    16  	tk_http "github.com/lastbackend/toolkit/pkg/server/http"
    17  	errors "github.com/lastbackend/toolkit/pkg/server/http/errors"
    18  	tk_ws "github.com/lastbackend/toolkit/pkg/server/http/websockets"
    19  	emptypb "google.golang.org/protobuf/types/known/emptypb"
    20  )
    21  
    22  // This is a compile-time assertion to ensure that this generated file
    23  // is compatible with the toolkit package it is being compiled against and
    24  // suppress "imported and not used" errors
    25  var (
    26  	_ context.Context
    27  	_ emptypb.Empty
    28  	_ http.Handler
    29  	_ errors.Err
    30  	_ io.Reader
    31  	_ json.Marshaler
    32  	_ tk_ws.Client
    33  	_ tk_http.Handler
    34  	_ client.GRPCClient
    35  )
    36  
    37  // Definitions
    38  
    39  // Service Http define
    40  type serviceHttp struct {
    41  	runtime runtime.Runtime
    42  }
    43  
    44  func NewHttpService(name string, opts ...runtime.Option) (_ toolkit.Service, err error) {
    45  	app := new(serviceHttp)
    46  
    47  	app.runtime, err = controller.NewRuntime(context.Background(), name, opts...)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  
    52  	// loop over plugins and initialize plugin instance
    53  
    54  	// loop over plugins and register plugin in toolkit
    55  
    56  	// create new Http HTTP server
    57  	app.runtime.Server().HTTPNew(name, nil)
    58  
    59  	app.runtime.Server().HTTP().AddHandler(http.MethodGet, "/hello", app.handlerHTTPHttpHelloWorld)
    60  
    61  	return app.runtime.Service(), nil
    62  }
    63  
    64  // Define services for Http HTTP server
    65  
    66  type HttpHTTPService interface {
    67  	HelloWorld(ctx context.Context, req *HelloRequest) (*HelloResponse, error)
    68  }
    69  
    70  // Define HTTP handlers for Router HTTP server
    71  
    72  func (s *serviceHttp) handlerHTTPHttpHelloWorld(w http.ResponseWriter, r *http.Request) {
    73  	ctx, cancel := context.WithCancel(r.Context())
    74  	defer cancel()
    75  
    76  	var protoRequest HelloRequest
    77  	var protoResponse *HelloResponse
    78  
    79  	_, om := tk_http.GetMarshaler(s.runtime.Server().HTTP(), r)
    80  
    81  	if err := r.ParseForm(); err != nil {
    82  		errors.HTTP.InternalServerError(w)
    83  		return
    84  	}
    85  
    86  	if err := tk_http.ParseRequestQueryParametersToProto(&protoRequest, r.Form); err != nil {
    87  		errors.HTTP.InternalServerError(w)
    88  		return
    89  	}
    90  
    91  	headers, err := tk_http.PrepareHeaderFromRequest(r)
    92  	if err != nil {
    93  		errors.HTTP.InternalServerError(w)
    94  		return
    95  	}
    96  
    97  	ctx = tk_http.NewIncomingContext(ctx, headers)
    98  
    99  	protoResponse, err = s.runtime.Server().HTTP().GetService().(HttpHTTPService).HelloWorld(ctx, &protoRequest)
   100  	if err != nil {
   101  		errors.GrpcErrorHandlerFunc(w, err)
   102  		return
   103  	}
   104  
   105  	buf, err := om.Marshal(protoResponse)
   106  	if err != nil {
   107  		errors.HTTP.InternalServerError(w)
   108  		return
   109  	}
   110  
   111  	w.Header().Set("Content-Type", om.ContentType())
   112  	if proceed, err := tk_http.HandleGRPCResponse(w, r, headers); err != nil || !proceed {
   113  		return
   114  	}
   115  
   116  	if _, err = w.Write(buf); err != nil {
   117  		return
   118  	}
   119  }