コミットを比較

...

4 コミット

作成者 SHA1 メッセージ 日付
守矢諏訪子 3f2eecb036 2.1.0 2023-05-13 14:17:07 +09:00
守矢諏訪子 390acc14f2 サーバーのソースコードを短くに 2023-05-13 13:40:41 +09:00
守矢諏訪子 a7ebc09f97 API機能性 2023-05-13 13:15:09 +09:00
守矢諏訪子 eae844f78f ローカライズは関数化 2023-05-13 12:17:41 +09:00
7個のファイルの変更157行の追加92行の削除

ファイルの表示

@ -1,3 +1,8 @@
# 2.1.0(未公開)
* ローカライズは関数化
* API機能性
* サーバーのソースコードを短くに
# 2.0.2
* Makefileでの「make install」部分を修正
* manページで「オプションなし」部分を追加

ファイルの表示

@ -1,5 +1,5 @@
NAME=urloli
VERSION=2.0.2
VERSION=$(cat main.go | grep "var version" | awk '{print $4}' | sed "s/\"//g")
# Linux、Cruxの場合は必須。他のディストリビューションはどうでも良い
PREFIX=/usr
# FreeBSDとOpenBSD

49
lang.go ノーマルファイル
ファイルの表示

@ -0,0 +1,49 @@
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 ""
}

ファイルの表示

