Skip to content

Commit

Permalink
Merge pull request #1 from justincjohnson/update-fork
Browse files Browse the repository at this point in the history
Initial changes for fork
  • Loading branch information
Justin Johnson authored Apr 21, 2022
2 parents fd1ba10 + 4047a40 commit 04fcc92
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 273 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
on: [push, pull_request]
name: Go Test

jobs:
unit:
strategy:
fail-fast: false
matrix:
os: [ "ubuntu" ]
go: [ "1.17.x", "1.18.x" ]
env:
COVERAGES: ""
runs-on: ${{ matrix.os }}-latest
name: ${{ matrix.os}} (go ${{ matrix.go }})
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go }}
- name: Go information
run: |
go version
go env
- name: Run tests
run: go test -v ./...
- name: Check formatted
run: gofmt -l .
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# IPFS Redirects Parser

**Experimental**
Parser for the IPFS HTTP Gateway's `_redirects` file format.

Currently this is a subset of Netlify's [redirects format](https://www.netlify.com/docs/redirects/).
The details of the supported functionality are still in flux and will eventually be included in a [spec](https://github.com/ipfs/specs).

## Format
Currently only supports `from`, `to`, `status` and `force`.

```
from to [status][!]
```

## Example

```sh
# Implicit 301 redirects
/home /
/blog/my-post.php /blog/my-post
/news /blog
/google https://www.google.com

# Redirect with a 301
/home / 301

# Redirect with a 302
/my-redirect / 302

# Rewrite a path
/pass-through /index.html 200

# Show a custom 404 for this path
/ecommerce /store-closed 404

# Single page app rewrite
/* /index.html 200

# Proxying
/api/* https://api.example.com/:splat 200

# Forced redirect, even if the from path exists
/app/* /app/index.html 200!
```

---

## Credit
This project was forked from [tj/go-redirects](https://github.com/tj/go-redirects). Thank you TJ for the initial work. 🙏
149 changes: 0 additions & 149 deletions Readme.md

This file was deleted.

9 changes: 0 additions & 9 deletions ci.yml

This file was deleted.

15 changes: 15 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module github.com/justincjohnson/go-ipfs-redirects

go 1.17

require (
github.com/pkg/errors v0.9.1
github.com/tj/assert v0.0.3
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.6.1 // indirect
gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c // indirect
)
17 changes: 17 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tj/assert v0.0.3 h1:Df/BlaZ20mq6kuai7f5z2TvPFiwC3xaWJSDQNiIS3Rk=
github.com/tj/assert v0.0.3/go.mod h1:Ne6X72Q+TB1AteidzQncjw9PabbMp4PBMZ1k+vd1Pvk=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c h1:grhR+C34yXImVGp7EzNk+DTIk+323eIUWOmEevy6bDo=
gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
50 changes: 4 additions & 46 deletions redirects.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,6 @@ import (
"github.com/pkg/errors"
)

// Params is a map of key/value pairs.
type Params map[string]interface{}

// Has returns true if the param is present.
func (p *Params) Has(key string) bool {
if p == nil {
return false
}

_, ok := (*p)[key]
return ok
}

// Get returns the key value.
func (p *Params) Get(key string) interface{} {
if p == nil {
return nil
}

return (*p)[key]
}

// A Rule represents a single redirection or rewrite rule.
type Rule struct {
// From is the path which is matched to perform the rule.
Expand All @@ -55,9 +33,6 @@ type Rule struct {
// Force is used to force a rewrite or redirect even
// when a response (or static file) is present.
Force bool

// Params is an optional arbitrary map of key/value pairs.
Params Params
}

// IsRewrite returns true if the rule represents a rewrite (status 200).
Expand Down Expand Up @@ -109,6 +84,10 @@ func Parse(r io.Reader) (rules []Rule, err error) {
return nil, errors.Wrapf(err, "missing destination path: %q", line)
}

if len(fields) > 3 {
return nil, errors.Wrapf(err, "must match format `from to [status][!]`")
}

// src and dst
rule := Rule{
From: fields[0],
Expand All @@ -127,11 +106,6 @@ func Parse(r io.Reader) (rules []Rule, err error) {
rule.Force = force
}

// params
if len(fields) > 3 {
rule.Params = parseParams(fields[3:])
}

rules = append(rules, rule)
}

Expand All @@ -144,22 +118,6 @@ func ParseString(s string) ([]Rule, error) {
return Parse(strings.NewReader(s))
}

// parseParams returns parsed param key/value pairs.
func parseParams(pairs []string) Params {
m := make(Params)

for _, p := range pairs {
parts := strings.Split(p, "=")
if len(parts) > 1 {
m[parts[0]] = parts[1]
} else {
m[parts[0]] = true
}
}

return m
}

// parseStatus returns the status code and force when "!" suffix is present.
func parseStatus(s string) (code int, force bool, err error) {
if strings.HasSuffix(s, "!") {
Expand Down
Loading

0 comments on commit 04fcc92

Please sign in to comment.