Documentation
¶
Overview ¶
Package server contains all the needed parsing creating and managing an HTTP listening server adapted for githook needs
Index ¶
- func CommandLogRESTHandler(cmdLog CommandLog) func(http.ResponseWriter, *http.Request)
- func CommandWorker(id string, jobs <-chan CommandJob, cmdLog CommandLog) (executed int)
- func HelloHandler(w http.ResponseWriter, req *http.Request)
- func JSONRequestMiddleware(h http.HandlerFunc) http.HandlerFunc
- func RepoRequestHandler(cmdLog CommandLog, workerChannel chan CommandJob, hookName string, ...) func(http.ResponseWriter, *http.Request)
- func TranslateParams(cmd []string, event event.RepoEvent) (trCmd []string, err error)
- type CommandJob
- type CommandLog
- type CommandResult
- type DiskCommandLog
- type Hook
- type MemoryCommandLog
- type Response
- type Server
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CommandLogRESTHandler ¶
func CommandLogRESTHandler(cmdLog CommandLog) func(http.ResponseWriter, *http.Request)
CommandLogRESTHandler Returns the command log via REST request
func CommandWorker ¶
func CommandWorker(id string, jobs <-chan CommandJob, cmdLog CommandLog) (executed int)
CommandWorker runs command receiving from jobs channel, it also stores the command execution result into a CommandLog interface
func HelloHandler ¶
func HelloHandler(w http.ResponseWriter, req *http.Request)
HelloHandler implements a basic HTTP Handler that returns a HelloWorld-like response for testing or debugging purposes
func JSONRequestMiddleware ¶
func JSONRequestMiddleware(h http.HandlerFunc) http.HandlerFunc
JSONRequestMiddleware implements an http.HandlerFunc middleware that sets the HTTP Content-Type header and prints a log line when the request is received and completed
func RepoRequestHandler ¶
func RepoRequestHandler(cmdLog CommandLog, workerChannel chan CommandJob, hookName string, hookInfo Hook) func(http.ResponseWriter, *http.Request)
RepoRequestHandler setups an http.HandlerFunc using Hook information This function makes the hard work of setting up a listener hook on the HTTP Server based on an Hook structure
func TranslateParams ¶
TranslateParams translates a list of command parameters (from Hook) based on the event received at event.RepoEvent. It uses Go's built-in templating (text/template) so all operations on templates can be performed on the command parameters. I.e.: `cmd := ["{{.Branch}}", "is", "the", "branch"]` with event.Branch := "develop" will be transformed to `cmd := ["develop", "is", "the", "branch"]` It returns the translated array of strings and error in case of error
Types ¶
type CommandJob ¶
type CommandJob struct { Cmd []string ID string Timeout int Response chan CommandResult }
CommandJob encodes a request to execute a command
type CommandLog ¶
type CommandLog interface { // AppendResult appends a CommandResult to the underlying CommandLog storage AppendResult(result CommandResult) (deleted int, err error) // GetResults returns at most the latest n CommandResult stored in the underlying storage // sorted from latest to older, if n < 0 it returns all the CommandResult stored GetResults(n int) (results []CommandResult, err error) // RotateResults rotates the older CommandResult stored in the underlying storage so // only MaxCommands CommandResult are left in the underlying storage, it returns the number rotated results // i.e.: the deleted ones RotateResults() (deleted int, err error) // Counts counts the number of CommandResult stored in the underlying storage Count() (count int, err error) }
CommandLog is the interface that must be implemented by command loggers
type CommandResult ¶
type CommandResult struct { Cmd []string `json:"cmd"` Err error `json:"err"` Stdout []byte `json:"stdout"` Stderr []byte `json:"stderr"` }
CommandResult stores the result of a command execution
func RunCommand ¶
func RunCommand(cmd []string, timeout int) (result CommandResult)
RunCommand executes the hook command on the system, it takes an array of string representing the command to be returned, a timeout in seconds and a channel for returning the data. It returns an instance of CommandResult
type DiskCommandLog ¶
DiskCommandLog implements the CommandLog interface storing the results in disk
func NewDiskCommandLog ¶
func NewDiskCommandLog(location string, rotate int) *DiskCommandLog
NewDiskCommandLog creates and object of type DiskCommandLog
func (*DiskCommandLog) AppendResult ¶
func (d *DiskCommandLog) AppendResult(result CommandResult) (deleted int, err error)
AppendResult of DiskCommandLog
func (*DiskCommandLog) Count ¶
func (d *DiskCommandLog) Count() (count int, err error)
Count of DiskCommandLog
func (*DiskCommandLog) GetResults ¶
func (d *DiskCommandLog) GetResults(n int) (results []CommandResult, err error)
GetResults of DiskCommandLog
func (*DiskCommandLog) RotateResults ¶
func (d *DiskCommandLog) RotateResults() (deleted int, err error)
RotateResults of DiskCommandLog
type Hook ¶
Hook structure holds all the information needed to configure an HTTP endpoint and execute the custom command on the system Type refers to the repository provider, it can be github, bitbucket or gitlab Path refers to the HTTP path where the HTTP server will be listening. I.e.: /mycustompayloadlistener Timeout specifies the number of seconds to wait for the custom command to be completed before killing it Cmd is the custom command to be executed each time an HTTP request is received It is a list of strings and each element will be an argument to exec.Command this implies that any kind of redirection using a shell won't work and will be treated as another parameter to exec function. I.e.: `Cmd: ["echo", "I want this in STDERR", "1>&2"]` will actually print "I want this in STDERR 1>&2" and won't print `"I want this in STDERR"` to `/dev/stderr` While this is less flexible from an UNIX shell perspective, it makes easier to run in different OS that don't has the same UNIX shell behaviour. It is also safer as remote data from git webhook provider won't be treated as part of a shell command but will be treated as part of a shell-command parameter Concurrency determines the number of concurrent workers that will be available to run command a concurrency level of 1 means that only 1 command can be executed at a time (mutex mode), default is 1
type MemoryCommandLog ¶
type MemoryCommandLog struct { MaxCommands int CommandLog []CommandResult }
MemoryCommandLog implements the CommandLog interface storing the results in memory
func NewMemoryCommandLog ¶
func NewMemoryCommandLog(rotate int) *MemoryCommandLog
NewMemoryCommandLog creates and object of type MemoryCommandLog
func (*MemoryCommandLog) AppendResult ¶
func (m *MemoryCommandLog) AppendResult(result CommandResult) (deleted int, err error)
AppendResult of MemoryCommandLog
func (*MemoryCommandLog) Count ¶
func (m *MemoryCommandLog) Count() (count int, err error)
Count of MemoryCommandLog
func (*MemoryCommandLog) GetResults ¶
func (m *MemoryCommandLog) GetResults(n int) (results []CommandResult, err error)
GetResults of MemoryCommandLog
func (*MemoryCommandLog) RotateResults ¶
func (m *MemoryCommandLog) RotateResults() (deleted int, err error)
RotateResults of MemoryCommandLog
type Response ¶
type Response struct { Status int `json:"status"` Msg string `json:"msg"` Body interface{} `json:"body,omitempty"` }
Response represents a server request response
type Server ¶
type Server struct { *http.Server TLSCert string TLSKey string CmdLogDir string CmdLogLimit int WorkerChannelSize int Hooks map[string]Hook MuxHandler *http.ServeMux HooksHandled map[string]int WorkerChannels map[string]chan CommandJob CmdLog CommandLog }
Server an http.Server all the needed information for starting and running the http server
func (*Server) ListenAndServe ¶
ListenAndServe set ups everything needed for the server to run and calls underlying http.Server ListenAndServer depending on Server is set up to use TLS or not