-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit cb4dffc
Showing
7 changed files
with
191 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 @@ | ||
.github/ |
This file contains 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,48 @@ | ||
name: Build | ||
|
||
on: | ||
push: | ||
tags: | ||
- '*' | ||
|
||
jobs: | ||
|
||
build: | ||
|
||
name: Build | ||
runs-on: ubuntu-latest | ||
|
||
env: | ||
REPO: vanity | ||
REGISTRY: ghcr.io | ||
USERNAME: ${{ github.repository_owner }} | ||
REPOSITORY: ${{ github.event.repository.name }} | ||
|
||
steps: | ||
|
||
- name: Checkout code | ||
uses: actions/checkout@v3 | ||
|
||
- name: Setup Docker BuildX | ||
uses: docker/setup-buildx-action@v1 | ||
|
||
- name: Login to the registry | ||
uses: docker/login-action@v1 | ||
with: | ||
registry: ${{ env.REGISTRY }} | ||
username: ${{ env.USERNAME }} | ||
password: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
- name: Extract image metadata | ||
id: meta | ||
uses: docker/metadata-action@v4 | ||
with: | ||
images: ${{ env.REGISTRY }}/${{ env.USERNAME }}/${{ env.REPO }} | ||
|
||
- name: Build and push image to registry | ||
uses: docker/build-push-action@v3 | ||
with: | ||
context: . | ||
push: true | ||
tags: ${{ steps.meta.outputs.tags }} | ||
labels: ${{ steps.meta.outputs.labels }} |
This file contains 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,31 @@ | ||
name: Release | ||
|
||
on: | ||
push: | ||
tags: | ||
- '*' | ||
|
||
jobs: | ||
|
||
release: | ||
|
||
name: Release | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Setup Go | ||
uses: actions/setup-go@v3 | ||
with: | ||
go-version: 1.19 | ||
|
||
- name: Checkout code | ||
uses: actions/checkout@v3 | ||
|
||
- name: Build and release | ||
uses: goreleaser/goreleaser-action@v3 | ||
with: | ||
distribution: goreleaser | ||
version: latest | ||
args: release --rm-dist | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
This file contains 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,2 @@ | ||
.DS_Store | ||
.idea/ |
This file contains 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,10 @@ | ||
FROM golang:1.19-alpine | ||
WORKDIR $GOPATH/src/go.sadegh.io/vanity | ||
COPY . . | ||
RUN CGO_ENABLED=0 go install | ||
|
||
FROM alpine:latest | ||
COPY --from=0 /go/bin/vanity /bin/vanity | ||
EXPOSE 8080 | ||
USER 2000:2000 | ||
CMD ["/bin/vanity"] |
This file contains 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,3 @@ | ||
module go.sadegh.io/vanity | ||
|
||
go 1.19 |
This file contains 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,96 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"html/template" | ||
"log" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"path" | ||
) | ||
|
||
const ( | ||
envVanityVCS = "VANITY_VCS" | ||
envVanityVCSURL = "VANITY_VCS_URL" | ||
) | ||
|
||
func main() { | ||
vcs := os.Getenv(envVanityVCS) | ||
if vcs == "" { | ||
vcs = "git" | ||
} | ||
|
||
vcsURL := os.Getenv(envVanityVCSURL) | ||
if vcsURL == "" { | ||
log.Fatalf("%s must be set, e.g. https://github.com/username", envVanityVCSURL) | ||
} | ||
|
||
u, err := url.Parse(vcsURL) | ||
if err != nil { | ||
log.Fatalf("invalid vcs url: %v", err) | ||
} | ||
|
||
if u.Scheme != "https" { | ||
log.Fatalf("%s scheme must be https", envVanityVCSURL) | ||
} | ||
|
||
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(200) | ||
_, _ = w.Write([]byte("ok")) | ||
}) | ||
|
||
http.HandleFunc("/", handler(vcs, u)) | ||
|
||
log.Printf("starting web server on :8080, vcs: %s, url: %s", vcs, vcsURL) | ||
log.Fatal(http.ListenAndServe(":8080", nil)) | ||
} | ||
|
||
var tmpl = template.Must(template.New("html").Parse(`<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> | ||
<meta name="go-import" content="{{.Host}} {{.VCS}} {{.VCSURL}}"> | ||
</head> | ||
</html> | ||
`)) | ||
|
||
func handler(vcs string, vcsURL *url.URL) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
if r.Method != http.MethodGet { | ||
w.WriteHeader(http.StatusMethodNotAllowed) | ||
return | ||
} | ||
|
||
u, err := url.Parse(fmt.Sprintf("https://%s%s", vcsURL.Host, path.Join(vcsURL.Path, r.URL.Path))) | ||
if err != nil { | ||
http.Error(w, fmt.Sprintf("error building VCS URL: %v", err), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
if r.URL.Query().Get("go-get") != "1" || len(r.URL.Path) < 2 { | ||
http.Redirect(w, r, u.String(), http.StatusTemporaryRedirect) | ||
return | ||
} | ||
|
||
data := struct { | ||
Host string | ||
VCS string | ||
VCSURL string | ||
}{ | ||
path.Join(r.Host, r.URL.Path), | ||
vcs, | ||
u.String(), | ||
} | ||
|
||
var buf bytes.Buffer | ||
if err := tmpl.Execute(&buf, &data); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
w.Header().Set("Cache-Control", "no-store") | ||
_, _ = w.Write(buf.Bytes()) | ||
} | ||
} |