goctapus

package module
v0.0.0-...-0193b58 Latest Latest
Warning

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

Go to latest
Published: May 31, 2018 License: Apache-2.0 Imports: 13 Imported by: 0

README

Gocatpus

Goctapus is a wrapper around Echo that makes it easier to build REST APIs and applications that are based on them.

For a sample application built on Goctapus, check Kamaropoulos/goctapus-example

There is also a fork (Kamaropoulos/goctapus-mongo) that uses MongoDB as the Database Backend but it is currently outdated and should be soon merged to this one.

This repository was originaly a fork of ezynda3/go-echo-vue in order to make it use MySQL as a database backend but it's purpose has changed a lot since then.

Getting started

Goctapus makes it really easy and fast to get started and build your own REST APIs.

All you have to do is:

  1. Install Go and setup MySQL if you haven't already,
  2. Clone or copy the files from Kamaropoulos/goctapus-blank-template
  3. Connect to a database.

    Goctapus will connect to the MySQL server using the information passed to the command line. You can open connections to one or multiple MySQL databases using ConnectDB(). For example:

    goctapus.ConnectDB("goapp")
    

    will connect to the databased named goapp on the MySQL server provided on the command line.

  4. Create your API endpoints:

    You can create endpoints using AddEndpoint(). For example:

    goctapus.AddEndpoint(goctapus.Route{
    	Method:  "GET",
    	Path:    "/tasks",
    	Handler: handlers.GetTasks(goctapus.Databases["goapp"]),
    	Rate:    10,
    }, myAwesomeMiddleware1(), myAwesomeMiddleware2())
    

    will add a GET endpoint on /tasks using the handler GetTasks() with a rate limit of 10 requests per second. It will also register two middlewares.

    You can also serve static files using AddStatic(). For example:

    goctapus.AddStatic(goctapus.Route{
    	Path: "/",
    	File: "public/index.html",
    })
    

    will serve the file index.html when the root path of the server is requested.

  5. Create your handlers and models

    Create your handler and model files on the handlers and models folders. For more information and examples you can refer to the Echo documentation and the Kamaropoulos/goctapus-example application.

  6. Run your application!

    To execute your application you can run:

    go run main.go [application_Port] [DB_Username] [DB_Password] [DB_Hostname/IP] [DB_PORT]
    

    where you'll have to replace:

    • [application_Port] with the port you want the application to listen to,
    • [DB_Username] with the username the application is going to connect to the Database with,
    • [DB_Password] with the password the application is going to connect to the Database with,
    • [DB_Hostname/IP] with the hostname or the IP the Database is running on,
    • [DB_Port] with the Port the Database is listening to.
To try the application, point your browser to localhost:[applicationPort]

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Databases map[string]*sql.DB
View Source
var Routes map[string]Route
View Source
var Server *echo.Echo

Functions

func AddEndpoint

func AddEndpoint(routeInfo Route, m ...echo.MiddlewareFunc)

func AddStatic

func AddStatic(routeInfo Route, m ...echo.MiddlewareFunc)

func ConnectDB

func ConnectDB(db_name string)

ConnectDB connects to the database server, creates the passed DB name if it doesn't exists and sets it as the current DB. It then stores the DB object into the public Databases object for later use.

func DELETE

func DELETE(routeInfo Route, m ...echo.MiddlewareFunc)

func GET

func GET(routeInfo Route, m ...echo.MiddlewareFunc)

func Init

func Init(args []string, logLevel string)

func InitDB

func InitDB(dbString string) (*sql.DB, error)

InitDB initializes the connection to the Database

func InitLogger

func InitLogger(logLevel string)

func Migrate

func Migrate(db *sql.DB, filename string)

Migrate executes an SQL File against a specific DB

func POST

func POST(routeInfo Route, m ...echo.MiddlewareFunc)

func PUT

func PUT(routeInfo Route, m ...echo.MiddlewareFunc)

func SetLogLevel

func SetLogLevel(level string)

func Start

func Start()

Types

type Configuration

type Configuration struct {
	// contains filtered or unexported fields
}
var Config Configuration

type Route

type Route struct {
	Method  string
	Path    string
	Handler echo.HandlerFunc
	File    string
	Rate    float64
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

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