Skip to content
Snippets Groups Projects
Commit 114a3c0b authored by Lyuben Penkovski's avatar Lyuben Penkovski
Browse files

Initial skeleton and main function

parent c7afb7ab
No related branches found
No related tags found
1 merge request!1Initial service skeleton
Pipeline #51010 passed with stage
in 37 seconds
......@@ -6,7 +6,7 @@ before_script:
- cd /go/src/code.vereign.com/${CI_PROJECT_PATH}
lint:
image: golangci/golangci-lint:v1.44.2
image: golangci/golangci-lint:v1.46.2
stage: test
tags:
- amd64-docker
......@@ -15,7 +15,7 @@ lint:
- golangci-lint run
unit tests:
image: golang:1.17.8
image: golang:1.17.10
stage: test
tags:
- amd64-docker
......
package main
import (
"context"
"errors"
"log"
"net/http"
"time"
"github.com/kelseyhightower/envconfig"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
goahttp "goa.design/goa/v3/http"
goa "goa.design/goa/v3/pkg"
"golang.org/x/sync/errgroup"
"code.vereign.com/gaiax/tsa/golib/graceful"
goahealth "code.vereign.com/gaiax/tsa/infohub/gen/health"
goahealthsrv "code.vereign.com/gaiax/tsa/infohub/gen/http/health/server"
goainfohubsrv "code.vereign.com/gaiax/tsa/infohub/gen/http/infohub/server"
goaopenapisrv "code.vereign.com/gaiax/tsa/infohub/gen/http/openapi/server"
goainfohub "code.vereign.com/gaiax/tsa/infohub/gen/infohub"
"code.vereign.com/gaiax/tsa/infohub/gen/openapi"
"code.vereign.com/gaiax/tsa/infohub/internal/config"
"code.vereign.com/gaiax/tsa/infohub/internal/service"
"code.vereign.com/gaiax/tsa/infohub/internal/service/health"
"code.vereign.com/gaiax/tsa/infohub/internal/service/infohub"
)
var Version = "0.0.0+development"
func main() {
// load configuration from environment
var cfg config.Config
if err := envconfig.Process("", &cfg); err != nil {
log.Fatalf("cannot load configuration: %v", err)
}
// create logger
logger, err := createLogger(cfg.LogLevel)
if err != nil {
log.Fatalln(err)
}
defer logger.Sync() //nolint:errcheck
logger.Info("infohub service started", zap.String("version", Version), zap.String("goa", goa.Version()))
// create services
var (
infohubSvc goainfohub.Service
healthSvc goahealth.Service
)
{
infohubSvc = infohub.New(logger)
healthSvc = health.New()
}
// create endpoints
var (
infohubEndpoints *goainfohub.Endpoints
healthEndpoints *goahealth.Endpoints
openapiEndpoints *openapi.Endpoints
)
{
infohubEndpoints = goainfohub.NewEndpoints(infohubSvc)
healthEndpoints = goahealth.NewEndpoints(healthSvc)
openapiEndpoints = openapi.NewEndpoints(nil)
}
// Provide the transport specific request decoder and response encoder.
// The goa http package has built-in support for JSON, XML and gob.
// Other encodings can be used by providing the corresponding functions,
// see goa.design/implement/encoding.
var (
dec = goahttp.RequestDecoder
enc = goahttp.ResponseEncoder
)
// Build the service HTTP request multiplexer and configure it to serve
// HTTP requests to the service endpoints.
mux := goahttp.NewMuxer()
// Wrap the endpoints with the transport specific layers. The generated
// server packages contains code generated from the design which maps
// the service input and output data structures to HTTP requests and
// responses.
var (
infohubServer *goainfohubsrv.Server
healthServer *goahealthsrv.Server
openapiServer *goaopenapisrv.Server
)
{
infohubServer = goainfohubsrv.New(infohubEndpoints, mux, dec, enc, nil, errFormatter)
healthServer = goahealthsrv.New(healthEndpoints, mux, dec, enc, nil, errFormatter)
openapiServer = goaopenapisrv.New(openapiEndpoints, mux, dec, enc, nil, errFormatter, nil, nil)
}
// Configure the mux.
goainfohubsrv.Mount(mux, infohubServer)
goahealthsrv.Mount(mux, healthServer)
goaopenapisrv.Mount(mux, openapiServer)
var handler http.Handler = mux
srv := &http.Server{
Addr: cfg.HTTP.Host + ":" + cfg.HTTP.Port,
Handler: handler,
IdleTimeout: cfg.HTTP.IdleTimeout,
ReadTimeout: cfg.HTTP.ReadTimeout,
WriteTimeout: cfg.HTTP.WriteTimeout,
}
g, ctx := errgroup.WithContext(context.Background())
g.Go(func() error {
if err := graceful.Shutdown(ctx, srv, 20*time.Second); err != nil {
logger.Error("server shutdown error", zap.Error(err))
return err
}
return errors.New("server stopped successfully")
})
if err := g.Wait(); err != nil {
logger.Error("run group stopped", zap.Error(err))
}
logger.Info("bye bye")
}
func createLogger(logLevel string, opts ...zap.Option) (*zap.Logger, error) {
var level = zapcore.InfoLevel
if logLevel != "" {
err := level.UnmarshalText([]byte(logLevel))
if err != nil {
return nil, err
}
}
config := zap.NewProductionConfig()
config.Level = zap.NewAtomicLevelAt(level)
config.DisableStacktrace = true
config.EncoderConfig.TimeKey = "ts"
config.EncoderConfig.EncodeTime = zapcore.RFC3339TimeEncoder
return config.Build(opts...)
}
func errFormatter(e error) goahttp.Statuser {
return service.NewErrorResponse(e)
}
//func httpClient() *http.Client {
// return &http.Client{
// Transport: &http.Transport{
// Proxy: http.ProxyFromEnvironment,
// DialContext: (&net.Dialer{
// Timeout: 30 * time.Second,
// }).DialContext,
// MaxIdleConns: 100,
// MaxIdleConnsPerHost: 100,
// TLSHandshakeTimeout: 10 * time.Second,
// IdleConnTimeout: 60 * time.Second,
// },
// Timeout: 30 * time.Second,
// }
//}
......@@ -19,7 +19,7 @@ var _ = Service("infohub", func() {
Description("Information Hub Service enables exporting and importing information.")
Method("Export", func() {
Description("Evaluate executes a policy with the given 'data' as input.")
Description("Export returns data signed as Verifiable Presentation.")
Payload(ExportRequest)
Result(Any)
HTTP(func() {
......
......@@ -202,7 +202,7 @@ Usage:
%[1]s [globalflags] infohub COMMAND [flags]
COMMAND:
export: Evaluate executes a policy with the given 'data' as input.
export: Export returns data signed as Verifiable Presentation.
Additional help:
%[1]s infohub COMMAND --help
......@@ -211,7 +211,7 @@ Additional help:
func infohubExportUsage() {
fmt.Fprintf(os.Stderr, `%[1]s [flags] infohub export -export-name STRING
Evaluate executes a policy with the given 'data' as input.
Export returns data signed as Verifiable Presentation.
-export-name STRING: Name of export to be performed.
Example:
......
{"swagger":"2.0","info":{"title":"Information Hub Service","description":"Information Hub Service exposes HTTP API for exporting and importing information.","version":""},"host":"localhost:8084","consumes":["application/json","application/xml","application/gob"],"produces":["application/json","application/xml","application/gob"],"paths":{"/liveness":{"get":{"tags":["health"],"summary":"Liveness health","operationId":"health#Liveness","responses":{"200":{"description":"OK response."}},"schemes":["http"]}},"/readiness":{"get":{"tags":["health"],"summary":"Readiness health","operationId":"health#Readiness","responses":{"200":{"description":"OK response."}},"schemes":["http"]}},"/v1/export/{exportName}":{"get":{"tags":["infohub"],"summary":"Export infohub","description":"Evaluate executes a policy with the given 'data' as input.","operationId":"infohub#Export","parameters":[{"name":"exportName","in":"path","description":"Name of export to be performed.","required":true,"type":"string"}],"responses":{"200":{"description":"OK response.","schema":{"type":"string","format":"binary"}}},"schemes":["http"]}}}}
\ No newline at end of file
{"swagger":"2.0","info":{"title":"Information Hub Service","description":"Information Hub Service exposes HTTP API for exporting and importing information.","version":""},"host":"localhost:8084","consumes":["application/json","application/xml","application/gob"],"produces":["application/json","application/xml","application/gob"],"paths":{"/liveness":{"get":{"tags":["health"],"summary":"Liveness health","operationId":"health#Liveness","responses":{"200":{"description":"OK response."}},"schemes":["http"]}},"/readiness":{"get":{"tags":["health"],"summary":"Readiness health","operationId":"health#Readiness","responses":{"200":{"description":"OK response."}},"schemes":["http"]}},"/v1/export/{exportName}":{"get":{"tags":["infohub"],"summary":"Export infohub","description":"Export returns data signed as Verifiable Presentation.","operationId":"infohub#Export","parameters":[{"name":"exportName","in":"path","description":"Name of export to be performed.","required":true,"type":"string"}],"responses":{"200":{"description":"OK response.","schema":{"type":"string","format":"binary"}}},"schemes":["http"]}}}}
\ No newline at end of file
......@@ -41,7 +41,7 @@ paths:
tags:
- infohub
summary: Export infohub
description: Evaluate executes a policy with the given 'data' as input.
description: Export returns data signed as Verifiable Presentation.
operationId: infohub#Export
parameters:
- name: exportName
......
{"openapi":"3.0.3","info":{"title":"Information Hub Service","description":"Information Hub Service exposes HTTP API for exporting and importing information.","version":"1.0"},"servers":[{"url":"http://localhost:8084","description":"Information Hub Server"}],"paths":{"/liveness":{"get":{"tags":["health"],"summary":"Liveness health","operationId":"health#Liveness","responses":{"200":{"description":"OK response."}}}},"/readiness":{"get":{"tags":["health"],"summary":"Readiness health","operationId":"health#Readiness","responses":{"200":{"description":"OK response."}}}},"/v1/export/{exportName}":{"get":{"tags":["infohub"],"summary":"Export infohub","description":"Evaluate executes a policy with the given 'data' as input.","operationId":"infohub#Export","parameters":[{"name":"exportName","in":"path","description":"Name of export to be performed.","required":true,"schema":{"type":"string","description":"Name of export to be performed.","example":"myexport"},"example":"myexport"}],"responses":{"200":{"description":"OK response.","content":{"application/json":{"schema":{"type":"string","example":"Veniam non.","format":"binary"},"example":"Ipsam similique nulla quis."}}}}}}},"components":{},"tags":[{"name":"health","description":"Health service provides health check endpoints."},{"name":"infohub","description":"Information Hub Service enables exporting and importing information."}]}
\ No newline at end of file
{"openapi":"3.0.3","info":{"title":"Information Hub Service","description":"Information Hub Service exposes HTTP API for exporting and importing information.","version":"1.0"},"servers":[{"url":"http://localhost:8084","description":"Information Hub Server"}],"paths":{"/liveness":{"get":{"tags":["health"],"summary":"Liveness health","operationId":"health#Liveness","responses":{"200":{"description":"OK response."}}}},"/readiness":{"get":{"tags":["health"],"summary":"Readiness health","operationId":"health#Readiness","responses":{"200":{"description":"OK response."}}}},"/v1/export/{exportName}":{"get":{"tags":["infohub"],"summary":"Export infohub","description":"Export returns data signed as Verifiable Presentation.","operationId":"infohub#Export","parameters":[{"name":"exportName","in":"path","description":"Name of export to be performed.","required":true,"schema":{"type":"string","description":"Name of export to be performed.","example":"myexport"},"example":"myexport"}],"responses":{"200":{"description":"OK response.","content":{"application/json":{"schema":{"type":"string","example":"Veniam non.","format":"binary"},"example":"Ipsam similique nulla quis."}}}}}}},"components":{},"tags":[{"name":"health","description":"Health service provides health check endpoints."},{"name":"infohub","description":"Information Hub Service enables exporting and importing information."}]}
\ No newline at end of file
......@@ -31,7 +31,7 @@ paths:
tags:
- infohub
summary: Export infohub
description: Evaluate executes a policy with the given 'data' as input.
description: Export returns data signed as Verifiable Presentation.
operationId: infohub#Export
parameters:
- name: exportName
......
......@@ -13,7 +13,7 @@ import (
// Information Hub Service enables exporting and importing information.
type Service interface {
// Evaluate executes a policy with the given 'data' as input.
// Export returns data signed as Verifiable Presentation.
Export(context.Context, *ExportRequest) (res interface{}, err error)
}
......
package config
import "time"
type Config struct {
HTTP httpConfig
LogLevel string `envconfig:"LOG_LEVEL" default:"INFO"`
}
type httpConfig struct {
Host string `envconfig:"HTTP_HOST"`
Port string `envconfig:"HTTP_PORT" default:"8080"`
IdleTimeout time.Duration `envconfig:"HTTP_IDLE_TIMEOUT" default:"120s"`
ReadTimeout time.Duration `envconfig:"HTTP_READ_TIMEOUT" default:"10s"`
WriteTimeout time.Duration `envconfig:"HTTP_WRITE_TIMEOUT" default:"10s"`
}
package service
import (
goahttp "goa.design/goa/v3/http"
goa "goa.design/goa/v3/pkg"
"code.vereign.com/gaiax/tsa/golib/errors"
)
func NewErrorResponse(err error) goahttp.Statuser {
if err == nil {
return nil
}
var newerr *errors.Error
switch e := err.(type) {
case *errors.Error:
newerr = e
case *goa.ServiceError:
// Use goahttp.ErrorResponse to determine error kind
goaerr := goahttp.NewErrorResponse(e)
kind := errors.GetKind(goaerr.StatusCode())
newerr = &errors.Error{
ID: e.ID,
Kind: kind,
Message: e.Message,
Err: e,
}
default:
newerr = &errors.Error{
ID: errors.NewID(),
Kind: errors.Internal,
Message: e.Error(),
Err: e,
}
}
return newerr
}
package health
import "context"
type Service struct{}
func New() *Service {
return &Service{}
}
func (s *Service) Liveness(ctx context.Context) error {
return nil
}
func (s *Service) Readiness(ctx context.Context) error {
return nil
}
package infohub
import (
"context"
"fmt"
"go.uber.org/zap"
"code.vereign.com/gaiax/tsa/infohub/gen/infohub"
)
type Service struct {
logger *zap.Logger
}
func New(logger *zap.Logger) *Service {
return &Service{logger: logger}
}
func (s *Service) Export(ctx context.Context, req *infohub.ExportRequest) (interface{}, error) {
return nil, fmt.Errorf("not implemented")
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment