Categorygithub.com/dranih/go-crud-api
module
0.0.3-beta
Repository: https://github.com/dranih/go-crud-api.git
Documentation: pkg.go.dev

# README

GO-CRUD-API

Build Status

:warning: Work in progress :warning:

Adding a REST API to a MySQL/MariaDB, PostgreSQL, SQL Server or SQLite database.

This is an attempt to port php-crud-api to golang.
Therefore, parts of this documentation refers to or copies the orginal projet readme.

Installation

  • Download latest release from Github and start it
./gocrudapi
  • Or clone it and compile :
[email protected]:dranih/go-crud-api.git
cd go-crud-api/cmd/gocrudapi/
go install
./gocrudapi

Configuration

gocrudapi looks for a gcaconfig.yml config file in current dir then $HOME if not found.
The GCA_CONFIG_FILE environnement variable can also be set to the path of yaml configuration file.

Config file example :

server:
  https: true

api:
  driver: "sqlite"
  controllers: "records,columns,cache,openapi,geojson,status"
  address: "/tmp/gocrudtests.db"
  database: "go-crud-api"
  username: "go-crud-api"
  password: "go-crud-api"

  middlewares:
  - basicAuth:
    - mode: "optional"
    - realm: "GoCrudApi : Username and password required"
    - passwordFile: "../../test/test.pwd"

These are all the configuration options and their default value :

  • server block :

    OptionDescriptionDefault value
    addressAddress the web server will be listening to:http or :https
    httpStart the http server (boolean)true
    httpsStart the https server (boolean)false
    httpPortAddress the http server will be listening to (int)8080
    httpsPortAddress the https server will be listening to (int)8443
    HttpsCertFilePath to the PEM cert file for tlswill generate a self-signed certificate if https on
    HttpsKeyFilePath to the PEM key file for tlswill generate a self-signed certificate if https on
    gracefulTimeoutDuration in seconds the web server will try to gracefully stop (int)15
    writeTimeoutSee http.Server (int)15
    readTimeoutSee http.Server (int)15
    idleTimeoutSee http.Server (int)60
  • api block :

    OptionDescriptionDefault value
    drivermysql, pgsql, sqlsrv or sqlitemysql
    addressHostname (or filename) of the database serverlocalhost
    portTCP port of the database server (int)defaults to driver default
    usernameUsername of the user connecting to the databaseno default
    passwordUsername of the user connecting to the databaseno default
    databaseDatabase the connecting is made tono default
    tablesComma separated list of tables to publishdefaults to 'all'
    mappingList of table/column mappingsno mapping
    middlewaresList of middlewares to load (see Middlewares for configuration)cors
    controllersList of controllers to loadrecords,geojson,openapi,status
    customControllersNot implemented yetN/A
    openApiBaseOpenAPI info{"info": {"title": "GO-CRUD-API", "version": "0.0.1"}}
    cacheTypeTempFile, Redis, Memcache, Memcached or NoCacheTempFile
    cachePathPath/address of the cachedefaults to system's temp directory
    cacheTimeNumber of seconds the cache is valid (int)10
    debugShow errors in the "X-Exception" headers (boolean)false
    basePathNot implemented yetN/A

All configuration options are also available as environment variables. Write the config option with capitals, a "GCA_" prefix and underscores for word breakes, so for instance:

  • GCA_SERVER_HTTPS=true
  • GCA_SERVER_HTTPSPORT=443
  • GCA_API_DRIVER=mysql
  • GCA_API_ADDRESS=localhost
  • GCA_API_PORT=3306
  • GCA_API_DATABASE=php-crud-api
  • GCA_API_USERNAME=php-crud-api
  • GCA_API_PASSWORD=php-crud-api
  • GCA_API_DEBUG=1

The environment variables take precedence over the yaml file configuration.

Limitations

See php-crud-api#limitations.

In addition, this golang implementation has some more limitations :

  • Not actively used in production
  • Using gotemplate for handlers instead of pure php code.

Features

See php-crud-api#features.

Missing features : customControllers, basePath

API usage

See php-crud-api#treeql-a-pragmatic-graphql

Middlewares

See php-crud-api#middleware

One of the main drawback of GO-CRUD-API compared to PHP-CRUD-API is the use of gotemplate syntax for the user provided handlers which is more complex and less powerful than php (or go) code.

The middlewares options have to be configured in the yaml configuration file, ex :

api:
  middlewares:
 - basicAuth:
    - mode: "optional"
    - realm: "GoCrudApi : Username and password required"
    - passwordFile: "../../test/test.pwd"
  - json:
    - controllers: "records"
    - tables: "products"
    - columns: "properties"
  - xml:
  - cors:
  - validation:
    - handler: "{{ if and (eq .Column.GetName \"post_id\") (and (not (kindIs \"float64\" .Value)) (not (kindIs \"int\" .Value))) }}must be numeric{{ else }}true{{ end }}"

OpenAPI specification

See php-crud-api#openapi-specification

Cache

See php-crud-api#cache

Types

See php-crud-api#types

Errors

See php-crud-api#errors

Status

See php-crud-api#status

Tests

Functional tests from PHP-CRUD-API had been implemented in the apiserver package.

More unit tests are needed in all the packages.

The test folder contains the procedure and the configuration files used to launch the tests with all four kind of databases (mysql, pgsql, sqlite and sqlserver).

The build and test github action only starts tests with sqlite database at the moment.

Dependencies

Those go packages are used :

PackageUsage
github.com/gorilla/muxRouting requests
github.com/clbanning/mxj/v2XML decode/encode
github.com/gorilla/sessionsHandling sessions
github.com/spf13/viperReading configuration
github.com/mattn/go-sqlite3Sqlite database driver
github.com/denisenkom/go-mssqldbMssql database driver
github.com/lib/pqPostgresql database driver
github.com/go-sql-driver/mysqlMysql database driver
github.com/Masterminds/sprig/v3Adds functions to go template
github.com/carmo-evan/strtotimeParsing text to date
github.com/bradfitz/gomemcacheMemcache client library
github.com/go-redis/redis/v8Redis client library
github.com/patrickmn/go-cacheIn-memory cache (not maintained ?)

To-do

  • Fix bug with seq in renaming tables in sqlserver (records test 080_add_barcode_with_ip_address_A fail after columns tests)
  • Tests :
    • more unit tests
    • implement php-crud-api tests
  • Other drivers (only sqlite now)
  • Cache mecanism
  • Finishing controllers
  • Custom controller (compile extra go code at launch like https://github.com/benhoyt/prig ?)
  • Finishing middlewares
  • Add a github workflow
  • Add an alter table function for sqlite (create new table, copy data, drop old table)
  • Review packages structure
  • Logger options
  • https
  • Write a README
  • Comment code
  • :tada:

# Packages

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