ポート番号の修正 + 言語はliblocale化 + 複数言語対応
This commit is contained in:
parent
1c1de3c8d5
commit
35999991ec
|
@ -1,3 +1,8 @@
|
|||
# 2.2.0
|
||||
* ポート番号の修正
|
||||
* 言語はliblocale化
|
||||
* 複数言語対応
|
||||
|
||||
# 2.1.1
|
||||
* バグを修正
|
||||
|
||||
|
|
10
config.go
10
config.go
|
@ -10,11 +10,7 @@ import (
|
|||
)
|
||||
|
||||
type Config struct {
|
||||
configpath string
|
||||
linkpath string
|
||||
webpath string
|
||||
domain string
|
||||
ip string
|
||||
configpath, linkpath, webpath, domain, ip string
|
||||
}
|
||||
|
||||
func geturl (url string, linkpath string, checkjson bool) (string, string) {
|
||||
|
@ -53,11 +49,13 @@ func getconf () (Config, error) {
|
|||
prefix := "/usr"
|
||||
if runtime.GOOS == "freebsd" || runtime.GOOS == "openbsd" {
|
||||
prefix += "/local"
|
||||
} else if runtime.GOOS == "netbsd" {
|
||||
prefix += "/pkg"
|
||||
}
|
||||
|
||||
cnf.configpath = "/etc/urloli/config.json"
|
||||
cnf.linkpath = "/etc/urloli/links.json"
|
||||
if runtime.GOOS == "freebsd" {
|
||||
if runtime.GOOS == "freebsd" || runtime.GOOS == "netbsd" {
|
||||
cnf.configpath = prefix + cnf.configpath
|
||||
cnf.linkpath = prefix + cnf.linkpath
|
||||
}
|
||||
|
|
2
go.mod
2
go.mod
|
@ -1,3 +1,5 @@
|
|||
module 076/urloli
|
||||
|
||||
go 1.18
|
||||
|
||||
require gitler.moe/suwako/goliblocale v1.0.0
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
gitler.moe/suwako/goliblocale v1.0.0 h1:QiQKNzdgpavwmAaYhAb5pth0I6qS8IJ7q2hYAgpXacU=
|
||||
gitler.moe/suwako/goliblocale v1.0.0/go.mod h1:pdv9Go5taevY8ClBOA+oLXjGap7G1RmIVKUMF8HSJmU=
|
49
lang.go
49
lang.go
|
@ -1,49 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func getlist (lang string) []byte {
|
||||
var jloc = []byte(`{
|
||||
"top": "トップ",
|
||||
"fuseiurl": "不正なURL",
|
||||
"tansyukuzumi": "短縮済み",
|
||||
"mikensyutu": "未検出",
|
||||
"errfusei": "URLは「http://」又は「https://」で始めます。",
|
||||
"errcharlim": "URLは500文字以内です。",
|
||||
"errurlent": "URLをご入力下さい。",
|
||||
"errurlnai": "このURLを見つけられませんでした。"
|
||||
}`)
|
||||
var eloc = []byte(`{
|
||||
"top": "Top",
|
||||
"fuseiurl": "Invalid URL",
|
||||
"tansyukuzumi": "Shortened",
|
||||
"mikensyutu": "Not found",
|
||||
"errfusei": "The URL should start with \"http://\" or \"https://\".",
|
||||
"errcharlim": "The URL should be less than 500 characters.",
|
||||
"errurlent": "Please enter a URL.",
|
||||
"errurlnai": "This URL could not be found."
|
||||
}`)
|
||||
|
||||
if lang == "en" { return eloc }
|
||||
return jloc
|
||||
}
|
||||
|
||||
func getloc (str string, lang string) string {
|
||||
var payload map[string]interface{}
|
||||
err := json.Unmarshal(getlist(lang), &payload)
|
||||
if err != nil {
|
||||
fmt.Println("loc:", err)
|
||||
return ""
|
||||
}
|
||||
|
||||
for k, v := range payload {
|
||||
if str == k {
|
||||
return v.(string)
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
{
|
||||
"top": "Top",
|
||||
"logo": "Logo",
|
||||
"fuseiurl": "Invalid URL",
|
||||
"tansyukuzumi": "Shortened",
|
||||
"mikensyutu": "Not found",
|
||||
"errfusei": "The URL should start with \"http://\" or \"https://\".",
|
||||
"errcharlim": "The URL should be less than 500 characters.",
|
||||
"errurlent": "Please enter a URL.",
|
||||
"errurlnai": "This URL could not be found.",
|
||||
"totop": "Return to toppage",
|
||||
"canunderaccess": "Please enter a URL.",
|
||||
"enterurl": "Please enter a URL.",
|
||||
"submit": "Submit",
|
||||
"langchange": "Change language"
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
{
|
||||
"top": "トップ",
|
||||
"logo": "ロゴ",
|
||||
"fuseiurl": "不正なURL",
|
||||
"tansyukuzumi": "短縮済み",
|
||||
"mikensyutu": "未検出",
|
||||
"errfusei": "URLは「http://」又は「https://」で始めます。",
|
||||
"errcharlim": "URLは500文字以内です。",
|
||||
"errurlent": "URLをご入力下さい。",
|
||||
"errurlnai": "このURLを見つけられませんでした。",
|
||||
"totop": "トップページに戻る",
|
||||
"canunderaccess": "下記のURLからアクセス出来ます。",
|
||||
"enterurl": "URLをご入力下さい",
|
||||
"submit": "送信",
|
||||
"langchange": "言語変更"
|
||||
}
|
2
main.go
2
main.go
|
@ -6,7 +6,7 @@ import (
|
|||
"strconv"
|
||||
)
|
||||
|
||||
var version = "2.1.1"
|
||||
var version = "2.2.0"
|
||||
|
||||
func help () {
|
||||
fmt.Println("使い方:");
|
||||
|
|
81
srv.go
81
srv.go
|
@ -9,17 +9,13 @@ import (
|
|||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"gitler.moe/suwako/goliblocale"
|
||||
)
|
||||
|
||||
type (
|
||||
Page struct {
|
||||
Tit string
|
||||
Err string
|
||||
Url string
|
||||
Dom string
|
||||
Lan string
|
||||
Ver string
|
||||
Ves string
|
||||
Tit, Err, Url, Dom, Lan, Ver, Ves string
|
||||
i18n map[string]string
|
||||
}
|
||||
Api struct {
|
||||
Cod int `json:"code"`
|
||||
|
@ -34,6 +30,28 @@ type (
|
|||
}
|
||||
)
|
||||
|
||||
func (p Page) T (key string) string {
|
||||
return p.i18n[key]
|
||||
}
|
||||
|
||||
func initloc (r *http.Request) string {
|
||||
supportLang := map[string]bool{
|
||||
"ja": true,
|
||||
"en": true,
|
||||
}
|
||||
|
||||
cookie, err := r.Cookie("lang")
|
||||
if err != nil {
|
||||
return "ja"
|
||||
}
|
||||
|
||||
if _, ok := supportLang[cookie.Value]; ok {
|
||||
return cookie.Value
|
||||
} else {
|
||||
return "ja"
|
||||
}
|
||||
}
|
||||
|
||||
func serv (cnf Config, port int) {
|
||||
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
|
||||
if err != nil {
|
||||
|
@ -55,6 +73,13 @@ func serv (cnf Config, port int) {
|
|||
})
|
||||
|
||||
http.HandleFunc("/api/lolify", func(w http.ResponseWriter, r *http.Request) {
|
||||
lang := initloc(r)
|
||||
i18n, err := goliblocale.GetLocale("locale/" + lang)
|
||||
if err != nil {
|
||||
fmt.Println("liblocaleエラー:%v", err)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
w.WriteHeader(200)
|
||||
res := &Api{Cod: 500, Err: "未対応"}
|
||||
|
@ -69,10 +94,10 @@ func serv (cnf Config, port int) {
|
|||
chkprx := checkprefix(addurl)
|
||||
chklim := checkcharlim(addurl)
|
||||
if !chkprx {
|
||||
res = &Api{Cod: 400, Err: getloc("errfusei", "ja")}
|
||||
res = &Api{Cod: 400, Err: i18n["errfusei"]}
|
||||
}
|
||||
if !chklim {
|
||||
res = &Api{Cod: 400, Err: getloc("errcharlim", "ja")}
|
||||
res = &Api{Cod: 400, Err: i18n["errcharlim"]}
|
||||
}
|
||||
|
||||
if chklim && chkprx {
|
||||
|
@ -84,7 +109,7 @@ func serv (cnf Config, port int) {
|
|||
}
|
||||
}
|
||||
} else {
|
||||
res = &Api{Cod: 400, Err: getloc("errurlent", "ja")}
|
||||
res = &Api{Cod: 400, Err: i18n["errurlent"]}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -96,17 +121,19 @@ func serv (cnf Config, port int) {
|
|||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
data := &Page{Ver: version, Ves: strings.ReplaceAll(version, ".", "")}
|
||||
uri := r.URL.Path
|
||||
lang := initloc(r)
|
||||
|
||||
cookie, err := r.Cookie("lang")
|
||||
i18n, err := goliblocale.GetLocale("locale/" + lang)
|
||||
if err != nil {
|
||||
data.Lan = "ja"
|
||||
} else {
|
||||
data.Lan = cookie.Value
|
||||
fmt.Println("liblocaleエラー:%v", err)
|
||||
return
|
||||
}
|
||||
data.i18n = i18n
|
||||
data.Lan = lang
|
||||
|
||||
// デフォルトページ=未検出
|
||||
data.Tit = getloc("mikensyutu", data.Lan)
|
||||
data.Err = getloc("errurlnai", data.Lan)
|
||||
data.Tit = i18n["mikensyutu"]
|
||||
data.Err = i18n["errurlnai"]
|
||||
ftmpl[0] = cnf.webpath + "/view/404.html"
|
||||
tmpl := template.Must(template.ParseFiles(ftmpl[0], ftmpl[1], ftmpl[2]))
|
||||
|
||||
|
@ -119,11 +146,11 @@ func serv (cnf Config, port int) {
|
|||
chkprx := checkprefix(addurl)
|
||||
chklim := checkcharlim(addurl)
|
||||
if !chkprx || !chklim {
|
||||
data.Tit = getloc("fuseiurl", data.Lan)
|
||||
data.Tit = i18n["fuseiurl"]
|
||||
if !chkprx {
|
||||
data.Err = getloc("errfusei", data.Lan)
|
||||
data.Err = i18n["errfusei"]
|
||||
} else if !chklim {
|
||||
data.Err = getloc("errcharlim", data.Lan)
|
||||
data.Err = i18n["errcharlim"]
|
||||
}
|
||||
} else {
|
||||
chkfn, _ := geturl(addurl, cnf.linkpath, true)
|
||||
|
@ -133,26 +160,22 @@ func serv (cnf Config, port int) {
|
|||
} else {
|
||||
data.Url = insertjson(addurl, cnf.linkpath)
|
||||
data.Dom = cnf.domain
|
||||
data.Tit = getloc("tansyukuzumi", data.Lan)
|
||||
data.Tit = i18n["tansyukuzumi"]
|
||||
ftmpl[0] = cnf.webpath + "/view/submitted.html"
|
||||
}
|
||||
}
|
||||
} else {
|
||||
data.Err = getloc("errurlent", data.Lan)
|
||||
data.Err = i18n["errurlent"]
|
||||
}
|
||||
} else if r.PostForm.Get("langchange") != "" {
|
||||
cookie, err := r.Cookie("lang")
|
||||
if err != nil || cookie.Value == "ja" {
|
||||
http.SetCookie(w, &http.Cookie{Name: "lang", Value: "en", MaxAge: 31536000, Path: "/"})
|
||||
} else {
|
||||
http.SetCookie(w, &http.Cookie{Name: "lang", Value: "ja", MaxAge: 31536000, Path: "/"})
|
||||
}
|
||||
lang := r.PostForm.Get("lang")
|
||||
http.SetCookie(w, &http.Cookie{Name: "lang", Value: lang, MaxAge: 31536000, Path: "/"})
|
||||
http.Redirect(w, r, "/", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
} else { // r.Method == "GET"
|
||||
if uri == "/" {
|
||||
data.Tit = getloc("top", data.Lan)
|
||||
data.Tit = i18n["top"]
|
||||
ftmpl[0] = cnf.webpath + "/view/index.html"
|
||||
} else {
|
||||
red, _ := geturl(uri[1:], cnf.linkpath, false)
|
||||
|
@ -169,5 +192,5 @@ func serv (cnf Config, port int) {
|
|||
})
|
||||
|
||||
fmt.Println(fmt.Sprint("http://" + cnf.ip + ":", port, " でサーバーを実行中。終了するには、CTRL+Cを押して下さい。"))
|
||||
http.ListenAndServe(cnf.ip + ":9910", nil)
|
||||
http.ListenAndServe(fmt.Sprint(cnf.ip + ":", port), nil)
|
||||
}
|
||||
|
|
|
@ -54,11 +54,27 @@ input {
|
|||
border: 1px #ff3b3b groove;
|
||||
}
|
||||
|
||||
.submit, .footer, h1, .switchlang {
|
||||
text-align: center;
|
||||
select {
|
||||
background-color: #683c7a;
|
||||
color: #d6a9d6;
|
||||
font-size: 18px;
|
||||
border-radius: 4px;
|
||||
max-width: 700px;
|
||||
border: 1px #f9f groove;
|
||||
padding: 8px;
|
||||
width: 200px;
|
||||
}
|
||||
|
||||
select, input.langchange {
|
||||
height: 35px;
|
||||
vertical-align: middle;
|
||||
}
|
||||
|
||||
.submit, .footer, h1 {
|
||||
text-align: center;
|
||||
margin-top: 32px;
|
||||
}
|
||||
|
||||
input.langchange {
|
||||
font-size: 24px;
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
{{template "header" .}}
|
||||
{{ .Err }}<br />
|
||||
<a href="/">{{if eq .Lan "ja"}}トップページに戻る{{else}}Return to toppage{{end}}</a>
|
||||
<a href="/">{{.T "totop"}}</a>
|
||||
{{template "footer" .}}
|
||||
|
|
|
@ -12,11 +12,11 @@
|
|||
</head>
|
||||
<body>
|
||||
<h1>
|
||||
<img class="headerimg" src="/static/logo.jpg" alt="{{if eq .Lan "ja"}}ロゴ{{else}}Logo{{end}}" />
|
||||
<img class="headerimg" src="/static/logo.jpg" alt="{{.T "logo"}}" />
|
||||
</h1>
|
||||
<div class="body">
|
||||
<p>
|
||||
<a href="/">{{if eq .Lan "ja"}}トップ{{else}}Top{{end}}</a> |
|
||||
<a href="/">{{.T "top"}}</a> |
|
||||
<a href="https://technicalsuwako.moe/blog/urloli-{{.Ves}}/">urloli-{{.Ver}}</a>
|
||||
</p>
|
||||
{{end}}
|
||||
|
|
|
@ -1,16 +1,20 @@
|
|||
{{template "header" .}}
|
||||
<form action="/" method="POST">
|
||||
<div class="switchlang">
|
||||
<input class="langchange" type="submit" name="langchange" value="{{if eq .Lan "ja"}}Change to English{{else}}日本語に変更する{{end}}" />
|
||||
</div>
|
||||
</form>
|
||||
<div class="form">
|
||||
<form action="/" method="POST">
|
||||
<select class="langchange" name="lang">
|
||||
<option value="ja"{{if eq .Lan "ja"}} selected{{end}}>日本語</option>
|
||||
<option value="en"{{if eq .Lan "en"}} selected{{end}}>English</option>
|
||||
</select>
|
||||
<input class="langchange" type="submit" name="langchange" value="{{.T "langchange"}}" />
|
||||
</form>
|
||||
</div>
|
||||
<hr />
|
||||
{{if eq .Lan "ja"}}URLをご入力下さい{{else}}Please enter a URL.{{end}}
|
||||
{{.T "enterurl"}}
|
||||
<div class="form">
|
||||
<form action="/" method="POST">
|
||||
<input type="text" name="newadd" value="" />
|
||||
<div class="submit">
|
||||
<input type="submit" name="sosin" value="{{if eq .Lan "ja"}}送信{{else}}Submit{{end}}" />
|
||||
<input type="submit" name="sosin" value="{{.T "submit"}}" />
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
|
||||
{{template "header" .}}
|
||||
{{if eq .Lan "ja"}}下記のURLからアクセス出来ます。{{else}}Please enter a URL.{{end}}
|
||||
{{.T "canunderaccess"}}
|
||||
<br />
|
||||
<a href="{{ .Dom }}/{{ .Url }}">{{ .Dom }}/{{ .Url }}</a>
|
||||
<br /><br />
|
||||
<a href="/">{{if eq .Lan "ja"}}トップページに戻る{{else}}Return to toppage{{end}}</a>
|
||||
<a href="/">{{.T "totop"}}</a>
|
||||
{{template "footer" .}}
|
||||
|
|
Loading…
Reference in New Issue