Documentation
¶
Overview ¶
*
- ast.go holds the data structure that defines Newt applications. *
- @author R. S. Doiel
*
- cli.go an implements runners for the cli of the Newt Project. *
- @author R. S. Doiel
Index ¶
- Constants
- func FmtHelp(src string, appName string, version string, releaseDate string, ...) string
- func NewtStaticFileServer(port int, htdocs string, verbose bool) error
- func PatternKeys(p string) []string
- func RunBuilder(in io.Reader, out io.Writer, eout io.Writer, args []string) int
- func RunGenerator(in io.Reader, out io.Writer, eout io.Writer, args []string) int
- func RunModeler(in io.Reader, out io.Writer, eout io.Writer, args []string) int
- func RunNewt(in io.Reader, out io.Writer, eout io.Writer, args []string, verbose bool) int
- func RunNewtCheckYAML(in io.Reader, out io.Writer, eout io.Writer, args []string, verbose bool) int
- func RunNewtConfig(in io.Reader, out io.Writer, eout io.Writer, args []string, verbose bool) int
- func RunNewtServices(in io.Reader, out io.Writer, eout io.Writer, args []string, verbose bool) int
- func RunRouter(in io.Reader, out io.Writer, eout io.Writer, args []string, dryRun bool, ...) int
- func RunStaticWebServer(in io.Reader, out io.Writer, eout io.Writer, args []string, port int, ...) int
- func RunTemplateEngine(in io.Reader, out io.Writer, eout io.Writer, args []string, port int, ...) int
- func TmplFooterPartial(out io.Writer, copyrightYear string, copyrightLink string, ...) error
- func TmplGen(out io.Writer, model *models.Model, action string) error
- func TmplHeadPartial(out io.Writer, defaultTitle string, cssPath string) error
- func TmplHeaderPartial(out io.Writer, defaultHeaderLink string, defaultHeaderLinkTitle string, ...) error
- func TmplNavPartial(out io.Writer, navElement string) error
- func UnmarshalAST(src []byte, ast *AST) error
- type AST
- func (ast *AST) AddModel(model *models.Model) error
- func (ast *AST) Check(buf io.Writer) bool
- func (ast *AST) Encode() ([]byte, error)
- func (ast *AST) GetAllTemplates() []string
- func (ast *AST) GetModelById(id string) (*models.Model, bool)
- func (ast *AST) GetModelIds() []string
- func (ast *AST) GetPrimaryTemplates() []string
- func (ast *AST) GetRouteIds() []string
- func (ast *AST) GetService(appName string) *Service
- func (ast *AST) GetTemplateByPrimary(fName string) (*Template, bool)
- func (ast *AST) GetTemplateIds() []string
- func (ast *AST) HasChanges() bool
- func (ast *AST) RemoveModelById(id string) error
- func (ast *AST) RemoveRouteById(id string) error
- func (ast *AST) RemoveService(appName string) error
- func (ast *AST) RemoveTemplateById(id string) error
- func (ast *AST) SaveAs(configName string) error
- func (ast *AST) UpdateModel(id string, model *models.Model) error
- type AppMetadata
- type BodyGen
- type Generator
- type Logger
- type Route
- func (nr *Route) Check(buf io.Writer) bool
- func (nr *Route) Handler(w http.ResponseWriter, r *http.Request)
- func (nr *Route) ResolvePattern(r *http.Request) map[string]interface{}
- func (nr *Route) ResolveRoute() error
- func (nr *Route) RunPipeline(w http.ResponseWriter, r *http.Request, env map[string]interface{}) ([]byte, int, string, error)
- type RouteService
- type Router
- type Service
- type Template
- type TemplateEngine
Constants ¶
const ( // These constants are used for exit code. FIXME: look up the POSIX recommendation on exit // codes and adopt those. OK = iota CONFIG // General failure of a command or service INIT_FAIL CHECK_FAIL MODELER_FAIL GENERATOR_FAIL BUILD_FAIL ROUTER_FAIL TEMPLATE_ENGINE_FAIL NEWT_FAIL SWS_FAIL POSTGREST_FAIL // Internal service failures RESOLVE HANDLER SERVER_ERROR UNSUPPORTED_ACTION DATA_ERROR READ_ERROR DECODE_ERROR TEMPLATE_ERROR // Default service settings ROUTER_PORT = 8010 TEMPLATE_ENGINE_PORT = 8011 TEMPLATE_ENGINE_TIMEOUT = 3 * time.Second TEMPLATE_ENGINE_BASE_DIR = "views" TEMPLATE_ENGINE_PARTIALS_DIR = "partials" TEMPLATE_ENGINE_EXT_NAME = ".hbs" SWS_PORT = 8000 SWS_HTDOCS = "." POSTGREST_PORT = 3000 POSTGRES_PORT = 5432 )
const ( TuiStandardMenuHelp = `Enter menu letter and id` TuiStandardMenu = `Menu [a]dd, [m]odify, [r]emove or press enter when done` )
const ( // Version number of release Version = "0.0.9" // ReleaseDate, the date version.go was generated ReleaseDate = "2025-04-02" // ReleaseHash, the Git hash when version.go was generated ReleaseHash = "3429ddf" LicenseText = `` /* 1430-byte string literal not displayed */ )
Variables ¶
This section is empty.
Functions ¶
func FmtHelp ¶
func FmtHelp(src string, appName string, version string, releaseDate string, releaseHash string) string
FmtHelp lets you process a text block with simple curly brace markup.
func PatternKeys ¶
PatternKeys parses a pattern and returns a list of keys found. NOTE: this could be improved to make sure that delimiters are paired and that the pattern's names do not contain spaces.
func RunBuilder ¶ added in v0.0.9
RunBuilder is a runner for take the generating SQL, templates, etc. and Generate the validator middleware a well as updating the Postgres+PostgRESTS configuration and databases.
func RunGenerator ¶ added in v0.0.9
RunGenerator is a runner for generating SQL and templates from our Newt YAML file.
func RunModeler ¶ added in v0.0.9
func RunNewt ¶
RunNewt is a runner that can run Newt's router and template engine plus PostgREST if defined in the Newt YAML file.
func RunNewtCheckYAML ¶
NewtRunCheckYAML will load a Newt YAML fiel and make sure it can parse the configuration.
func RunNewtConfig ¶ added in v0.0.9
RunNewtConfig will initialize a Newt project by creating a Newt YAML file interactively.
func RunNewtServices ¶ added in v0.0.9
RunNewtServices will run the applictions defined in your Newt YAML file.
func RunRouter ¶ added in v0.0.9
func RunRouter(in io.Reader, out io.Writer, eout io.Writer, args []string, dryRun bool, port int, htdocs string, verbose bool) int
RunRouter is a runner for Newt data router and static file service
func RunStaticWebServer ¶
func RunStaticWebServer(in io.Reader, out io.Writer, eout io.Writer, args []string, port int, verbose bool) int
RunStaticWebServer this provides a localhost for static file content.
func RunTemplateEngine ¶ added in v0.0.9
func RunTemplateEngine(in io.Reader, out io.Writer, eout io.Writer, args []string, port int, timeout int, verbose bool) int
RunTemplateEngine is a runner for a Newt's template engine.
func TmplFooterPartial ¶ added in v0.0.9
func TmplGen ¶ added in v0.0.9
TmplGen takes an io.Writer, model and an action string rendering the contents of the model as a Newt handlebars template for the provided action. It returns an error value when something goes wrong.
func TmplHeadPartial ¶ added in v0.0.9
func TmplHeaderPartial ¶ added in v0.0.9
func UnmarshalAST ¶ added in v0.0.9
UnmarshalAST will read []byte of YAML or JSON, populate the provided *AST object and return an error.
``` src, _ := os.ReadFile("app.yaml") ast := new(AST)
if err := UnmarshalAST(src, ast); err != nil {
// ... handle error
}
```
Types ¶
type AST ¶ added in v0.0.9
type AST struct {
// AppMetadata holds your application's metadata such as needed to render an "about" page in your final app.
AppMetadata *AppMetadata `json:"app_metadata,omitempty" yaml:"app_metadata,omitempty"`
// Services holds definitions of the services used to compose your application.
// and enough metadata to generated appropriate Systemd and Luanchd configurations.
Services []*Service `json:"services,omitempty" yaml:"services,omitempty"`
// Models holds a list of data models. It is used by
// both the data router and code generator.
Models []*models.Model `json:"models,omitempty" yaml:"models,omitempty"`
// Routes holds an array of maps of route definitions used by
// the data router and code generator
Routes []*Route `json:"routes,omitempty" yaml:"routes,omitempty"`
// Templates holds an array of maps the request to template to request for
// Newt (Handlebars) template engine
Templates []*Template `json:"templates,omitempty" yaml:"templates,omitempty"`
// contains filtered or unexported fields
}
AST holds a configuration for Newt for the data router and code generator.
func LoadAST ¶ added in v0.0.9
LoadAST read a YAML file, merges environment variables and returns a AST object and error value.
``` ast, err := LoadAST("app.yaml")
if err != nil {
// ... handle error
}
```
func NewAST ¶ added in v0.0.9
func NewAST() *AST
NewAST will create an empty AST with top level attributes
func (*AST) AddModel ¶ added in v0.0.9
AddModel takes a new Model, checks if the model exists in the list (i.e. has an existing model id that matches the new model and if not appends it so the list.
func (*AST) Check ¶ added in v0.0.9
Check reviews the ast *AST and reports and issues, return true if no errors found and false otherwise. The "buf" will hold the error output.
func (*AST) GetAllTemplates ¶ added in v0.0.9
GetAllTemplates returns a list of templates, including partials defined in the .Templates property. Part template names are indented with a "\t"
func (*AST) GetModelById ¶ added in v0.0.9
GetModelById return a specific model by it's id
func (*AST) GetModelIds ¶ added in v0.0.9
GetModelIds returns a list of model ids
func (*AST) GetPrimaryTemplates ¶ added in v0.0.9
GetPrimaryTemplates return a list of primary template filenames
func (*AST) GetRouteIds ¶ added in v0.0.9
GetRouteIds returns a list of Router ids found in ast.Routes
func (*AST) GetService ¶ added in v0.0.9
GetService takes a list of applications, `[]*Service`, and returns the application name in the list or nil.
func (*AST) GetTemplateByPrimary ¶ added in v0.0.9
GetTemplateByPrimary returns the template entry using primary template filename
func (*AST) GetTemplateIds ¶ added in v0.0.9
GetTemplateIds return a list of template ids.
func (*AST) HasChanges ¶ added in v0.0.9
func (*AST) RemoveModelById ¶ added in v0.0.9
RemoveModelById find the model with the model id and remove it
func (*AST) RemoveRouteById ¶ added in v0.0.9
RemoveRouteById find the route with route id and remove it
func (*AST) RemoveService ¶ added in v0.0.9
RemoveService takes a list of applications, `[]*Service`, and remove the target item.
func (*AST) RemoveTemplateById ¶ added in v0.0.9
RemoveTemplateById() find the template id and remove it from the .Templates structure
type AppMetadata ¶ added in v0.0.9
type AppMetadata struct {
AppName string `json:"name,omitempty" yaml:"app_name,omitempty"`
AppTitle string `json:"title,omitempty" yaml:"app_title,omitempty"`
CopyrightYear string `json:"copyright_year,omitempty" yaml:"copyright_year,omitempty"`
CopyrightLink string `json:"copyright_link,omitempty" yaml:"copyright_link,omitempty"`
CopyrightText string `json:"copyright_text,omitempty" yaml:"copyright_text,omitempty"`
LogoLink string `json:"logo_link,omitempty" yaml:"logo_link,omitempty"`
LogoText string `json:"logo_text,omitempty" yaml:"logo_text,omitempty"`
LicenseLink string `json:"license_link,omitempty" yaml:"license_link,omitempty"`
LicenseText string `json:"license_text,omitempty" yaml:"license_text,omitempty"`
CSSPath string `json:"css_path,omitempty" yaml:"css_path,omitempty"`
HeaderLink string `json:"header_link,omitempty" yaml:"header_link,omitempty"`
HeaderText string `json:"header_text,omitempty" yaml:"header_text,omitempty"`
ContactAddress string `json:"contact_address,omitempty" yaml:"contact_address,omitempty"`
ContactPhone string `json:"contact_phone,omitempty" yaml:"contact_phone,omitempty"`
ContactEMail string `json:"contact_email,omitempty" yaml:"contact_email,omitempty"`
}
AppMetadata holds metadata about your Newt Service This is primarily used in generated Handlbars partials
type Generator ¶ added in v0.0.9
type Generator struct {
// Namespace is used when generating the SQL/conf for setting up Postgres+PostgREST
Namespace string
// Models holds the models used to generator specific code
Models []*models.Model
// Options holds the result environment variables and options that can be used in generator code
Options map[string]interface{}
// AppMetadata holds the metadata for the application being generated
AppMetadata *AppMetadata
// Postgres configuration information
Postgres *Service
// PostgREST configuration information
PostgREST *Service
// TemplateEngine configuration information
TemplateEngine *Service
// contains filtered or unexported fields
}
Generator holds our Newt Generator structures for rendering code.
func NewGenerator ¶
NewGenerator instaitates a new Generator object form a filename and AST object It returns a Generator object and error value.
func (*Generator) Generate ¶ added in v0.0.9
Generator generates the code based on the contents of Generator struct. It will also verify that the needed parameters are provided.
- generatorName is the generator to use - action is a parameter that the selected generator can use (e.g. PostgreSQL has setup as well as ) - modelId references the `.id` attribute of the model needing code generation
type Logger ¶
type Logger struct {
// After (defaults to true) logs the request after running the wrapped handler
After bool
// Verbose (defaults to false) show the contents of a GET, POST, PUT and DELETE in log output
Verbose bool
// contains filtered or unexported fields
}
Logger implementes Newt Project's web logging
type Route ¶ added in v0.0.9
type Route struct {
// Id holds an id for the route. It is unique for each set of HTTP methods. E.g. The id can
// tie a GET (retrieve a webform) and POST (submit the form) but NOT two GET or two POST.
// This allows us to work with simple and complex actions.
Id string `json:"id,omitempty" yaml:"id,omitempty"`
// Pattern holds the HTTP Method and URL path, may include Go 1.22 patterns
Pattern string `json:"request,required" yaml:"request,omitempty"`
// Description holds a human describe of the purpose of this route.
Description string `json:"description,omitempty" yaml:"description,omitempty"`
// Pipeline holds the series of http services context with the output of one sent to another.
Pipeline []*RouteService `json:"pipeline,omitempty" yaml:"pipeline,omitempty"`
// Debug if true log verbosely
Debug bool `json:"debug,omitempty" yaml:"debug,omitempty"`
// Env holds a map of defaults that are available from the environment and from path values in the url
Options map[string]interface{} `json:"options,omitempty" yaml:"options,omitempty"`
// Vars holds the variables defined in the route
Vars []string `json:"-" yaml:"-"`
}
This holds the route definitions, e.g. request, description, pipeline, debug
func (*Route) Check ¶ added in v0.0.9
Check will review the route and return true if no errors and false othise. It will write error descriptions to buf.
func (*Route) Handler ¶ added in v0.0.9
func (nr *Route) Handler(w http.ResponseWriter, r *http.Request)
Handler creates an http handler func for a given route.
func (*Route) ResolvePattern ¶ added in v0.0.9
ResolvePattern takes the request Pattern and pulls out the values from the actual request. returns values in map[string]string and an error value.
func (*Route) ResolveRoute ¶ added in v0.0.9
ResolveRoute reviews the `.Request` attribute and updates the Vars using PatternKeys()
type RouteService ¶ added in v0.0.9
type RouteService struct {
// Service holds the http Request Pattern to request a reast from a service
Service string `json:"service,required" yaml:"service,omitempty"`
// Description describes the service and purpose of contact. Human readable.
Description string `json:"description,omitempty" yaml:"description,omitempty"`
// Timeout sets a timeout value to recieve a response from the service.
Timeout time.Duration `json:"timeout,omitempty" yaml:"timeout,omitempty"`
}
RouteService holds the necessary information to contact a data ast and retrieve the results for use in a pipeline.
type Router ¶ added in v0.0.9
type Router struct {
// Port is the port the router will listen on
Port int
// Routes holds a list of route
Routes []*Route
// Htdocs holds the location of a the static files if used
Htdocs string
}
Router is used to implement the Newt Router
func (*Router) Check ¶ added in v0.0.9
Check will review the router and its routes and return true if no errors found and false otherwise. It will write error descriptions to buf.
func (*Router) GetRouteIds ¶ added in v0.0.9
GetRouteIds returns a list of route ids defined in the Router.
func (*Router) ListenAndServe ¶ added in v0.0.9
ListenAndServe() runs the router web service
type Service ¶
type Service struct {
// AppName holds the name of the application, e.g. Postgres, PostgREST
AppName string `josn:"name,omitempty" yaml:"name,omitempty"`
// AppPath holds the path to the binary application, e.g. PostgREST
// This property provides the location of the service to run.
AppPath string `json:"path,omitempty" yaml:"path,omitempty"`
// ConfPath holds teh path to the configuration file (e.g. PostgREST configuration file)
ConfPath string `json:"conf_path,omitempty" yaml:"conf_path,omitempty"`
// Namespace holds the Postgres Schema name It is used to generate
// a setup.sql file using the -pg-setup option in newt cli.
Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
// CName is the name of the dataset collection you wish to use/generate.
CName string `json:"c_name,omitempty" yaml:"c_name,omitempty"`
// Port is the name of the localhost port Newt will listen on.
Port int `json:"port,omitempty" yaml:"port,omitempty"`
// Timeout is a duration, it is used to set timeouts and the application.
Timeout time.Duration `json:"timeout,omitempty" yaml:"timeout,omitempty"`
// Htdocs holds any static files you want to make available through
// Newt router.
Htdocs string `json:"htdocs,omitempty" yaml:"htdocs,omitempty"`
// BaseDir is used by Handlebars, usually holds the "views" directory.
BaseDir string `json:"base_dir,omitempty" yaml:"base_dir,omitempty"`
// ExtName is used by Handlebars to set the expected extension (e.g. ".hbs")
ExtName string `json:"ext_name,omitempty" yaml:"ext_name,omitempty"`
// PartialsDir is used by Handlebars to find partial templates, usually inside the views directory
PartialsDir string `json:"partials_dir,omitempty" yaml:"partials_dir,omitempty"`
// DSN, data ast name is a URI connection string
DSN string `json:"dsn,omitemity" yaml:"dsn,omitempty"`
// Environment holds a list of OS environment variables that can be made
// available to the web services.
Environment []string `json:"environment,omitempty" yaml:"enviroment,omitempty"`
// Options is a map of name to string values, it is where the
// the environment variable valuess are stored.
Options map[string]interface{} `json:"options,omitempty" yaml:"options,omitempty"`
}
Service implements runtime config for Newt and off the shelf programs used to compose your Newt based application.
func NewServices ¶ added in v0.0.9
func NewServices() []*Service
NewServices generates a default set of applications for your Newt project.
type Template ¶ added in v0.0.9
type Template struct {
// Id ties a set of one or more template together, e.g. a web form and its response
Id string `json:"id,required" yaml:"id,omitempty"`
// Description describes the purpose of the tempalte mapping. It is used to debug Newt YAML files.
Description string `json:"description,omitempty" yaml:"description,omitempty"`
// Pattern holds a request path, e.g. `/blog_post`. NOTE: the method is ignored. A POST
// is presumed to hold data that will be processed by the template engine. A GET retrieves the
// unresolved template.
Pattern string `json:"request,required" yaml:"request,omitempty"`
// Template holds a path to the primary template (aka view) file for this route. Path can be relative
// to the current working directory.
Template string `json:"template,required" yaml:"template,omitempty"`
// Debug logs more verbosely if true
Debug bool `json:"debug,omitempty" yaml:"debug,omitempty"`
// Document hold the a map of values passed into it from the Newt YAML file in the applications
// property. These are a way to map in environment or application wide values. These are exposed in
// the Newt template engine `options`.
Document map[string]interface{} `json:"document,omitempty" yaml:"document,omitempty"`
// Vars holds the names of any variables expressed in the pattern, these an be used to replace elements of
// the output object.
Vars []string `json:"-" yaml:"-"`
// Body holds a map of data to process with the template
Body map[string]interface{} `json:"-" yaml:"-"`
// Tmpl points to the compied template
Tmpl *raymond.Template `json:"-" yaml:"-"`
// BaseDir is used by holds the "views" directory.
BaseDir string `json:"-" yaml:"-"`
// ExtName is used by set the expected extension (e.g. ".hbs")
ExtName string `json:"-" yaml:"-"`
// Partials holds partials directory
PartialsDir string `json:"-" yaml:"-"`
}
Template hold the request to template mapping for in the TemplateEngine
func (*Template) Check ¶ added in v0.0.9
Check evaluates the *Template and outputs finding. Returns true of no error, false if errors found
func (*Template) Handler ¶ added in v0.0.9
func (t *Template) Handler(w http.ResponseWriter, r *http.Request)
Handler decodes a the request body and then processes that as a template engine.
func (*Template) ResolvePath ¶ added in v0.0.9
ResolvePath reviews the `.Request` attribute and updates the Vars using PatternKeys()
func (*Template) ResolveTemplate ¶ added in v0.0.9
ResolvesTemplate is responsible for reading and parse the template and partials associated with a mapped request. If an error is encountered a error value is returned.
type TemplateEngine ¶ added in v0.0.9
type TemplateEngine struct {
// Port is the name of the localhost port Newt will listen on.
Port int `json:"port,omitempty" yaml:"port,omitempty"`
// BaseDir is holds the "views" for that are formed from the templates.
BaseDir string `json:"base_dir,omitempty" yaml:"base_dir,omitempty"`
// ExtName is used to set the expected extension (e.g. ".hbs")
ExtName string `json:"ext_name,omitempty" yaml:"ext_name,omitempty"`
// PartialsDir is used to find partial templates, usually inside the views directory
PartialsDir string `json:"partials_dir,omitempty" yaml:"partials_dir,omitempty"`
// Timeout is a duration, it is used to set timeouts and the application.
Timeout time.Duration `json:"timeout,omitempty" yaml:"timeout,omitempty"`
// Templates defined for the service
Templates []*Template `json:"templates,omitempty" yaml:"templates,omitempty"`
}
TemplateEngine defines the `nte` application YAML file. It joins some of the Service struct with an array of templates so that "check" can validate the YAML.
func NewTemplateEngine ¶ added in v0.0.9
func NewTemplateEngine(ast *AST) (*TemplateEngine, error)
NewTemplateEngine create a new TemplateEngine struct. If a filename is provided it reads the file and sets things up accordingly.
func (*TemplateEngine) Check ¶ added in v0.0.9
func (tEng *TemplateEngine) Check(buf io.Writer) bool
Check makes sure the TemplateEngine struct is populated
func (*TemplateEngine) ListenAndServe ¶ added in v0.0.9
func (te *TemplateEngine) ListenAndServe() error