Initial commit

This commit is contained in:
2025-07-08 17:39:01 +00:00
commit 686cdb80b5
36 changed files with 3338 additions and 0 deletions

30
pkg/config/custom.go Normal file
View File

@ -0,0 +1,30 @@
// This serves as a reference sample configuration
// to show how to merge custom config fields into
// go-app configuration
package config
import (
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/config"
)
type DemoConfig struct {
Timezone string `yaml:"timezone" json:"timezone,omitempty" default:"UTC"`
Opts *DemoOpts `yaml:"opts" json:"opts,omitempty"`
// Embeds go-app config, used by go-app to
// merge custom config into go-app config, and to produce
// a complete configuration json schema
*config.AppConfig
}
type DemoOpts struct {
FactLang string `yaml:"factLang" json:"factLang,omitempty" default:"en"`
FactType FactType `yaml:"factType" json:"factType,omitempty" default:"random" enum:"today,random"`
}
type FactType string
const (
TypeToday FactType = "today"
TypeRandom FactType = "random"
)

86
pkg/demogrpc/demo.go Normal file
View File

@ -0,0 +1,86 @@
package demogrpc
import (
"context"
"fmt"
"github.com/go-resty/resty/v2"
"github.com/rs/zerolog"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
grpccodes "google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/otel"
pb "gitea.libretechconsulting.com/rmcguire/go-server-with-otel/api/demo/app/v1alpha1"
"gitea.libretechconsulting.com/rmcguire/go-server-with-otel/pkg/config"
)
const (
defaultFactType = config.TypeRandom
factsBaseURI = "https://uselessfacts.jsph.pl/api/v2/facts"
)
type DemoGRPCServer struct {
tracer trace.Tracer
ctx context.Context
cfg *config.DemoConfig
client *resty.Client
pb.UnimplementedDemoAppServiceServer
}
func NewDemoGRPCServer(ctx context.Context, cfg *config.DemoConfig) *DemoGRPCServer {
if cfg.Opts == nil {
cfg.Opts = &config.DemoOpts{}
}
if cfg.Opts.FactType == "" {
cfg.Opts.FactType = config.TypeRandom
}
return &DemoGRPCServer{
ctx: ctx,
cfg: cfg,
tracer: otel.GetTracer(ctx, "demoGRPCServer"),
client: resty.New().SetBaseURL(factsBaseURI),
}
}
func (d *DemoGRPCServer) GetDemo(ctx context.Context, req *pb.GetDemoRequest) (
*pb.GetDemoResponse, error,
) {
ctx, span := d.tracer.Start(ctx, "getDemo", trace.WithAttributes(
attribute.String("language", req.GetLanguage()),
))
defer span.End()
r := d.client.R().SetContext(ctx)
if req.GetLanguage() != "" {
r = r.SetQueryParam("language", req.GetLanguage())
}
fact := new(RandomFact)
r.SetResult(fact)
resp, err := r.Get(fmt.Sprintf("/%s", d.cfg.Opts.FactType))
if err != nil {
span.SetStatus(codes.Error, err.Error())
span.RecordError(err)
zerolog.Ctx(d.ctx).Err(err).Send()
return nil, status.Errorf(grpccodes.Unknown, "%s: %s",
err.Error(), string(resp.Body()))
}
span.SetAttributes(attribute.String("fact", fact.Text))
span.SetStatus(codes.Ok, "")
zerolog.Ctx(d.ctx).Debug().Str("fact", fact.Text).Msg("retrieved fact")
return fact.FactToProto(), nil
}

25
pkg/demogrpc/fact.go Normal file
View File

@ -0,0 +1,25 @@
package demogrpc
import (
"google.golang.org/protobuf/types/known/timestamppb"
pb "gitea.libretechconsulting.com/rmcguire/go-server-with-otel/api/demo/app/v1alpha1"
)
type RandomFact struct {
ID string `json:"id,omitempty"`
Text string `json:"text,omitempty"`
Source string `json:"source,omitempty"`
SourceURL string `json:"source_url,omitempty"`
Language string `json:"language,omitempty"`
Permalink string `json:"permalink,omitempty"`
}
func (f *RandomFact) FactToProto() *pb.GetDemoResponse {
return &pb.GetDemoResponse{
Timestamp: timestamppb.Now(),
Fact: f.Text,
Source: f.SourceURL,
Language: f.Language,
}
}

30
pkg/demogrpc/server.go Normal file
View File

@ -0,0 +1,30 @@
package demogrpc
import (
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/srv/grpc/opts"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
demoAppPb "gitea.libretechconsulting.com/rmcguire/go-server-with-otel/api/demo/app/v1alpha1"
)
func (ds *DemoGRPCServer) GetDialOpts() []grpc.DialOption {
return []grpc.DialOption{
// NOTE: Necessary for grpc-gateway to connect to grpc server
// Update if grpc service has credentials, tls, etc..
grpc.WithTransportCredentials(insecure.NewCredentials()),
}
}
func (ds *DemoGRPCServer) GetServices() []*opts.GRPCService {
return []*opts.GRPCService{
{
Name: "Demo App",
Type: &demoAppPb.DemoAppService_ServiceDesc,
Service: ds,
GwRegistrationFuncs: []opts.GwRegistrationFunc{
demoAppPb.RegisterDemoAppServiceHandler,
},
},
}
}

44
pkg/demohttp/server.go Normal file
View File

@ -0,0 +1,44 @@
package demohttp
import (
"context"
"net/http"
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/srv/http/opts"
"gitea.libretechconsulting.com/rmcguire/go-server-with-otel/pkg/config"
)
type DemoHTTPServer struct {
ctx context.Context
cfg *config.DemoConfig
}
func NewDemoHTTPServer(ctx context.Context, cfg *config.DemoConfig) *DemoHTTPServer {
return &DemoHTTPServer{
ctx: ctx,
cfg: cfg,
}
}
func (dh *DemoHTTPServer) GetHandleFuncs() []opts.HTTPFunc {
// TODO: Implement real http handle funcs
return []opts.HTTPFunc{
{
Path: "/test",
HandlerFunc: func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotImplemented)
w.Write([]byte("unimplemented demo app"))
},
},
}
}
func (dh *DemoHTTPServer) GetHealthCheckFuncs() []opts.HealthCheckFunc {
return []opts.HealthCheckFunc{
func(ctx context.Context) error {
// TODO: Implement real health checks
return nil
},
}
}