githttp

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2018 License: Apache-2.0 Imports: 14 Imported by: 0

README

githttp

Build Status Go Report Card Codacy Badge

A Smart Git Http server library in Go (golang)

Example
package main

import (
    "log"
    "net/http"
    "crypto/sha1"
    "github.com/gofunky/githttp"
)

func main() {
    // Get git handler to serve a directory of repos
    git, err := githttp.NewGitContext(githttp.GitOptions{
    	ProjectRoot: "my/repos",
    	AutoCreate: true,
    	ReceivePack: true,
    	UploadPack: true,
    	EventHandler: func(ev githttp.Event) {
    	    if ev.Error != nil {
    	    	log.Fatal(ev)
    	    }
    	},
    	Prep: func() githttp.Preprocesser {
    		return githttp.Preprocesser{
            Process:func(params *githttp.ProcessParams) error {
            	if params.IsNew {
            		// E.g., generate .gitignore file
            	}
            	return nil
    		},
            Path:func(rawPath string) (targetPath string, err error) {
            	// Lets hash the string
            	h := sha1.New()
            	h.Write([]byte(rawPath))
            	// Resulting target path will be "./my/repos/{hash(rawPath)}/
            	return string(h.Sum(nil)), nil
            },
    	}},
    })
    // Panic if the server context couldn't be created
    if err != nil {
    	panic(err)
    }

    // Attach handler to http server
    http.Handle("/", git)

    // Start HTTP server
    err = http.ListenAndServe(":8080", nil)
    if err != nil {
        panic(err)
    }
}
Authentication example
package main

import (
    "log"
    "net/http"

    "github.com/gofunky/githttp"
    "github.com/gofunky/githttp/auth"
)


func main() {
    // Get git handler to serve a directory of repos
    git, err := githttp.NewGitContext(githttp.GitOptions{
    	ProjectRoot: "my/repos",
    	ReceivePack: true,
    	UploadPack: true,
    })
    // Panic if the server context couldn't be created
    if err != nil {
    	panic(err)
    }

    // Build an authentication middleware based on a function
    authenticator := auth.Authenticator(func(info auth.AuthInfo) (bool, error) {
        // Disallow Pushes (making git server pull only)
        if info.Push {
            return false, nil
        }

        // Typically this would be a database lookup
        if info.Username == "admin" && info.Password == "password" {
            return true, nil
        }

        return false, nil
    })

    // Attach handler to http server
    // wrap authenticator around git handler
    http.Handle("/", authenticator(git))

    // Start HTTP server
    err = http.ListenAndServe(":8080", nil)
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}

Documentation

Index

Constants

View Source
const (
	TAG = iota + 1
	PUSH
	FETCH
	PUSH_FORCE
)

Possible event types

View Source
const VERSION = "1.3.0"

VERSION shows the git server version.

Variables

View Source
var ErrMissingArgument = errors.New("insufficient factory options options provided")

ErrMissingArgument is to be returned if there are git server options missing that are passed to the factory.

Functions

This section is empty.

Types

type ErrorNoAccess

type ErrorNoAccess struct {
	// Path to directory of repo accessed
	Dir string
}

ErrorNoAccess is a error with the path to the requested repository

func (*ErrorNoAccess) Error

func (e *ErrorNoAccess) Error() string

type Event

type Event struct {
	// One of tag/push/fetch
	Type EventType `json:"type"`

	// SHA of commit
	Commit string `json:"commit"`

	// Path to bare repo
	Dir string

	// //
	// Set for pushes or tagging
	// //
	Tag    string `json:"tag,omitempty"`
	Last   string `json:"last,omitempty"`
	Branch string `json:"branch,omitempty"`

	// Error contains the error that happened (if any)
	// during this action/event
	Error error

	// Http stuff
	Request *http.Request
}

An event (triggered on push/pull)

type EventType

type EventType int

func (EventType) MarshalJSON

func (e EventType) MarshalJSON() ([]byte, error)

func (EventType) String

func (e EventType) String() string

func (EventType) UnmarshalJSON

func (e EventType) UnmarshalJSON(data []byte) error

type GitHTTP

type GitHTTP interface {
	Init() (*gitContext, error)
	ServeHTTP(w http.ResponseWriter, r *http.Request)
}

GitHTTP exposes the interfaces of the git server.

func NewGitContext

func NewGitContext(options GitOptions) (context GitHTTP, err error)

NewGitContext is the factory for the git server.

type GitOptions

type GitOptions struct {
	// Root directory to serve repos from
	ProjectRoot string

	// Path to git binary
	GitBinPath string

	// Access rules
	UploadPack  bool
	ReceivePack bool

	// Implicit generation
	AutoCreate bool

	// May be used to create a common context for the preprocessing funcs
	Prep func() Preprocesser

	// Event handling functions
	EventHandler func(ev Event)
}

GitOptions contains the the git server options.

type GitReader

type GitReader struct {
	// Underlying reader (to relay calls to)
	io.Reader

	// Error
	GitError error
}

GitReader scans for errors in the output of a git command

func (*GitReader) Read

func (g *GitReader) Read(p []byte) (n int, err error)

Implement the io.Reader interface

type HandlerReq

type HandlerReq struct {
	Rpc  string
	Dir  string
	File string
	// contains filtered or unexported fields
}

type Preprocesser added in v1.2.0

type Preprocesser struct {
	// Process updates the code.
	Process func(params *ProcessParams) error
	// Path checks if the requested uri is valid and returns a deterministic local repository path.
	Path func(rawPath string) (targetPath string, err error)
}

Preprocesser is called on every git request.

func (*Preprocesser) IsPathNil added in v1.3.0

func (t *Preprocesser) IsPathNil() bool

IsPathNil returns true if a the Preprocesser struct or the IsPathNil func is nil.

func (*Preprocesser) IsProcessNil added in v1.3.0

func (t *Preprocesser) IsProcessNil() bool

IsProcessNil returns true if a the Preprocesser struct or the Process func is nil.

type ProcessParams

type ProcessParams struct {
	// The public path to the git repository
	Repository string
	// Local path of the git repository where the files are located
	LocalPath string
	// If the repository has just been created
	IsNew bool
}

ProcessParams contain the preprocessing parameters.

type RpcReader

type RpcReader struct {
	// Underlying reader (to relay calls to).
	io.Reader

	// RPC type (receive-pack or upload-pack).
	Rpc string

	// List of events RpcReader has picked up through scanning.
	// These events do not have the Dir field set.
	Events []Event
	// contains filtered or unexported fields
}

RpcReader scans for events in the incoming rpc request data.

func (*RpcReader) Read

func (r *RpcReader) Read(p []byte) (n int, err error)

Read implements the io.Reader interface.

type Service

type Service struct {
	Method  string
	Handler func(HandlerReq) error
	RPC     string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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