@ -6,7 +6,7 @@ import (
"strconv"
)
var version = "2.0.2"
var version = "2.1.0"
func help () {
fmt.Println("使い方:");

186
srv.go
ファイルの表示

@ -4,22 +4,87 @@ import (
"text/template"
"fmt"
"net/http"
"encoding/json"
"strings"
)
type Page struct {
Tit string
Err string
Url string
Dom string
Lan string
Ver string
}
type (
Page struct {
Tit string
Err string
Url string
Dom string
Lan string
Ver string
Ves string
}
Api struct {
Cod int `json:"code"`
Err string `json:"error"`
Url string `json:"url"`
Mot string `json:"origin"`
New bool `json:"isnew"`
}
Stat struct {
Url string `json:"url"`
Ver string `json:"version"`
}
)
func serv (cnf Config, port int) {
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
ftmpl := []string{cnf.webpath + "/view/index.html", cnf.webpath + "/view/header.html", cnf.webpath + "/view/footer.html"}
http.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(200)
buf, _ := json.MarshalIndent(&Stat{Url: cnf.domain, Ver: version}, "", " ")
_, _ = w.Write(buf)
})
http.HandleFunc("/api/lolify", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(200)
res := &Api{Cod: 500, Err: "未対応"}
if r.Method == "POST" {
err := r.ParseForm()
if err != nil {
fmt.Println(err)
res.Err = "失敗"
} else {
if r.PostForm.Get("url") != "" {
addurl := r.PostForm.Get("url")
chkprx := checkprefix(addurl)
chklim := checkcharlim(addurl)
if !chkprx {
res = &Api{Cod: 400, Err: getloc("errfusei", "ja")}
}
if !chklim {
res = &Api{Cod: 400, Err: getloc("errcharlim", "ja")}
}
if chklim && chkprx {
chkfn, key := geturl(addurl, cnf.linkpath, true)
if chkfn != "" {
res = &Api{Cod: 200, Url: cnf.domain + "/" + key, Mot: addurl, New: false}
} else {
res = &Api{Cod: 200, Url: cnf.domain + "/" + insertjson(addurl, cnf.linkpath), Mot: addurl, New: true}
}
}
} else {
res = &Api{Cod: 400, Err: getloc("errurlent", "ja")}
}
}
}
buf, _ := json.MarshalIndent(res, "", " ")
_, _ = w.Write(buf)
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
data := &Page{Tit: "トップ", Ver: version}
data := &Page{Ver: version, Ves: strings.ReplaceAll(version, ".", "")}
uri := r.URL.Path
cookie, err := r.Cookie("lang")
if err != nil {
data.Lan = "ja"
@ -27,13 +92,11 @@ func serv (cnf Config, port int) {
data.Lan = cookie.Value
}
uri := r.URL.Path
query := r.URL.Query()
qnewurl := query.Get("newurl")
if data.Lan == "en" {
data.Tit = "Top"
}
tmpl := template.Must(template.ParseFiles(cnf.webpath + "/view/index.html", cnf.webpath + "/view/header.html", cnf.webpath + "/view/footer.html"))
// デフォルトページ=未検出
data.Tit = getloc("mikensyutu", data.Lan)
data.Err = getloc("errurlnai", data.Lan)
ftmpl[0] = cnf.webpath + "/view/404.html"
tmpl := template.Must(template.ParseFiles(ftmpl[0], ftmpl[1], ftmpl[2]))
if r.Method == "POST" {
err := r.ParseForm()
@ -43,103 +106,52 @@ func serv (cnf Config, port int) {
addurl := r.PostForm.Get("newadd")
chkprx := checkprefix(addurl)
chklim := checkcharlim(addurl)
if !chkprx {
if data.Lan == "ja" {
data.Tit = "不正なURL"
data.Err = "URLは「http://」又は「https://」で始めます。"
} else {
data.Tit = "Invalid URL"
data.Err = "The URL should start with \"http://\" or \"https://\"."
if !chkprx || !chklim {
data.Tit = getloc("fuseiurl", data.Lan)
if !chkprx {
data.Err = getloc("errfusei", data.Lan)
} else if !chklim {
data.Err = getloc("errcharlim", data.Lan)
}
tmpl = template.Must(template.ParseFiles(cnf.webpath + "/view/404.html", cnf.webpath + "/view/header.html", cnf.webpath + "/view/footer.html"))
}
if !chklim {
if data.Lan == "ja" {
data.Tit = "不正なURL"
data.Err = "URLは500文字以内です。"
} else {
data.Tit = "Invalid URL"
data.Err = "The URL should be less than 500 characters."
}
tmpl = template.Must(template.ParseFiles(cnf.webpath + "/view/404.html", cnf.webpath + "/view/header.html", cnf.webpath + "/view/footer.html"))
}
if chklim && chkprx {
} else {
chkfn, _ := geturl(addurl, cnf.linkpath, true)
if chkfn != "" {
http.Redirect(w, r, addurl, http.StatusSeeOther)
return
} else {
res := insertjson(addurl, cnf.linkpath)
data.Url = res
data.Url = insertjson(addurl, cnf.linkpath)
data.Dom = cnf.domain
if data.Lan == "ja" {
data.Tit = "短縮済み"
} else {
data.Tit = "Shortened"
}
tmpl = template.Must(template.ParseFiles(cnf.webpath + "/view/submitted.html", cnf.webpath + "/view/header.html", cnf.webpath + "/view/footer.html"))
data.Tit = getloc("tansyukuzumi", data.Lan)
ftmpl[0] = cnf.webpath + "/view/submitted.html"
}
}
} else {
if data.Lan == "ja" {
data.Tit = "未検出"
data.Err = "URLをご入力下さい。"
} else {
data.Tit = "Not found"
data.Err = "Please enter a URL."
}
tmpl = template.Must(template.ParseFiles(cnf.webpath + "/view/404.html", cnf.webpath + "/view/header.html", cnf.webpath + "/view/footer.html"))
data.Err = getloc("errurlent", data.Lan)
}
} 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: "/"})
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: "/"})
http.SetCookie(w, &http.Cookie{Name: "lang", Value: "ja", MaxAge: 31536000, Path: "/"})
}
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
} else {
if uri == "/" && qnewurl == "" {
tmpl = template.Must(template.ParseFiles(cnf.webpath + "/view/index.html", cnf.webpath + "/view/header.html", cnf.webpath + "/view/footer.html"))
} else if uri != "/" && qnewurl == "" {
} else { // r.Method == "GET"
if uri == "/" {
data.Tit = getloc("top", data.Lan)
ftmpl[0] = cnf.webpath + "/view/index.html"
} else {
red, _ := geturl(uri[1:], cnf.linkpath, false)
if red != "" {
http.Redirect(w, r, red, http.StatusSeeOther)
return
} else {
if data.Lan == "ja" {
data.Tit = "未検出"
data.Err = "このURLを見つけられませんでした。"
} else {
data.Tit = "Not found"
data.Err = "This URL could not be found."
}
tmpl = template.Must(template.ParseFiles(cnf.webpath + "/view/404.html", cnf.webpath + "/view/header.html", cnf.webpath + "/view/footer.html"))
}
} else if uri == "/" && qnewurl != "" {
data.Url = qnewurl
data.Dom = cnf.domain
if data.Lan == "ja" {
data.Tit = "短縮済み"
} else {
data.Tit = "Shortened"
}
tmpl = template.Must(template.ParseFiles(cnf.webpath + "/view/submitted.html", cnf.webpath + "/view/header.html", cnf.webpath + "/view/footer.html"))
} else {
if data.Lan == "ja" {
data.Tit = "未検出"
data.Err = "このURLを見つけられませんでした。"
} else {
data.Tit = "Not found"
data.Err = "This URL could not be found."
}
tmpl = template.Must(template.ParseFiles(cnf.webpath + "/view/404.html", cnf.webpath + "/view/header.html", cnf.webpath + "/view/footer.html"))
}
}
} // r.Method
tmpl = template.Must(template.ParseFiles(ftmpl[0], ftmpl[1], ftmpl[2]))
tmpl.Execute(w, data)
data = nil
})

ファイルの表示

@ -3,8 +3,6 @@
<p class="footer">
<a href="https://gitler.moe/suwako/urloli"><img src="/static/git.png" alt="Git" /></a> |
<a href="https://076.moe/"></a>
<br />
urloli-{{.Ver}}
</p>
</body>
</html>

ファイルの表示

@ -16,6 +16,7 @@
</h1>
<div class="body">
<p>
<a href="/">{{if eq .Lan "ja"}}トップ{{else}}Top{{end}}</a>
<a href="/">{{if eq .Lan "ja"}}トップ{{else}}Top{{end}}</a> |
<a href="https://technicalsuwako.moe/blog/urloli-{{.Ves}}/">urloli-{{.Ver}}</a>
</p>
{{end}}