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/v3" _ "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
- func GetConfigName() string
- func InitAdmin(db *sql.DB, dbtype string) error
- func InitTelemetry(context context.Context, exporter trace.SpanExporter, ...) error
- func NewDB(conf *Config, openDB bool, log *zap.SugaredLogger, fs core.FS) (*sql.DB, error)
- func SecretsCmd(cmdName, fileName string, sa SecretArgs, args []string, log *zap.SugaredLogger) error
- type Admin
- type Auth
- type Client
- type Config
- type Core
- type Database
- type DeployReq
- type HookFn
- type HttpService
- func (s *HttpService) Attach(mux Mux) error
- func (s *HttpService) AttachWithNS(mux Mux, namespace string) error
- func (s *HttpService) Deploy(conf *Config, options ...Option) error
- func (s *HttpService) GetDB() *sql.DB
- func (s *HttpService) GetGraphJin() *core.GraphJin
- func (s *HttpService) GraphQL(ah auth.HandlerFunc) http.Handler
- func (s *HttpService) GraphQLWithNS(ah auth.HandlerFunc, ns string) http.Handler
- func (s *HttpService) REST(ah auth.HandlerFunc) http.Handler
- func (s *HttpService) RESTWithNS(ah auth.HandlerFunc, ns string) http.Handler
- func (s *HttpService) Reload() error
- func (s *HttpService) Start() error
- func (s *HttpService) WebUI(routePrefix, gqlEndpoint string) http.Handler
- type JWTConfig
- type Mux
- type Option
- type Payload
- type RateLimiter
- type Resp
- type SecretArgs
- type Serv
Constants ¶
const ( DeployRoute = "/api/v1/deploy" RollbackRoute = "/api/v1/deploy/rollback" )
Variables ¶
This section is empty.
Functions ¶
func GetConfigName ¶
func GetConfigName() string
GetConfigName returns the name of the configuration
func InitTelemetry ¶
func InitTelemetry( context context.Context, exporter trace.SpanExporter, serviceName, serviceInstanceID string, ) error
InitTelemetry initializes the OpenTelemetry SDK with the given exporter and service name.
func SecretsCmd ¶
func SecretsCmd(cmdName, fileName string, sa SecretArgs, args []string, log *zap.SugaredLogger) error
SecretsCmd runs the secrets command
Types ¶
type Admin ¶
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 Client ¶
type Client struct {
*resty.Client
}
func NewAdminClient ¶
NewAdminClient creates a new admin client
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 ¶
NewConfig function creates a new GraphJin configuration from the provided config string
func ReadInConfig ¶
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 ¶
ReadInConfigFS is the same as ReadInConfig but it also takes a filesytem as an argument
func (*Config) AbsolutePath ¶
AbsolutePath returns the absolute path of the file
func (*Config) GetSecretOrEnv ¶
GetSecretOrEnv returns the value of the secret key if it exists or the value of the environment variable
type Database ¶
type Database struct { ConnString string `mapstructure:"connection_string" jsonschema:"title=Connection String"` 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 HttpService ¶
func NewGraphJinService ¶
func NewGraphJinService(conf *Config, options ...Option) (*HttpService, error)
NewGraphJinService a new service
func (*HttpService) Attach ¶
func (s *HttpService) Attach(mux Mux) error
Attach route to the internal http service
func (*HttpService) AttachWithNS ¶
func (s *HttpService) AttachWithNS(mux Mux, namespace string) error
AttachWithNS a namespaced route to the internal http service
func (*HttpService) Deploy ¶
func (s *HttpService) Deploy(conf *Config, options ...Option) error
Deploy a new configuration
func (*HttpService) GetGraphJin ¶
func (s *HttpService) GetGraphJin() *core.GraphJin
GetGraphJin fetching internal GraphJin core
func (*HttpService) GraphQL ¶
func (s *HttpService) GraphQL(ah auth.HandlerFunc) http.Handler
GraphQLis the http handler the GraphQL endpoint
func (*HttpService) GraphQLWithNS ¶
func (s *HttpService) GraphQLWithNS(ah auth.HandlerFunc, ns string) http.Handler
GraphQLWithNS is the http handler the namespaced GraphQL endpoint
func (*HttpService) REST ¶
func (s *HttpService) REST(ah auth.HandlerFunc) http.Handler
REST is the http handler the REST endpoint
func (*HttpService) RESTWithNS ¶
func (s *HttpService) RESTWithNS(ah auth.HandlerFunc, ns string) http.Handler
RESTWithNS is the http handler the namespaced REST endpoint
func (*HttpService) Reload ¶
func (s *HttpService) Reload() error
Reload re-runs database discover and reinitializes service.
func (*HttpService) Start ¶
func (s *HttpService) Start() error
Start the service listening on the configured port
type Option ¶
type Option func(*graphjinService) error
func OptionDeployActive ¶
func OptionDeployActive() Option
OptionDeployActive caused the active config to be deployed on
func OptionSetHookFunc ¶
OptionSetHookFunc sets a function to be called on every request
func OptionSetNamespace ¶
OptionSetNamespace sets service namespace
func OptionSetZapLogger ¶
OptionSetZapLogger sets service structured logger
type Payload ¶
type Payload struct { Data json.RawMessage `json:"data,omitempty"` Errors []core.Error `json:"errors,omitempty"` }
type RateLimiter ¶
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 SecretArgs ¶
type SecretArgs struct {
KMS, KMSC, AWS, GCP, Azure, PGP string
}
SecretArgs holds the arguments for the secrets command
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"` // Database configuration DB Database `mapstructure:"database" jsonschema:"title=Database"` }
Configuration for the GraphJin Service