Skip to content

Commit

Permalink
init: vanity
Browse files Browse the repository at this point in the history
  • Loading branch information
xsadegh committed Nov 7, 2022
0 parents commit cb4dffc
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 0 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.github/
48 changes: 48 additions & 0 deletions .github/workflows/build.yaml
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 }}
31 changes: 31 additions & 0 deletions .github/workflows/release.yaml
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 }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
.idea/
10 changes: 10 additions & 0 deletions Dockerfile
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"]
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module go.sadegh.io/vanity

go 1.19
96 changes: 96 additions & 0 deletions vanity.go
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())
}
}

0 comments on commit cb4dffc

Please sign in to comment.