-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add gogit-http-server command #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+138
−29
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
10eb84f
feat: add gogit-http-server command
aymanbagabas d8ee5da
feat(http-server): add logging middleware
aymanbagabas 1159863
feat(http-server): add prefix server option
aymanbagabas cbe17a4
chore(http-server): use the new api
aymanbagabas 8406ae4
chore: update go-git/go-git to v6-transport
aymanbagabas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type logWriter struct { | ||
http.ResponseWriter | ||
code, bytes int | ||
} | ||
|
||
func (r *logWriter) Write(p []byte) (int, error) { | ||
written, err := r.ResponseWriter.Write(p) | ||
r.bytes += written | ||
return written, err | ||
} | ||
|
||
// Note this is generally only called when sending an HTTP error, so it's | ||
// important to set the `code` value to 200 as a default | ||
func (r *logWriter) WriteHeader(code int) { | ||
r.code = code | ||
r.ResponseWriter.WriteHeader(code) | ||
} | ||
|
||
// LoggingMiddleware is the logging middleware where we log incoming and | ||
// outgoing requests for a multiplexer. It should be the first middleware | ||
// called so it can log request times accurately. | ||
func LoggingMiddleware(logger *log.Logger, next http.Handler) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
addr := r.RemoteAddr | ||
if colon := strings.LastIndex(addr, ":"); colon != -1 { | ||
addr = addr[:colon] | ||
} | ||
|
||
writer := &logWriter{ | ||
ResponseWriter: w, | ||
code: http.StatusOK, // default. so important! see above. | ||
} | ||
|
||
startTime := time.Now() | ||
|
||
next.ServeHTTP(writer, r) | ||
|
||
elapsedTime := time.Since(startTime) | ||
logger.Printf("%s %s %s %s %d %dB %v", addr, r.Method, r.RequestURI, r.Proto, writer.code, writer.bytes, elapsedTime) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"path/filepath" | ||
|
||
"github.com/go-git/go-billy/v5/osfs" | ||
githttp "github.com/go-git/go-git/v6/backend/http" | ||
"github.com/go-git/go-git/v6/plumbing/transport" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
port int | ||
prefix string | ||
) | ||
|
||
func init() { | ||
rootCmd.Flags().IntVarP(&port, "port", "p", 8080, "Port to run the HTTP server on") | ||
rootCmd.Flags().StringVarP(&prefix, "prefix", "", "", "Prefix for the HTTP server routes") | ||
} | ||
|
||
var rootCmd = &cobra.Command{ | ||
Use: "gogit-http-server [options] <directory>", | ||
Short: "Run a Go Git HTTP server", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
directory := args[0] | ||
addr := fmt.Sprintf(":%d", port) | ||
abs, err := filepath.Abs(directory) | ||
if err != nil { | ||
return fmt.Errorf("failed to get absolute path: %w", err) | ||
} | ||
|
||
log.Printf("Using absolute path: %q", abs) | ||
logger := log.Default() | ||
loader := transport.NewFilesystemLoader(osfs.New(abs, osfs.WithBoundOS()), false) | ||
gitmw := githttp.NewBackend(loader, &githttp.BackendOptions{ | ||
ErrorLog: logger, | ||
Prefix: prefix, | ||
}) | ||
|
||
handler := LoggingMiddleware(logger, gitmw) | ||
log.Printf("Starting server on %q for directory %q", addr, directory) | ||
if err := http.ListenAndServe(addr, handler); !errors.Is(err, http.ErrServerClosed) { | ||
return err | ||
} | ||
return nil | ||
}, | ||
} | ||
|
||
func main() { | ||
if err := rootCmd.Execute(); err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.