Skip to content

Commit 6316ccf

Browse files
Merge pull request #6: git-bundle-web-server: simple web-server process
The `git-bundle-web-server` process runs the server process. It performs a very basic form of routing to ensure that the input route is still in the `routes` file, then serves the `bundle-list` or the requested `.bundle` file.
2 parents d928300 + 59b391a commit 6316ccf

File tree

5 files changed

+98
-0
lines changed

5 files changed

+98
-0
lines changed

.github/workflows/main.yml

+2
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ jobs:
2020
run: |
2121
cd $GITHUB_WORKSPACE/src/git-bundle-server
2222
GOPATH="$GITHUB_WORKSPACE/../../" go build ./cmd/git-bundle-server
23+
GOPATH="$GITHUB_WORKSPACE/../../" go build ./cmd/git-bundle-web-server
2324
2425
- name: Check style
2526
run: |
2627
cd $GITHUB_WORKSPACE/src/git-bundle-server
2728
GOPATH="$GITHUB_WORKSPACE/../../" go vet ./cmd/git-bundle-server
29+
GOPATH="$GITHUB_WORKSPACE/../../" go vet ./cmd/git-bundle-web-server
2830
2931
# TODO: add automated tests.

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/git-bundle-server
2+
/git-bundle-web-server

cmd/git-bundle-web-server/go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/github/git-bundle-server/cmd/git-bundle-web-server
2+
3+
go 1.19

cmd/git-bundle-web-server/main.go

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"net/http"
7+
"os"
8+
"strings"
9+
10+
"github.com/github/git-bundle-server/internal/core"
11+
)
12+
13+
func parseRoute(path string) (string, string, string, error) {
14+
if len(path) == 0 {
15+
return "", "", "", fmt.Errorf("empty route")
16+
}
17+
18+
if path[0] == '/' {
19+
path = path[1:]
20+
}
21+
22+
slash1 := strings.Index(path, "/")
23+
if slash1 < 0 {
24+
return "", "", "", fmt.Errorf("route has owner, but no repo")
25+
}
26+
slash2 := strings.Index(path[slash1+1:], "/")
27+
if slash2 < 0 {
28+
// No trailing slash.
29+
return path[:slash1], path[slash1+1:], "", nil
30+
}
31+
slash2 += slash1 + 1
32+
slash3 := strings.Index(path[slash2+1:], "/")
33+
if slash3 >= 0 {
34+
return "", "", "", fmt.Errorf("path has depth exceeding three")
35+
}
36+
37+
return path[:slash1], path[slash1+1 : slash2], path[slash2+1:], nil
38+
}
39+
40+
func serve(w http.ResponseWriter, r *http.Request) {
41+
path := r.URL.Path
42+
43+
owner, repo, file, err := parseRoute(path)
44+
if err != nil {
45+
w.WriteHeader(http.StatusNotFound)
46+
fmt.Printf("Failed to parse route: %s\n", err)
47+
return
48+
}
49+
50+
route := owner + "/" + repo
51+
52+
repos, err := core.GetRepositories()
53+
if err != nil {
54+
w.WriteHeader(http.StatusInternalServerError)
55+
fmt.Printf("Failed to load routes\n")
56+
return
57+
}
58+
59+
repository, contains := repos[route]
60+
if !contains {
61+
w.WriteHeader(http.StatusNotFound)
62+
fmt.Printf("Failed to get route out of repos\n")
63+
return
64+
}
65+
66+
if file == "" {
67+
file = "bundle-list"
68+
}
69+
70+
fileToServe := repository.WebDir + "/" + file
71+
data, err := os.ReadFile(fileToServe)
72+
if err != nil {
73+
w.WriteHeader(http.StatusNotFound)
74+
fmt.Printf("Failed to read file\n")
75+
return
76+
}
77+
78+
fmt.Printf("Successfully serving content for %s/%s\n", route, file)
79+
w.Write(data)
80+
}
81+
82+
func main() {
83+
// API routes
84+
http.HandleFunc("/", serve)
85+
86+
port := ":8080"
87+
fmt.Println("Server is running on port" + port)
88+
89+
// Start server on port specified above
90+
log.Fatal(http.ListenAndServe(port, nil))
91+
}

go.work

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ go 1.18
22

33
use (
44
./cmd/git-bundle-server/
5+
./cmd/git-bundle-web-server/
56
./internal/bundles/
67
./internal/core/
78
./internal/git/

0 commit comments

Comments
 (0)