mirror of
https://github.com/jimeh/shortmarks.git
synced 2026-02-19 09:56:42 +00:00
feat: initial implementation
This commit is contained in:
44
.github/workflows/ci.yml
vendored
Normal file
44
.github/workflows/ci.yml
vendored
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
---
|
||||||
|
name: CI
|
||||||
|
on: [push]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- uses: actions/setup-go@v2
|
||||||
|
with:
|
||||||
|
go-version: 1.18
|
||||||
|
- name: Install dependencies
|
||||||
|
run: go mod download
|
||||||
|
- name: Build binary
|
||||||
|
run: make
|
||||||
|
|
||||||
|
release:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
needs: [test]
|
||||||
|
if: startsWith(github.ref, 'refs/tags/v')
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
- name: Set up QEMU
|
||||||
|
uses: docker/setup-qemu-action@v1
|
||||||
|
- uses: docker/setup-buildx-action@v1
|
||||||
|
- name: Docker Login
|
||||||
|
uses: docker/login-action@v1
|
||||||
|
with:
|
||||||
|
registry: ghcr.io
|
||||||
|
username: ${{ github.repository_owner }}
|
||||||
|
password: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
- uses: actions/setup-go@v2
|
||||||
|
with:
|
||||||
|
go-version: 1.18
|
||||||
|
- name: Run GoReleaser
|
||||||
|
uses: goreleaser/goreleaser-action@v2
|
||||||
|
with:
|
||||||
|
version: latest
|
||||||
|
args: release --rm-dist
|
||||||
|
env:
|
||||||
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
bin/*
|
||||||
|
/shortmarks
|
||||||
100
.goreleaser.yml
Normal file
100
.goreleaser.yml
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
before:
|
||||||
|
hooks:
|
||||||
|
- go mod tidy
|
||||||
|
builds:
|
||||||
|
- env:
|
||||||
|
- CGO_ENABLED=0
|
||||||
|
mod_timestamp: "{{ .CommitTimestamp }}"
|
||||||
|
flags:
|
||||||
|
- -trimpath
|
||||||
|
ldflags:
|
||||||
|
- "-s -w"
|
||||||
|
goos:
|
||||||
|
- "darwin"
|
||||||
|
- "freebsd"
|
||||||
|
- "linux"
|
||||||
|
- "windows"
|
||||||
|
goarch:
|
||||||
|
- "amd64"
|
||||||
|
- "386"
|
||||||
|
- "arm"
|
||||||
|
- "arm64"
|
||||||
|
goarm:
|
||||||
|
- "6"
|
||||||
|
- "7"
|
||||||
|
|
||||||
|
checksum:
|
||||||
|
name_template: "checksums.txt"
|
||||||
|
snapshot:
|
||||||
|
name_template: "{{ .Tag }}-next"
|
||||||
|
changelog:
|
||||||
|
sort: asc
|
||||||
|
filters:
|
||||||
|
exclude:
|
||||||
|
- "^docs:"
|
||||||
|
- "^test:"
|
||||||
|
|
||||||
|
dockers:
|
||||||
|
- image_templates:
|
||||||
|
- "ghcr.io/jimeh/shortmarks:{{ .Version }}-amd64"
|
||||||
|
- "ghcr.io/jimeh/shortmarks:latest-amd64"
|
||||||
|
dockerfile: Dockerfile.goreleaser
|
||||||
|
use: buildx
|
||||||
|
goos: linux
|
||||||
|
goarch: amd64
|
||||||
|
build_flag_templates:
|
||||||
|
- "--platform=linux/amd64"
|
||||||
|
- image_templates:
|
||||||
|
- "ghcr.io/jimeh/shortmarks:{{ .Version }}-386"
|
||||||
|
- "ghcr.io/jimeh/shortmarks:latest-386"
|
||||||
|
dockerfile: Dockerfile.goreleaser
|
||||||
|
use: buildx
|
||||||
|
goos: linux
|
||||||
|
goarch: "386"
|
||||||
|
build_flag_templates:
|
||||||
|
- "--platform=linux/386"
|
||||||
|
- image_templates:
|
||||||
|
- "ghcr.io/jimeh/shortmarks:{{ .Version }}-arm64"
|
||||||
|
- "ghcr.io/jimeh/shortmarks:latest-arm64"
|
||||||
|
use: buildx
|
||||||
|
goos: linux
|
||||||
|
goarch: arm64
|
||||||
|
dockerfile: Dockerfile.goreleaser
|
||||||
|
build_flag_templates:
|
||||||
|
- "--platform=linux/arm64"
|
||||||
|
- image_templates:
|
||||||
|
- "ghcr.io/jimeh/shortmarks:{{ .Version }}-armv6"
|
||||||
|
- "ghcr.io/jimeh/shortmarks:latest-armv6"
|
||||||
|
use: buildx
|
||||||
|
goos: linux
|
||||||
|
goarch: arm
|
||||||
|
goarm: "6"
|
||||||
|
dockerfile: Dockerfile.goreleaser
|
||||||
|
build_flag_templates:
|
||||||
|
- "--platform=linux/arm/v6"
|
||||||
|
- image_templates:
|
||||||
|
- "ghcr.io/jimeh/shortmarks:{{ .Version }}-armv7"
|
||||||
|
- "ghcr.io/jimeh/shortmarks:latest-armv7"
|
||||||
|
use: buildx
|
||||||
|
goos: linux
|
||||||
|
goarch: arm
|
||||||
|
goarm: "7"
|
||||||
|
dockerfile: Dockerfile.goreleaser
|
||||||
|
build_flag_templates:
|
||||||
|
- "--platform=linux/arm/v7"
|
||||||
|
|
||||||
|
docker_manifests:
|
||||||
|
- name_template: ghcr.io/jimeh/shortmarks:{{ .Version }}
|
||||||
|
image_templates:
|
||||||
|
- ghcr.io/jimeh/shortmarks:{{ .Version }}-amd64
|
||||||
|
- ghcr.io/jimeh/shortmarks:{{ .Version }}-386
|
||||||
|
- ghcr.io/jimeh/shortmarks:{{ .Version }}-arm64
|
||||||
|
- ghcr.io/jimeh/shortmarks:{{ .Version }}-armv6
|
||||||
|
- ghcr.io/jimeh/shortmarks:{{ .Version }}-armv7
|
||||||
|
- name_template: ghcr.io/jimeh/shortmarks:latest
|
||||||
|
image_templates:
|
||||||
|
- ghcr.io/jimeh/shortmarks:latest-amd64
|
||||||
|
- ghcr.io/jimeh/shortmarks:latest-386
|
||||||
|
- ghcr.io/jimeh/shortmarks:latest-arm64
|
||||||
|
- ghcr.io/jimeh/shortmarks:latest-armv6
|
||||||
|
- ghcr.io/jimeh/shortmarks:latest-armv7
|
||||||
13
Dockerfile
Normal file
13
Dockerfile
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
FROM golang:1.18-alpine as builder
|
||||||
|
|
||||||
|
RUN apk add --no-cache git make
|
||||||
|
WORKDIR /app
|
||||||
|
COPY . .
|
||||||
|
RUN env CGO_ENABLED=0 go build -a -o shortmarks -ldflags "-s -w"
|
||||||
|
|
||||||
|
FROM scratch
|
||||||
|
ENV PORT 8080
|
||||||
|
EXPOSE 8080
|
||||||
|
WORKDIR /
|
||||||
|
COPY --from=builder /app/shortmarks /
|
||||||
|
CMD ["/shortmarks"]
|
||||||
6
Dockerfile.goreleaser
Normal file
6
Dockerfile.goreleaser
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
FROM scratch
|
||||||
|
COPY ./shortmarks /
|
||||||
|
ENV PORT 8080
|
||||||
|
EXPOSE 8080
|
||||||
|
WORKDIR /
|
||||||
|
CMD ["/shortmarks"]
|
||||||
116
LICENSE
Normal file
116
LICENSE
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
CC0 1.0 Universal
|
||||||
|
|
||||||
|
Statement of Purpose
|
||||||
|
|
||||||
|
The laws of most jurisdictions throughout the world automatically confer
|
||||||
|
exclusive Copyright and Related Rights (defined below) upon the creator and
|
||||||
|
subsequent owner(s) (each and all, an "owner") of an original work of
|
||||||
|
authorship and/or a database (each, a "Work").
|
||||||
|
|
||||||
|
Certain owners wish to permanently relinquish those rights to a Work for the
|
||||||
|
purpose of contributing to a commons of creative, cultural and scientific
|
||||||
|
works ("Commons") that the public can reliably and without fear of later
|
||||||
|
claims of infringement build upon, modify, incorporate in other works, reuse
|
||||||
|
and redistribute as freely as possible in any form whatsoever and for any
|
||||||
|
purposes, including without limitation commercial purposes. These owners may
|
||||||
|
contribute to the Commons to promote the ideal of a free culture and the
|
||||||
|
further production of creative, cultural and scientific works, or to gain
|
||||||
|
reputation or greater distribution for their Work in part through the use and
|
||||||
|
efforts of others.
|
||||||
|
|
||||||
|
For these and/or other purposes and motivations, and without any expectation
|
||||||
|
of additional consideration or compensation, the person associating CC0 with a
|
||||||
|
Work (the "Affirmer"), to the extent that he or she is an owner of Copyright
|
||||||
|
and Related Rights in the Work, voluntarily elects to apply CC0 to the Work
|
||||||
|
and publicly distribute the Work under its terms, with knowledge of his or her
|
||||||
|
Copyright and Related Rights in the Work and the meaning and intended legal
|
||||||
|
effect of CC0 on those rights.
|
||||||
|
|
||||||
|
1. Copyright and Related Rights. A Work made available under CC0 may be
|
||||||
|
protected by copyright and related or neighboring rights ("Copyright and
|
||||||
|
Related Rights"). Copyright and Related Rights include, but are not limited
|
||||||
|
to, the following:
|
||||||
|
|
||||||
|
i. the right to reproduce, adapt, distribute, perform, display, communicate,
|
||||||
|
and translate a Work;
|
||||||
|
|
||||||
|
ii. moral rights retained by the original author(s) and/or performer(s);
|
||||||
|
|
||||||
|
iii. publicity and privacy rights pertaining to a person's image or likeness
|
||||||
|
depicted in a Work;
|
||||||
|
|
||||||
|
iv. rights protecting against unfair competition in regards to a Work,
|
||||||
|
subject to the limitations in paragraph 4(a), below;
|
||||||
|
|
||||||
|
v. rights protecting the extraction, dissemination, use and reuse of data in
|
||||||
|
a Work;
|
||||||
|
|
||||||
|
vi. database rights (such as those arising under Directive 96/9/EC of the
|
||||||
|
European Parliament and of the Council of 11 March 1996 on the legal
|
||||||
|
protection of databases, and under any national implementation thereof,
|
||||||
|
including any amended or successor version of such directive); and
|
||||||
|
|
||||||
|
vii. other similar, equivalent or corresponding rights throughout the world
|
||||||
|
based on applicable law or treaty, and any national implementations thereof.
|
||||||
|
|
||||||
|
2. Waiver. To the greatest extent permitted by, but not in contravention of,
|
||||||
|
applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and
|
||||||
|
unconditionally waives, abandons, and surrenders all of Affirmer's Copyright
|
||||||
|
and Related Rights and associated claims and causes of action, whether now
|
||||||
|
known or unknown (including existing as well as future claims and causes of
|
||||||
|
action), in the Work (i) in all territories worldwide, (ii) for the maximum
|
||||||
|
duration provided by applicable law or treaty (including future time
|
||||||
|
extensions), (iii) in any current or future medium and for any number of
|
||||||
|
copies, and (iv) for any purpose whatsoever, including without limitation
|
||||||
|
commercial, advertising or promotional purposes (the "Waiver"). Affirmer makes
|
||||||
|
the Waiver for the benefit of each member of the public at large and to the
|
||||||
|
detriment of Affirmer's heirs and successors, fully intending that such Waiver
|
||||||
|
shall not be subject to revocation, rescission, cancellation, termination, or
|
||||||
|
any other legal or equitable action to disrupt the quiet enjoyment of the Work
|
||||||
|
by the public as contemplated by Affirmer's express Statement of Purpose.
|
||||||
|
|
||||||
|
3. Public License Fallback. Should any part of the Waiver for any reason be
|
||||||
|
judged legally invalid or ineffective under applicable law, then the Waiver
|
||||||
|
shall be preserved to the maximum extent permitted taking into account
|
||||||
|
Affirmer's express Statement of Purpose. In addition, to the extent the Waiver
|
||||||
|
is so judged Affirmer hereby grants to each affected person a royalty-free,
|
||||||
|
non transferable, non sublicensable, non exclusive, irrevocable and
|
||||||
|
unconditional license to exercise Affirmer's Copyright and Related Rights in
|
||||||
|
the Work (i) in all territories worldwide, (ii) for the maximum duration
|
||||||
|
provided by applicable law or treaty (including future time extensions), (iii)
|
||||||
|
in any current or future medium and for any number of copies, and (iv) for any
|
||||||
|
purpose whatsoever, including without limitation commercial, advertising or
|
||||||
|
promotional purposes (the "License"). The License shall be deemed effective as
|
||||||
|
of the date CC0 was applied by Affirmer to the Work. Should any part of the
|
||||||
|
License for any reason be judged legally invalid or ineffective under
|
||||||
|
applicable law, such partial invalidity or ineffectiveness shall not
|
||||||
|
invalidate the remainder of the License, and in such case Affirmer hereby
|
||||||
|
affirms that he or she will not (i) exercise any of his or her remaining
|
||||||
|
Copyright and Related Rights in the Work or (ii) assert any associated claims
|
||||||
|
and causes of action with respect to the Work, in either case contrary to
|
||||||
|
Affirmer's express Statement of Purpose.
|
||||||
|
|
||||||
|
4. Limitations and Disclaimers.
|
||||||
|
|
||||||
|
a. No trademark or patent rights held by Affirmer are waived, abandoned,
|
||||||
|
surrendered, licensed or otherwise affected by this document.
|
||||||
|
|
||||||
|
b. Affirmer offers the Work as-is and makes no representations or warranties
|
||||||
|
of any kind concerning the Work, express, implied, statutory or otherwise,
|
||||||
|
including without limitation warranties of title, merchantability, fitness
|
||||||
|
for a particular purpose, non infringement, or the absence of latent or
|
||||||
|
other defects, accuracy, or the present or absence of errors, whether or not
|
||||||
|
discoverable, all to the greatest extent permissible under applicable law.
|
||||||
|
|
||||||
|
c. Affirmer disclaims responsibility for clearing rights of other persons
|
||||||
|
that may apply to the Work or any use thereof, including without limitation
|
||||||
|
any person's Copyright and Related Rights in the Work. Further, Affirmer
|
||||||
|
disclaims responsibility for obtaining any necessary consents, permissions
|
||||||
|
or other rights required for any use of the Work.
|
||||||
|
|
||||||
|
d. Affirmer understands and acknowledges that Creative Commons is not a
|
||||||
|
party to this document and has no duty or obligation with respect to this
|
||||||
|
CC0 or use of the Work.
|
||||||
|
|
||||||
|
For more information, please see
|
||||||
|
<http://creativecommons.org/publicdomain/zero/1.0/>
|
||||||
24
Makefile
Normal file
24
Makefile
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
NAME = shortmarks
|
||||||
|
BINARY = bin/${NAME}
|
||||||
|
VERSION ?= $(shell cat VERSION)
|
||||||
|
SOURCES = $(shell find . -name '*.go' -o -name 'Makefile')
|
||||||
|
|
||||||
|
$(BINARY): $(SOURCES)
|
||||||
|
CGO_ENABLED=0 go build -a -o ${BINARY} -ldflags "-s -w"
|
||||||
|
|
||||||
|
.PHONY: build
|
||||||
|
build: $(BINARY)
|
||||||
|
|
||||||
|
.PHONY: run
|
||||||
|
run: $(BINARY)
|
||||||
|
$(BINARY)
|
||||||
|
|
||||||
|
.PHONY: clean
|
||||||
|
clean:
|
||||||
|
$(eval BIN_DIR := $(shell dirname ${BINARY}))
|
||||||
|
if [ -f ${BINARY} ]; then rm ${BINARY}; fi
|
||||||
|
if [ -d ${BIN_DIR} ]; then rmdir ${BIN_DIR}; fi
|
||||||
|
|
||||||
|
.PHONY: docker
|
||||||
|
docker:
|
||||||
|
docker build -t "$(shell whoami)/$(NAME)" .
|
||||||
3
config.yml
Normal file
3
config.yml
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
suffix: .shortmarks.jimeh.io
|
||||||
|
shortmarks:
|
||||||
|
gj: https://github.com/jimeh
|
||||||
5
go.mod
Normal file
5
go.mod
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
module github.com/jimeh/shortmarks
|
||||||
|
|
||||||
|
go 1.18
|
||||||
|
|
||||||
|
require gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
3
go.sum
Normal file
3
go.sum
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
110
main.go
Normal file
110
main.go
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"log"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
Suffix string `yaml:"suffix"`
|
||||||
|
Shortmarks map[string]string `yaml:"shortmarks"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
err := mainE()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func mainE() error {
|
||||||
|
f := flag.NewFlagSet("shortmarks", flag.ExitOnError)
|
||||||
|
configFileFlag := f.String("c", "", "yaml config file")
|
||||||
|
portFlag := f.String("p", "", "listening port")
|
||||||
|
err := f.Parse(os.Args[1:])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
configFile := os.Getenv("SHORTMARKS_CONFIG")
|
||||||
|
if configFileFlag != nil && *configFileFlag != "" {
|
||||||
|
configFile = *configFileFlag
|
||||||
|
}
|
||||||
|
if configFile == "" {
|
||||||
|
log.Fatal("config file is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
config := &Config{}
|
||||||
|
|
||||||
|
content, err := os.ReadFile(configFile)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = yaml.Unmarshal([]byte(content), config)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
srv := &http.Server{
|
||||||
|
ReadTimeout: 5 * time.Second,
|
||||||
|
WriteTimeout: 5 * time.Second,
|
||||||
|
IdleTimeout: 30 * time.Second,
|
||||||
|
Handler: http.HandlerFunc(handler(config)),
|
||||||
|
}
|
||||||
|
|
||||||
|
port := "8080"
|
||||||
|
if v := os.Getenv("PORT"); v != "" {
|
||||||
|
port = v
|
||||||
|
}
|
||||||
|
if portFlag != nil && *portFlag != "" {
|
||||||
|
port = *portFlag
|
||||||
|
}
|
||||||
|
|
||||||
|
ln, err := net.Listen("tcp", ":"+port)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Fatal(srv.Serve(ln))
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func handler(config *Config) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, req *http.Request) {
|
||||||
|
if config.Suffix == "" || len(config.Shortmarks) == 0 {
|
||||||
|
http.Error(w, "not found", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var short string
|
||||||
|
|
||||||
|
hostname := strings.SplitN(req.Host, ":", 2)[0]
|
||||||
|
if strings.HasSuffix(hostname, config.Suffix) {
|
||||||
|
short = strings.TrimSuffix(hostname, config.Suffix)
|
||||||
|
}
|
||||||
|
|
||||||
|
target, ok := config.Shortmarks[short]
|
||||||
|
if !ok {
|
||||||
|
http.Error(w, "not found", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.URL.Path != "/" {
|
||||||
|
target += req.URL.Path
|
||||||
|
}
|
||||||
|
if req.URL.RawQuery != "" {
|
||||||
|
target += "?" + req.URL.RawQuery
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Connection", "close")
|
||||||
|
http.Redirect(w, req, target, http.StatusTemporaryRedirect)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user