-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
42 lines (31 loc) · 1.27 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package cfghttp /* import "gopkg.xa4b.com/git/cfghttp" */
import (
"fmt"
"net/http"
"github.com/go-chi/chi"
)
// Server holds the handlers, middleware and mux for
// requests that the git client can make via HTTP
type Server struct {
mux *chi.Mux
pathPrefix string
git GitServer
middlewares []func(http.Handler) http.Handler
}
// NewServer returns a new server object that can be used as a mux with the http.Handler
func NewServer(gs GitServer, opts ...ServerOption) http.Handler {
s := &Server{git: gs, mux: chi.NewRouter()}
for _, optFn := range opts {
optFn(s) // the GitServer object needs to be added before this... so some options can interact with it
}
s.mux.Use(s.middlewares...)
s.mux.Get(fmt.Sprintf("/info/refs"), s.InfoRefsHandler)
s.mux.Get(fmt.Sprintf("/{repoName}/info/refs"), s.InfoRefsHandler)
s.mux.Post(fmt.Sprintf("/git-receive-pack"), s.ReceivePackHandler)
s.mux.Post(fmt.Sprintf("/{repoName}/git-receive-pack"), s.ReceivePackHandler)
s.mux.Post(fmt.Sprintf("/git-upload-pack"), s.UploadPackHandler)
s.mux.Post(fmt.Sprintf("/{repoName}/git-upload-pack"), s.UploadPackHandler)
return s
}
// ServeHTTP serves the internal muxer for the http handler
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { s.mux.ServeHTTP(w, r) }