Skip to content

Commit 044b780

Browse files
committed
Initial commit
0 parents  commit 044b780

File tree

5 files changed

+104
-0
lines changed

5 files changed

+104
-0
lines changed

.gitlab-ci.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
image: golang:1.8.6-alpine3.6
2+
3+
stages:
4+
- test
5+
- deploy
6+
7+
format-test:
8+
stage: test
9+
script:
10+
- go fmt
11+
- go vet
12+
- go test
13+
14+
deploy:
15+
image: google/cloud-sdk:183.0.0
16+
stage: deploy
17+
script:
18+
- gcloud app deploy --no-promote
19+
when: manual

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Takuya Noguchi
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

app.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
runtime: go
2+
api_version: go1.8
3+
4+
handlers:
5+
- url: /.*
6+
script: _go_app
7+
secure: always

redirect.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package redirect
2+
3+
import (
4+
"net/http"
5+
"strings"
6+
)
7+
8+
const (
9+
domain = "https://docs.djangoproject.com/ja/2.0/"
10+
prefix = "/en/latest/"
11+
suffix = ".html"
12+
)
13+
14+
func mapUrl(s string) (r string) {
15+
if strings.HasSuffix(s, suffix) {
16+
s = strings.TrimSuffix(s, suffix) + "/"
17+
}
18+
19+
r = domain
20+
if strings.HasPrefix(s, prefix) {
21+
r += strings.TrimPrefix(s, prefix)
22+
}
23+
24+
return r
25+
}
26+
27+
func redirect(w http.ResponseWriter, r *http.Request) {
28+
var s = mapUrl(r.URL.Path)
29+
30+
http.Redirect(w, r, s, 301)
31+
}
32+
33+
func init() {
34+
http.HandleFunc("/", redirect)
35+
}

redirect_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package redirect
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestMapUrl(t *testing.T) {
8+
m := map[string]string{
9+
"": "https://docs.djangoproject.com/ja/2.0/",
10+
"/": "https://docs.djangoproject.com/ja/2.0/",
11+
"/favicon.ico": "https://docs.djangoproject.com/ja/2.0/",
12+
"/en/latest/faq/": "https://docs.djangoproject.com/ja/2.0/faq/",
13+
"/ja/latest/faq/": "https://docs.djangoproject.com/ja/2.0/",
14+
"/en/latest/contents.html": "https://docs.djangoproject.com/ja/2.0/contents/",
15+
}
16+
17+
for k, v := range m {
18+
if r := mapUrl(k); r != v {
19+
t.Errorf("mapUrl(%q) is expected: %q, but actually %q", k, v, r)
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)