Categorygithub.com/snowzach/gorestapi
modulepackage
1.1.0
Repository: https://github.com/snowzach/gorestapi.git
Documentation: pkg.go.dev

# README

Base API Example

This API example is a basic framework for a REST API

Compiling

This is designed as a go module aware program and thus requires go 1.11 or better You can clone it anywhere, just run make inside the cloned directory to build

Requirements

This does require a postgres database to be setup and reachable. It will attempt to create and migrate the database upon starting.

Configuration

The configuration is designed to be specified with environment variables in all caps with underscores instead of periods.

Example:
LOGGER_LEVEL=debug

Options:

SettingDescriptionDefault
logger.levelThe default logging level"info"
logger.encodingLogging format (console, json or stackdriver)"console"
logger.colorEnable color in console modetrue
logger.dev_modeDump additional information as part of log messagestrue
logger.disable_callerHide the caller source file and line numberfalse
logger.disable_stacktraceHide a stacktrace on debug logstrue
---------
metrics.enabledEnable metrics servertrue
metrics.hostHost/IP to listen on for metrics server""
metrics.portPort to listen on for metrics server6060
profiler.enabledEnable go profiler on metrics server under /debug/pprof/true
pidfileIf set, creates a pidfile at the given path""
---------
server.hostThe host address to listen on (blank=all addresses)""
server.portThe port number to listen on8900
server.tlsEnable https/tlsfalse
server.devcertGenerate a development certfalse
server.certfileThe HTTPS/TLS server certificate"server.crt"
server.keyfileThe HTTPS/TLS server key file"server.key"
server.log.enabledLog server requeststrue
server.log.levelLog level for server requests"info
server.log.request_bodyLog the request bodyfalse
server.log.response_bodyLog the response bodyfalse
server.log.ignore_pathsThe endpoint prefixes to not log[]string{"/version"}
server.cors.enabledEnable CORS middlewarefalse
server.cors.allowed_originsCORS Allowed origins[]string{"*"}
server.cors.allowed_methodsCORS Allowed methods[]string{...everything}
server.cors.allowed_headersCORS Allowed headers[]string{"*"}
server.cors.allowed_credentialsCORS Allowed credentialsfalse
server.cors.max_ageCORS Max Age300
server.metrics.enabledEnable metrics on server endpointstrue
server.metrics.ignore_pathsThe endpoint prefixes to not capture metrics on[]string{"/version"}
---------
database.usernameThe database username"postgres"
database.passwordThe database password"password"
database.hostThos hostname for the database"postgres"
database.portThe port for the database5432
database.databaseThe database"gorestapi"
database.auto_createAutomatically create databasetrue
database.search_pathSet the search path""
database.sslmodeThe postgres sslmode to use"disable"
database.sslcertThe postgres sslcert file""
database.sslkeyThe postgres sslkey file""
database.sslrootcertThe postgres sslrootcert file""
database.retriesHow many times to try to reconnect to the database on start7
database.sleep_between_retriesHow long to sleep between retries"7s"
database.max_connectionsHow many pooled connections to have40
database.loq_queriesLog queries (must set logging.level=debug)false
database.wipe_confirmWipe the database during startfalse

Data Storage

Data is stored in a postgres database by default.

Query Logic

Find requests GET /api/things and GET /api/widgets uses a url query parser to allow very complex logic including AND, OR and precedence operators. For the documentation on how to use this format see https://github.com/snowzach/queryp

Swagger Documentation

When you run the API it has built in Swagger documentation available at /api/api-docs/ (trailing slash required) The documentation is automatically generated.

TLS/HTTPS

You can enable https by setting the config option server.tls = true and pointing it to your keyfile and certfile. To create a self-signed cert: openssl req -new -newkey rsa:2048 -days 3650 -nodes -x509 -keyout server.key -out server.crt It also has the option to automatically generate a development cert every time it runs using the server.devcert option.

Relocation

If you want to start with this as boilerplate for your project, you can clone this repo and use the make relocate option to rename the package. make relocate TARGET=github.com/myname/mycoolproject

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author