Skip to content

Commit 7258876

Browse files
committed
ADD: root folder
1 parent 549ae21 commit 7258876

File tree

5 files changed

+41
-8
lines changed

5 files changed

+41
-8
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ ENV PATH_WEBHOOK "_hook"
2020
ENV PATH_HEALTH "_health"
2121
ENV REPO_BRANCH "main"
2222
ENV REPO_TARGET_FOLDER "target-git"
23-
ENV REPO_URL "https://github.com/jarpsimoes/git-http-server.git"
23+
ENV REPO_URL ""
2424
ENV HTTP_PORT 8081
2525

2626
RUN mkdir -p ${APP_HOME}

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,16 @@ The GIT-HttpServer only support basic authentication on repositories by protocol
2323
| PATH_VERSION | Set get git commit version path | _version | Yes |
2424
| PATH_WEBHOOK | Set webhook path | _hook | Yes |
2525
| PATH_HEALTH | Set health check path | _health | Yes |
26-
| REPO_BRANCH | Set default branch to clone content | main | Yes |
27-
| REPO_TARGET_FOLDER | Set folder to clone source | target-git | Yes |
28-
| REPO_URL | Set url as a source origin | https://github.com/jarpsimoes/git-http-server.git | Yes |
26+
| REPO_BRANCH | Set default branch to clone content | main | No |
27+
| REPO_TARGET_FOLDER | Set folder to clone source | target-git | No |
28+
| REPO_URL | Set url as a source origin | https://github.com/jarpsimoes/git-http-server.git | No |
2929
| REPO_USERNAME | Set username or token identifier to basic authentication | N/D | No |
3030
| REPO_PASSWORD | Set password or token to basic authentication | N/D | No |
3131
| HTTP_PORT | Set port to expose content | 8081 | Yes |
3232
| GHS_CUSTOM_PATH_<path> | Custom path to work as a proxy server | N/D | No |
3333
| GHS_CUSTOM_REWRITE_<path> | Set to remove from proxy request base path | N/D | No |
34+
| FOLDER_ROOT | Select root folder inside cloned repository | $REPO_TARGET_FOLDER/ | No |
35+
3436

3537
## Implementation
3638

src/handlers/default_handlers.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
// StaticContentHandler it's a provider static content cloned from repository
1515
func StaticContentHandler(w http.ResponseWriter, r *http.Request) {
1616
repoConfig := utils.GetRepositoryConfigInstance()
17+
1718
keys, ok := r.URL.Query()["_branch"]
1819

1920
branch := repoConfig.GetBranch()
@@ -25,6 +26,15 @@ func StaticContentHandler(w http.ResponseWriter, r *http.Request) {
2526
}
2627

2728
basePath := utils.BuildBranchPath(repoConfig.GetTargetFolder(), branch)
29+
30+
if repoConfig.GetRootFolder() != "" {
31+
basePath = fmt.Sprintf("%s/%s", basePath, repoConfig.GetRootFolder())
32+
33+
if strings.Contains(basePath, "//") {
34+
basePath = strings.ReplaceAll(basePath, "//", "/")
35+
}
36+
}
37+
2838
log.Printf("[Request] Path %s", basePath)
2939

3040
fs := http.FileServer(http.Dir(basePath))
@@ -66,6 +76,15 @@ func PullHandler(w http.ResponseWriter, r *http.Request) {
6676
fmt.Fprintf(w, "Last commit [%s]", commit.ToString())
6777
}
6878

79+
// FeatureNotEnabled it's a handler to response feature not enabled
80+
func FeatureNotEnabled(w http.ResponseWriter, r *http.Request) {
81+
82+
w.WriteHeader(http.StatusNotAcceptable)
83+
84+
fmt.Fprintf(w, "Operation %s not supported with present configuration \n", r.URL)
85+
86+
}
87+
6988
// HealthCheckHandler it's a handler to return server status
7089
func HealthCheckHandler(w http.ResponseWriter, r *http.Request) {
7190
healthCheck := utils.GetHealthCheckControlInstance()

src/server.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,15 @@ func main() {
1616
repo := utils.GetRepositoryConfigInstance()
1717
customPaths := utils.GetCustomPathsInstance()
1818

19-
utils.CloneRepository(repo.GetRepo(), repo.GetBranch(), repo.GetTargetFolder(), true)
19+
if repo.GetRepo() != "" {
20+
utils.CloneRepository(repo.GetRepo(), repo.GetBranch(), repo.GetTargetFolder(), true)
21+
http.HandleFunc(fmt.Sprintf("/%s/", routeConfig.GetClone()), handlers.CloneHandler)
22+
http.HandleFunc(fmt.Sprintf("/%s/", routeConfig.GetPull()), handlers.PullHandler)
23+
} else {
24+
http.HandleFunc(fmt.Sprintf("/%s/", routeConfig.GetClone()), handlers.FeatureNotEnabled)
25+
http.HandleFunc(fmt.Sprintf("/%s/", routeConfig.GetPull()), handlers.FeatureNotEnabled)
26+
}
2027

21-
http.HandleFunc(fmt.Sprintf("/%s/", routeConfig.GetClone()), handlers.CloneHandler)
22-
http.HandleFunc(fmt.Sprintf("/%s/", routeConfig.GetPull()), handlers.PullHandler)
2328
http.HandleFunc(fmt.Sprintf("/%s/", routeConfig.GetHealthCheck()), handlers.HealthCheckHandler)
2429
if len(*customPaths) > 0 {
2530

src/utils/environment_config.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type BaseRepositoryConfig struct {
2828
repoURL string
2929
branch string
3030
targetFolder string
31+
rootFolder string
3132
}
3233

3334
// BasicAuthenticationMethod it's a struct to represent authorization configuration
@@ -121,6 +122,11 @@ func (brc BaseRepositoryConfig) GetTargetFolder() string {
121122
return brc.targetFolder
122123
}
123124

125+
// GetRootFolder (baseRepositoryConfig) it's a method to get root folder
126+
func (brc BaseRepositoryConfig) GetRootFolder() string {
127+
return brc.rootFolder
128+
}
129+
124130
// Show (BasicAuthenticationMethod) it's a function print strut content as string
125131
func (bam BasicAuthenticationMethod) Show() string {
126132
return fmt.Sprintf("username=%v, password=******", bam.username)
@@ -149,14 +155,14 @@ func GetRouteConfigInstance() *BaseRouteConfig {
149155
if baseRouteConfigInstance == nil {
150156
log.Println("[BaseRouteConfig] Creating new instance ")
151157

152-
// TODO Replace with Environment Variables
153158
baseRouteConfigInstance = &BaseRouteConfig{
154159
clonePath: os.Getenv("PATH_CLONE"),
155160
webhookPath: os.Getenv("PATH_WEBHOOK"),
156161
pullPath: os.Getenv("PATH_PULL"),
157162
versionPath: os.Getenv("PATH_VERSION"),
158163
healthCheckControl: os.Getenv("PATH_HEALTH"),
159164
}
165+
160166
}
161167
}
162168
return baseRouteConfigInstance
@@ -175,6 +181,7 @@ func GetRepositoryConfigInstance() *BaseRepositoryConfig {
175181
repoURL: os.Getenv("REPO_URL"),
176182
branch: os.Getenv("REPO_BRANCH"),
177183
targetFolder: os.Getenv("REPO_TARGET_FOLDER"),
184+
rootFolder: os.Getenv("FOLDER_ROOT"),
178185
}
179186

180187
}

0 commit comments

Comments
 (0)