HttpApi package - github.com/abaldeweg/services/packages/http_api - Go Packages

HttpApi

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2026 License: MIT Imports: 6 Imported by: 0

README

http_api

http_api offers a simple abstraction around existing tools to run an HTTP API server, with router and CORS.

Usage

Always set MODE=release in production environments for best performance and security.

You can set the Gin mode ("release", "debug", "test") via the environment variable MODE. If not set, it defaults to debug.

All origins are allowed by default. You should restrict allowed origins in production via the environment variable CORS_ALLOW_ORIGIN, providing a comma-separated list of allowed origins.

package main

import (
    "log"

    "github.com/abaldeweg/services/packages/http_api"
)

func main() {
    api := http_api.NewHttpApi()
    // Define your routes here
    controller := api.Controller("/api")
    controller.Get("/test", func(c *gin.Context) {
        c.JSON(http.StatusOK, gin.H{"message": "Test"})
    })
    log.Fatal(api.Run())
}

Env Vars

Var Defaults Values Description
Mode debug "release", "debug", "test" Gin mode
PORT 8080 int Port to run the server on
CORS_ALLOW_ORIGIN "" string Allowed origins

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetEnvCorsAllowOrigin

func SetEnvCorsAllowOrigin(name string)

SetEnvCorsAllowOrigin sets the environment variable name for env_name_cors_allow_origin.

func SetEnvMode

func SetEnvMode(name string)

SetEnvMode sets the environment variable name for env_name_mode.

func SetEnvPort

func SetEnvPort(name string)

SetEnvPort sets the environment variable name for env_name_port.

Types

type HttpApi

type HttpApi struct {
	// contains filtered or unexported fields
}

HttpApi wraps a gin.Engine and manages the server port.

func (*HttpApi) Controller

func (r *HttpApi) Controller(path string, handlers ...gin.HandlerFunc) gin.IRoutes

Controller creates a new route group with the given relative path and handlers.

func (*HttpApi) Delete

func (r *HttpApi) Delete(path string, handlers ...gin.HandlerFunc) gin.IRoutes

Delete registers a new DELETE route.

func (*HttpApi) Get

func (r *HttpApi) Get(path string, handlers ...gin.HandlerFunc) gin.IRoutes

Get registers a new GET route.

func (*HttpApi) Head

func (r *HttpApi) Head(path string, handlers ...gin.HandlerFunc) gin.IRoutes

Head registers a new HEAD route.

func (*HttpApi) Options

func (r *HttpApi) Options(path string, handlers ...gin.HandlerFunc) gin.IRoutes

Options registers a new OPTIONS route.

func (*HttpApi) Patch

func (r *HttpApi) Patch(path string, handlers ...gin.HandlerFunc) gin.IRoutes

Patch registers a new PATCH route.

func (*HttpApi) Post

func (r *HttpApi) Post(path string, handlers ...gin.HandlerFunc) gin.IRoutes

Post registers a new POST route.

func (*HttpApi) Put

func (r *HttpApi) Put(path string, handlers ...gin.HandlerFunc) gin.IRoutes

Put registers a new PUT route.

func (*HttpApi) Request

func (r *HttpApi) Request(method, path string, handlers ...gin.HandlerFunc) gin.IRoutes

Handle registers a new route with the given method.

func (*HttpApi) Run

func (r *HttpApi) Run() error

Run starts the HTTP server.

type HttpApiInterface

type HttpApiInterface interface {
	Run() error

	Request(method, path string, handlers ...gin.HandlerFunc) gin.IRoutes
	Controller(path string, handlers ...gin.HandlerFunc) gin.IRoutes
	Get(path string, handlers ...gin.HandlerFunc) gin.IRoutes
	Post(path string, handlers ...gin.HandlerFunc) gin.IRoutes
	Delete(path string, handlers ...gin.HandlerFunc) gin.IRoutes
	Patch(path string, handlers ...gin.HandlerFunc) gin.IRoutes
	Put(path string, handlers ...gin.HandlerFunc) gin.IRoutes
	Options(path string, handlers ...gin.HandlerFunc) gin.IRoutes
	Head(path string, handlers ...gin.HandlerFunc) gin.IRoutes
}

HttpApiInterface defines the public methods for HttpApi.

func NewHttpApi

func NewHttpApi() HttpApiInterface

NewHttpApi creates a new HttpApi with engine.

Jump to

Keyboard shortcuts

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