mirror of
https://github.com/OliveTin/OliveTin
synced 2025-12-12 09:05:39 +00:00
Single endpoint for web+ui (Issue #4). Migrated to modern protobuf+grpc.
This commit is contained in:
54
internal/httpservers/restapi.go
Normal file
54
internal/httpservers/restapi.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package httpservers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/protobuf/encoding/protojson"
|
||||
"net/http"
|
||||
|
||||
gw "github.com/jamesread/OliveTin/gen/grpc"
|
||||
|
||||
cors "github.com/jamesread/OliveTin/internal/cors"
|
||||
|
||||
config "github.com/jamesread/OliveTin/internal/config"
|
||||
)
|
||||
|
||||
var (
|
||||
cfg *config.Config
|
||||
)
|
||||
|
||||
func startRestAPIServer(globalConfig *config.Config) error {
|
||||
cfg = globalConfig
|
||||
|
||||
log.WithFields(log.Fields{
|
||||
"address": cfg.ListenAddressGrpcActions,
|
||||
}).Info("Starting REST API")
|
||||
|
||||
ctx := context.Background()
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
|
||||
// The JSONPb.EmitDefaults is necssary, so "empty" fields are returned in JSON.
|
||||
//mux := runtime.NewServeMux(runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.JSONPb{OrigName: true, EmitDefaults: true}))
|
||||
mux := runtime.NewServeMux(
|
||||
runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.HTTPBodyMarshaler{
|
||||
Marshaler: &runtime.JSONPb{
|
||||
MarshalOptions: protojson.MarshalOptions{
|
||||
UseProtoNames: true,
|
||||
EmitUnpopulated: true,
|
||||
},
|
||||
},
|
||||
}),
|
||||
)
|
||||
opts := []grpc.DialOption{grpc.WithInsecure()}
|
||||
|
||||
err := gw.RegisterOliveTinApiHandlerFromEndpoint(ctx, mux, cfg.ListenAddressGrpcActions, opts)
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("gw error %v", err)
|
||||
}
|
||||
|
||||
return http.ListenAndServe(cfg.ListenAddressRestActions, cors.AllowCors(mux))
|
||||
}
|
||||
Reference in New Issue
Block a user