Categorygithub.com/zeuce/golang-api
modulepackage
1.0.1
Repository: https://github.com/zeuce/golang-api.git
Documentation: pkg.go.dev

# README

GoLang API

A GoLang library to help to help facilitate RESTful API creation

Installation
Usage

Installation

RUN:

go get github.com/zeuce/grapi

Usage

Setup Server

HTTP:

package main

import (
    "github.com/zeuce/grapi"
)

func main() {
    //Start's a HTTP server on port 3000
    grapi.SetupServer(3000)
}

HTTPS:

package main

import (
    "github.com/zeuce/grapi"
)

func main() {
    //Start's a HTTPS server on port 3000
    grapi.SetupServerSSL(3000,"/path/to/certfile", "/path/to/keyfile")
}

Setup Logging

package main

import (
    "github.com/zeuce/grapi"
)

func main() {
    //Setup logging support
    grapi.SetupLogging("path/to/logDir", "<LOGFILENAME>", "<PREFIX>")
}

Route Handling

GET:

package main

import (
    "github.com/zeuce/grapi"
    "encoding/json"
)

func handlerFunc(w http.ResponseWriter, r *http.Request) {
    d := grapi.ResponseStruct {
        StatusCode: 200,
        Message: "Yay its working"
    }
    json.NewEncoder(w).Encode(d)
}

func main() {
    //Setup GET for /
    grapi.Get("/", handlerFunc)
}

POST:

package main

import (
    "github.com/zeuce/grapi"
    "encoding/json"
)

func handlerFunc(w http.ResponseWriter, r *http.Request) {
    d := grapi.ResponseStruct {
        StatusCode: 200,
        Message: "Yay its working"
    }
    json.NewEncoder(w).Encode(d)
}

func main() {
    //Setup POST for /
    grapi.Post("/", handlerFunc)
}

DELETE:

package main

import (
    "github.com/zeuce/grapi"
    "encoding/json"
)

func handlerFunc(w http.ResponseWriter, r *http.Request) {
    d := grapi.ResponseStruct {
        StatusCode: 200,
        Message: "Yay its working"
    }
    json.NewEncoder(w).Encode(d)
}

func main() {
    //Setup DELETE for /
    grapi.Delete("/", handlerFunc)
}

PATCH:

package main

import (
    "github.com/zeuce/grapi"
    "encoding/json"
)

func handlerFunc(w http.ResponseWriter, r *http.Request) {
    d := grapi.ResponseStruct {
        StatusCode: 200,
        Message: "Yay its working"
    }
    json.NewEncoder(w).Encode(d)
}

func main() {
    //Setup PATCH for /
    grapi.Patch("/", handlerFunc)
}

Adding Global Headers

package main

import (
    "github.com/zeuce/grapi"
)

func main() {
    //Add global header
    grapi.AddDefaultHeader("key", "val")
    //Adding multiple global headers
    grapi.AddDefaultHeaders([]grapi.Header{
        grapi.Header {
            Key: "foo",
            Value: "bar",
        },
        grapi.Heder {
            Key: "foo1",
            Value: "bar1",
        },
    })
}

# Functions

AddDefaultHeader will add the header given in every request @param key - The key value of header @param value - The value of the key.
AddDefaultHeaders will add headers with given list @param headers - A list of type Header.
Delete will setup a DELETE handle function @param path - The path the server will check on request @param f - A function to tell it what to do on request.
Get will setup a GET handle function @param path - The path the server will check on request @param f - A function to tell it what to do on request.
Patch will setup a PATCH handle function @param path - The path the server will check on request @param f - A function to tell it what to do on request.
Post
Post will setup a POST handle function @param path - The path the server will check on request @param f - A function to tell it what to do on request.
SetupLogging will log add logHandler() to router and create log file with supplied args @param logDir - The directory to save your log file to @param logFile - The log file name @param logPrefix - The prefix displayed in console / log file.
SetupServer will start a HTTP server on port given @param router - Your mux router @param port - An int value in range 1 - 65535.
SetupServerSSL will start a HTTPS server on port given @param port - An int value in range 1 - 65535 @param certFile - The path to your cert file @param keyFile - The path to your key file.

# Variables

Router for internal use only.

# Structs

Header header struct for adding headers.
ResponseStruct is a struct for to help prevent code duplication.