ginServer

package module
v0.0.0-...-a499989 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2023 License: MIT Imports: 15 Imported by: 2

Documentation

Overview

Package ginServer contains the gin server and ginServer helper functions

Index

Examples

Constants

View Source
const StatusNotModified = 304 // RFC 7232, 4.1

StatusNotModified returns a 304 HTTP status code.

View Source
const TimeFormat = "Mon, 02 Jan 2006 15:04:05 GMT"

TImeFormat useful for formatting timestamps

Variables

View Source
var Router *gin.Engine

Router is the main gin.Engine that is used to serve all requests

Functions

func AddRouterGroup

func AddRouterGroup(group string, route string, method string, fp func(*gin.Context))

AddRouterGroup adds a gin-gonic router group to the gin server

func CheckLastModified

func CheckLastModified(w http.ResponseWriter, r *http.Request, modtime time.Time) bool

CheckLastModified modtime is the modification time of the resource to be served, or IsZero(). return value is whether this request is now complete.

func ClearSession

func ClearSession(c *gin.Context)

ClearSession clears the session of the client.

func ConfigureGin

func ConfigureGin(mode string, cookieDomain string, secureHeaders bool, allowedHosts []string, cookieSession SessionConfiguration, launchPprof bool)

ConfigureGin sets up all the GoCore features we currently offer

Example

Simple example using csrf security, session cookies, and a custom port.

package main

import (
	"github.com/DanielRenne/GoCore/core/ginServer"
)

func main() {
	cookieConfig := ginServer.SessionConfiguration{
		Enabled:               true,
		SessionKey:            "test",
		SessionName:           "test",
		SessionExpirationDays: 15,
		SessionSecureCookie:   false,
	}
	ginServer.ConfigureGin("debug", "", false, []string{}, cookieConfig, true)
	// Run this if you want to use the gin server after configuring it how you want
	// import "github.com/DanielRenne/GoCore/core/app"
	// app.RunLite(9090)
}
Output:

func GetRequestBody

func GetRequestBody(c *gin.Context) ([]byte, error)

GetRequestBody returns the body of the request as a string.

func GetSessionKey

func GetSessionKey(c *gin.Context, key string) (sessionKey string)

GetSessionKey returns a thread-safe value of the session key.

func Initialize

func Initialize(mode string, cookieDomain string)

Initialize is an internal export used in app/app.go based on your webConfig.json

func InitializeLite

func InitializeLite(mode string, secureHeaders bool, allowedHosts []string)

InitializeLite is an internal export used in app/app.go based on if you use app.InitializeLite()

func ReadFileBase64

func ReadFileBase64(path string, c *gin.Context)

ReadFileBase64 reads a file and responds with a base64 encoded string. Primarily used for jquery ajax response binary data blob encoding.

func ReadGzipCSSFile

func ReadGzipCSSFile(path string, c *gin.Context)

ReadGzipCSSFile reads a file from the path parameter and returns to the client as text/css.

func ReadGzipJSFile

func ReadGzipJSFile(path string, c *gin.Context)

ReadGzipJSFile reads a file from the path parameter and returns to the client as application/gzip.

func ReadHTMLFile

func ReadHTMLFile(path string, c *gin.Context)

ReadHTMLFile reads a file from the path parameter and returns to the client as text/html.

func ReadJSFile

func ReadJSFile(path string, c *gin.Context)

ReadJSFile reads a file from the path parameter and returns to the client as text/javascript.

func ReadJSONFile

func ReadJSONFile(path string, c *gin.Context)

ReadJSONFile reads a file from the path parameter and returns to the client application/json

func ReadJpgFile

func ReadJpgFile(path string, c *gin.Context)

ReadJpgFile reads a file from the path parameter and returns to the client as image/jpeg.

func ReadPngFile

func ReadPngFile(path string, c *gin.Context)

ReadPngFile reads a file from the path parameter and returns to the client as image/png.

func RenderHTML

func RenderHTML(html string, c *gin.Context)

RenderHTML takes a string and returns to the client as text/html.

func RespondCSSFile

func RespondCSSFile(data []byte, modTime time.Time, c *gin.Context)

RespondCSSFile returns a file to the client as text/css.

func RespondEotFile

func RespondEotFile(data []byte, modTime time.Time, c *gin.Context)

RespondEotFile returns a file to the client as application/vnd.ms-fontobject.

func RespondError

func RespondError(message string) []byte

RespondError returns an byte array comprised of a JSON formated object with the error message.

func RespondGzipCSSFile

func RespondGzipCSSFile(data []byte, modTime time.Time, c *gin.Context)

RespondGzipCSSFile returns a file to the client as text/css.

func RespondGzipJSFile

func RespondGzipJSFile(data []byte, modTime time.Time, c *gin.Context)

RespondGzipJSFile returns a file to the client as application/gzip.

func RespondJSFile

func RespondJSFile(data []byte, modTime time.Time, c *gin.Context)

RespondJSFile returns a file to the client as application/javascript.

func RespondJSON

func RespondJSON(v interface{}, c *gin.Context)

RespondJSON returns to the client application/json format for the passed interface.

func RespondOtfFile

func RespondOtfFile(data []byte, modTime time.Time, c *gin.Context)

RespondOtfFile returns a file to the client as application/x-font-opentype.

func RespondSvgFile

func RespondSvgFile(data []byte, modTime time.Time, c *gin.Context)

ResondSvgFile returns a file to the client as image/svg+xml.

func RespondTtfFile

func RespondTtfFile(data []byte, modTime time.Time, c *gin.Context)

ResponsdTtfFile returns a file to the client as application/x-font-ttf.

func RespondWoff2File

func RespondWoff2File(data []byte, modTime time.Time, c *gin.Context)

ResondWoff2File returns a file to the client as application/font-woff2.

func RespondWoffFile

func RespondWoffFile(data []byte, modTime time.Time, c *gin.Context)

ResondWoffFile returns a file to the client as application/font-woff.

func SaveSession

func SaveSession(c *gin.Context)

SaveSession saves the session of the client.

func SetSessionKey

func SetSessionKey(c *gin.Context, key string, value string)

SetSessionKey sets a thread-safe value of the session key.

Types

type ErrorResponse

type ErrorResponse struct {
	Message string `json:"message"`
}

ErrorResonse is a struct that is used to return errors to the client.

type LocaleLanguage

type LocaleLanguage struct {
	Locale   string
	Language string
}

LocaleLanguage is a struct that is used to return the language and locale of the client.

func GetLocaleLanguage

func GetLocaleLanguage(c *gin.Context) (ll LocaleLanguage)

GetLocaleLanguage returns the locale and language of the client.

type SessionConfiguration

type SessionConfiguration struct {
	Enabled               bool
	SessionKey            string
	SessionName           string
	SessionExpirationDays int
	SessionSecureCookie   bool
}

SessionConfiguration is used to configure the session cookie

Jump to

Keyboard shortcuts

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