serv

package
v0.21.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2022 License: Apache-2.0 Imports: 60 Imported by: 0

Documentation

Overview

Package serv provides an API to include and use the GraphJin service with your own code. For detailed documentation visit https://graphjin.com

Example usage:

package main

import (
	"database/sql"
	"fmt"
	"time"
	"github.com/dosco/graphjin/core"
	_ "github.com/jackc/pgx/v5/stdlib"
)

func main() {
	conf := serv.Config{ AppName: "Test App" }
	conf.DB.Host := "127.0.0.1"
	conf.DB.Port := 5432
	conf.DB.DBName := "test_db"
	conf.DB.User := "postgres"
	conf.DB.Password := "postgres"

	gjs, err := serv.NewGraphJinService(conf)
	if err != nil {
		log.Fatal(err)
	}

 	if err := gjs.Start(); err != nil {
		log.Fatal(err)
	}
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetConfigName added in v0.17.7

func GetConfigName() string

func InitAdmin added in v0.17.0

func InitAdmin(db *sql.DB, dbtype string) error

func InitTelemetry added in v0.20.4

func InitTelemetry(
	c context.Context,
	exp trace.SpanExporter,
	serviceName, serviceInstanceID string) error

func NewDB added in v0.17.0

func NewDB(conf *Config, openDB bool, log *zap.SugaredLogger, fs afero.Fs) (*sql.DB, error)

Types

type Action

type Action struct {
	Name     string
	SQL      string
	AuthName string `mapstructure:"auth_name"`
}

Action struct contains config values for a GraphJin service action

type Admin added in v0.17.0

type Admin struct {
	// Enables the ability to hot-deploy a new configuration
	HotDeploy bool `mapstructure:"hot_deploy" jsonschema:"title=Enable Hot Deploy"`

	// Secret key used to control access to the admin api
	AdminSecretKey string `mapstructure:"admin_secret_key" jsonschema:"title=Admin API Secret Key"`
}

Configuration for admin service

type Auth added in v0.16.38

type Auth = auth.Auth

type BuildInfo

type BuildInfo struct {
	Version string
	Commit  string
	Date    string
}

func GetBuildInfo

func GetBuildInfo() BuildInfo

type Config

type Config struct {

	// Configuration for the GraphJin compiler core
	Core `mapstructure:",squash" jsonschema:"title=Compiler Configuration"`

	// Configuration for the GraphJin Service
	Serv `mapstructure:",squash" jsonschema:"title=Service Configuration"`

	// Configuration for admin service
	Admin `mapstructure:",squash" jsonschema:"title=Admin Configuration"`
	// contains filtered or unexported fields
}

Configuration for the GraphJin service

func NewConfig added in v0.17.13

func NewConfig(config, format string) (*Config, error)

func ReadInConfig

func ReadInConfig(configFile string) (*Config, error)

ReadInConfig function reads in the config file for the environment specified in the GO_ENV environment variable. This is the best way to create a new GraphJin config.

func ReadInConfigFS added in v0.17.0

func ReadInConfigFS(configFile string, fs afero.Fs) (*Config, error)

ReadInConfigFS is the same as ReadInConfig but it also takes a filesytem as an argument

func (*Config) GetSecret added in v0.17.24

func (c *Config) GetSecret(k string) (string, bool)

func (*Config) GetSecretOrEnv added in v0.17.24

func (c *Config) GetSecretOrEnv(k string) string

func (*Config) RelPath

func (c *Config) RelPath(p string) string

func (*Config) SetHash added in v0.17.0

func (c *Config) SetHash(hash string)

func (*Config) SetName added in v0.17.0

func (c *Config) SetName(name string)

type Core

type Core = core.Config

type Database added in v0.16.56

type Database struct {
	Type     string `jsonschema:"title=Type,enum=postgres,enum=mysql"`
	Host     string `jsonschema:"title=Host"`
	Port     uint16 `jsonschema:"title=Port"`
	DBName   string `jsonschema:"title=Database Name"`
	User     string `jsonschema:"title=User"`
	Password string `jsonschema:"title=Password"`
	Schema   string `jsonschema:"title=Postgres Schema"`

	// Size of database connection pool
	PoolSize int `mapstructure:"pool_size" jsonschema:"title=Connection Pool Size"`

	// Max number of active database connections allowed
	MaxConnections int `mapstructure:"max_connections" jsonschema:"title=Maximum Connections"`

	// Max time after which idle database connections are closed
	MaxConnIdleTime time.Duration `mapstructure:"max_connection_idle_time" jsonschema:"title=Connection Idel Time"`

	// Max time after which database connections are not reused
	MaxConnLifeTime time.Duration `mapstructure:"max_connection_life_time" jsonschema:"title=Connection Life Time"`

	// Database ping timeout is used for db health checking
	PingTimeout time.Duration `mapstructure:"ping_timeout" jsonschema:"title=Healthcheck Ping Timeout"`

	// Set up an secure TLS encrypted database connection
	EnableTLS bool `mapstructure:"enable_tls" jsonschema:"title=Enable TLS"`

	// Required for TLS. For example with Google Cloud SQL it's
	// <gcp-project-id>:<cloud-sql-instance>
	ServerName string `mapstructure:"server_name" jsonschema:"title=TLS Server Name"`

	// Required for TLS. Can be a file path or the contents of the PEM file
	ServerCert string `mapstructure:"server_cert" jsonschema:"title=Server Certificate"`

	// Required for TLS. Can be a file path or the contents of the PEM file
	ClientCert string `mapstructure:"client_cert" jsonschema:"title=Client Certificate"`

	// Required for TLS. Can be a file path or the contents of the pem file
	ClientKey string `mapstructure:"client_key" jsonschema:"title=Client Key"`
}

Database configuration

type HookFn added in v0.17.29

type HookFn func(*core.Result)

type JWTConfig added in v0.16.56

type JWTConfig = auth.JWTConfig

type Mux added in v0.17.21

type Mux interface {
	Handle(string, http.Handler)
	ServeHTTP(http.ResponseWriter, *http.Request)
}

type Option added in v0.17.0

type Option func(*service) error

func OptionDeployActive added in v0.17.1

func OptionDeployActive() Option

func OptionSetDB added in v0.20.31

func OptionSetDB(db *sql.DB) Option

func OptionSetFS added in v0.17.0

func OptionSetFS(fs afero.Fs) Option

func OptionSetHookFunc added in v0.17.29

func OptionSetHookFunc(fn HookFn) Option

func OptionSetNamespace added in v0.17.22

func OptionSetNamespace(namespace string) Option

func OptionSetZapLogger added in v0.17.24

func OptionSetZapLogger(zlog *zap.Logger) Option

type Payload added in v0.17.13

type Payload struct {
	Data   json.RawMessage `json:"data,omitempty"`
	Errors []core.Error    `json:"errors,omitempty"`
}

type RateLimiter added in v0.16.56

type RateLimiter struct {
	// The number of events per second
	Rate float64 `jsonschema:"title=Connection Rate"`

	// Bucket a burst of at most 'bucket' number of events
	Bucket int `jsonschema:"title=Bucket Size"`

	// The header that contains the client ip
	IPHeader string `mapstructure:"ip_header" jsonschema:"title=IP From HTTP Header,example=X-Forwarded-For"`
}

RateLimiter sets the API rate limits

type Serv

type Serv struct {
	// Application name is used in log and debug messages
	AppName string `mapstructure:"app_name" jsonschema:"title=Application Name"`

	// When enabled runs the service with production level security defaults.
	// For example allow lists are enforced.
	Production bool `jsonschema:"title=Production Mode,default=false"`

	// The default path to find all configuration files and scripts
	ConfigPath string `mapstructure:"config_path" jsonschema:"title=Config Path"`

	// The file for the secret key store. This must be a Mozilla SOPS file
	SecretsFile string `mapstructure:"secrets_file" jsonschema:"title=Secrets File"`

	// Logging level must be one of debug, error, warn, info
	LogLevel string `mapstructure:"log_level" jsonschema:"title=Log Level,enum=debug,enum=error,enum=warn,enum=info"`

	// Logging Format must me either json or simple
	LogFormat string `mapstructure:"log_format" jsonschema:"title=Logging Level,enum=json,enum=simple"`

	// The host and port the service runs on. Example localhost:8080
	HostPort string `mapstructure:"host_port" jsonschema:"title=Host and Port"`

	// Host to run the service on
	Host string `jsonschema:"title=Host"`

	// Port to run the service on
	Port string `jsonschema:"title=Port"`

	// Enables HTTP compression
	HTTPGZip bool `mapstructure:"http_compress" jsonschema:"title=Enable Compression,default=true"`

	// Sets the API rate limits
	RateLimiter RateLimiter `mapstructure:"rate_limiter" jsonschema:"title=Set API Rate Limiting"`

	// Enables the Server-Timing HTTP header
	ServerTiming bool `mapstructure:"server_timing" jsonschema:"title=Server Timing HTTP Header,default=true"`

	// Enable the web UI. Disabled in production
	WebUI bool `mapstructure:"web_ui" jsonschema:"title=Enable Web UI,default=false"`

	// Enable OpenTrace request tracing
	EnableTracing bool `mapstructure:"enable_tracing" jsonschema:"title=Enable Tracing,default=true"`

	// Enables reloading the service on config changes. Disabled in production
	WatchAndReload bool `mapstructure:"reload_on_config_change" jsonschema:"title=Reload Config"`

	// Enable blocking requests with a HTTP 401 on auth failure
	AuthFailBlock bool `mapstructure:"auth_fail_block" jsonschema:"title=Block Request on Authorization Failure"`

	// This is the path to the database migration files
	MigrationsPath string `mapstructure:"migrations_path" jsonschema:"title=Migrations Path"`

	// Sets the HTTP CORS Access-Control-Allow-Origin header
	AllowedOrigins []string `mapstructure:"cors_allowed_origins" jsonschema:"title=HTTP CORS Allowed Origins"`

	// Sets the HTTP CORS Access-Control-Allow-Headers header
	AllowedHeaders []string `mapstructure:"cors_allowed_headers" jsonschema:"title=HTTP CORS Allowed Headers"`

	// Enables debug logs for CORS
	DebugCORS bool `mapstructure:"cors_debug" jsonschema:"title=Log CORS"`

	// Sets the HTTP Cache-Control header
	CacheControl string `mapstructure:"cache_control" jsonschema:"title=Enable Cache-Control"`

	// Sets the default authentication used by the service
	Auth Auth `jsonschema:"title=Authentication"`

	// Sets multiple authentication mechanism which can be used with actions
	Auths []Auth `jsonschema:"title=Other Authentication"`

	// Database configuration
	DB Database `mapstructure:"database" jsonschema:"title=Database"`

	Actions []Action `jsonschema:"-"`
}

Configuration for the GraphJin Service

type Service

type Service struct {
	atomic.Value
	// contains filtered or unexported fields
}

func NewGraphJinService added in v0.16.38

func NewGraphJinService(conf *Config, options ...Option) (*Service, error)

func (*Service) Attach

func (s *Service) Attach(mux Mux) error

func (*Service) AttachWithNamespace added in v0.17.22

func (s *Service) AttachWithNamespace(mux Mux, namespace string) error

func (*Service) Deploy added in v0.17.0

func (s *Service) Deploy(conf *Config, options ...Option) error

func (*Service) GetDB added in v0.17.19

func (s *Service) GetDB() *sql.DB

func (*Service) GetGraphJin added in v0.20.23

func (s *Service) GetGraphJin() *core.GraphJin

func (*Service) Reload added in v0.20.2

func (s *Service) Reload() error

Reload redoes database discover and reinitializes GraphJin.

func (*Service) Start

func (s *Service) Start() error

func (*Service) Subscribe added in v0.17.19

func (s *Service) Subscribe(
	c context.Context,
	query string,
	vars json.RawMessage,
	rc *core.ReqConfig) (*core.Member, error)

Directories

Path Synopsis
module
Package auth provides an API to use GraphJin serv auth handles with your own application.
Package auth provides an API to use GraphJin serv auth handles with your own application.
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL