githttp

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

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

Go to latest
Published: Jul 22, 2018 License: Apache-2.0 Imports: 17 Imported by: 1

README

go-git-http

Build Status

A Smart Git Http server library in Go (golang)

Example
package main

import (
    "log"
    "net/http"

    "github.com/AaronO/go-git-http"
)

func main() {
    // Get git handler to serve a directory of repos
    git := githttp.New("/Users/aaron/git")

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

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

import (
    "log"
    "net/http"

    "github.com/AaronO/go-git-http"
    "github.com/AaronO/go-git-http/auth"
)


func main() {
    // Get git handler to serve a directory of repos
    git := githttp.New("/Users/aaron/git")

    // 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.0.0"

Variables

This section is empty.

Functions

func RunCommand

func RunCommand(command string, args ...string) (string, string, error)

func RunCommandMust

func RunCommandMust(command string, args ...string) (string, string)

func RunCommandWithWd

func RunCommandWithWd(dir, command string, args ...string) (string, string, error)

Types

type ErrorNoAccess

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

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 struct {
	// Root directory to serve repos from
	ProjectRoot string

	// Git service path prefix
	UrlPrefix string

	// Path to git binary
	GitBinPath string

	// Access rules
	UploadPack  bool
	ReceivePack bool

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

func New

func New(root, urlPrefix string) *GitHttp

Shorthand constructor for most common scenario

func (*GitHttp) Init

func (g *GitHttp) Init() (*GitHttp, error)

Build root directory if doesn't exist

func (*GitHttp) ServeHTTP

func (g *GitHttp) ServeHTTP(w http.ResponseWriter, r *http.Request)

Implement the http.Handler interface

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 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