2023-05-10 15:29:26 +09:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"text/template"
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
2023-05-13 13:15:09 +09:00
|
|
|
|
"encoding/json"
|
2023-05-13 14:17:07 +09:00
|
|
|
|
"strings"
|
2023-07-04 10:03:52 +09:00
|
|
|
|
"log"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
2023-07-17 02:28:52 +09:00
|
|
|
|
"gitler.moe/suwako/goliblocale"
|
2023-05-10 15:29:26 +09:00
|
|
|
|
)
|
|
|
|
|
|
2023-05-13 13:15:09 +09:00
|
|
|
|
type (
|
|
|
|
|
Page struct {
|
2023-07-17 02:28:52 +09:00
|
|
|
|
Tit, Err, Url, Dom, Lan, Ver, Ves string
|
|
|
|
|
i18n map[string]string
|
2023-05-13 13:15:09 +09:00
|
|
|
|
}
|
|
|
|
|
Api struct {
|
|
|
|
|
Cod int `json:"code"`
|
|
|
|
|
Err string `json:"error"`
|
|
|
|
|
Url string `json:"url"`
|
|
|
|
|
Mot string `json:"origin"`
|
|
|
|
|
New bool `json:"isnew"`
|
|
|
|
|
}
|
2023-05-13 14:17:07 +09:00
|
|
|
|
Stat struct {
|
|
|
|
|
Url string `json:"url"`
|
|
|
|
|
Ver string `json:"version"`
|
|
|
|
|
}
|
2023-05-13 13:15:09 +09:00
|
|
|
|
)
|
2023-05-10 15:29:26 +09:00
|
|
|
|
|
2023-07-17 02:28:52 +09:00
|
|
|
|
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"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-10 15:29:26 +09:00
|
|
|
|
func serv (cnf Config, port int) {
|
2023-07-04 10:03:52 +09:00
|
|
|
|
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
err = os.Chdir(dir)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-06 15:34:59 +09:00
|
|
|
|
http.Handle(
|
|
|
|
|
"/static/",
|
|
|
|
|
http.StripPrefix("/static/", http.FileServer(http.Dir(cnf.webpath + "/static"))),
|
|
|
|
|
)
|
|
|
|
|
ftmpl := []string{
|
|
|
|
|
cnf.webpath + "/view/index.html",
|
|
|
|
|
cnf.webpath + "/view/header.html",
|
|
|
|
|
cnf.webpath + "/view/footer.html",
|
|
|
|
|
}
|
2023-05-10 15:29:26 +09:00
|
|
|
|
|
2023-05-13 13:15:09 +09:00
|
|
|
|
http.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
|
|
|
|
w.WriteHeader(200)
|
2023-05-13 14:17:07 +09:00
|
|
|
|
buf, _ := json.MarshalIndent(&Stat{Url: cnf.domain, Ver: version}, "", " ")
|
2023-05-13 13:15:09 +09:00
|
|
|
|
_, _ = w.Write(buf)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
http.HandleFunc("/api/lolify", func(w http.ResponseWriter, r *http.Request) {
|
2023-07-17 02:28:52 +09:00
|
|
|
|
lang := initloc(r)
|
2023-07-18 13:54:18 +09:00
|
|
|
|
i18n, err := goliblocale.GetLocale(cnf.webpath + "/locale/" + lang)
|
2023-07-17 02:28:52 +09:00
|
|
|
|
if err != nil {
|
2024-06-06 15:34:59 +09:00
|
|
|
|
fmt.Printf("liblocaleエラー:%v", err)
|
2023-07-17 02:28:52 +09:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-13 13:15:09 +09:00
|
|
|
|
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)
|
2023-05-13 13:40:41 +09:00
|
|
|
|
res.Err = "失敗"
|
2023-05-13 13:15:09 +09:00
|
|
|
|
} else {
|
|
|
|
|
if r.PostForm.Get("url") != "" {
|
|
|
|
|
addurl := r.PostForm.Get("url")
|
|
|
|
|
chkprx := checkprefix(addurl)
|
|
|
|
|
chklim := checkcharlim(addurl)
|
|
|
|
|
if !chkprx {
|
2023-07-17 02:28:52 +09:00
|
|
|
|
res = &Api{Cod: 400, Err: i18n["errfusei"]}
|
2023-05-13 13:15:09 +09:00
|
|
|
|
}
|
|
|
|
|
if !chklim {
|
2023-07-17 02:28:52 +09:00
|
|
|
|
res = &Api{Cod: 400, Err: i18n["errcharlim"]}
|
2023-05-13 13:15:09 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if chklim && chkprx {
|
|
|
|
|
chkfn, key := geturl(addurl, cnf.linkpath, true)
|
|
|
|
|
if chkfn != "" {
|
2024-06-06 15:34:59 +09:00
|
|
|
|
res = &Api{
|
|
|
|
|
Cod: 200,
|
|
|
|
|
Url: cnf.domain + "/" + key,
|
|
|
|
|
Mot: addurl,
|
|
|
|
|
New: false,
|
|
|
|
|
}
|
2023-05-13 13:15:09 +09:00
|
|
|
|
} else {
|
2024-06-06 15:34:59 +09:00
|
|
|
|
res = &Api{
|
|
|
|
|
Cod: 200,
|
|
|
|
|
Url: cnf.domain + "/" + insertjson(addurl, cnf.linkpath),
|
|
|
|
|
Mot: addurl,
|
|
|
|
|
New: true,
|
|
|
|
|
}
|
2023-05-13 13:15:09 +09:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2023-07-17 02:28:52 +09:00
|
|
|
|
res = &Api{Cod: 400, Err: i18n["errurlent"]}
|
2023-05-13 13:15:09 +09:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buf, _ := json.MarshalIndent(res, "", " ")
|
|
|
|
|
_, _ = w.Write(buf)
|
|
|
|
|
})
|
|
|
|
|
|
2023-05-10 15:29:26 +09:00
|
|
|
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
2023-05-13 14:17:07 +09:00
|
|
|
|
data := &Page{Ver: version, Ves: strings.ReplaceAll(version, ".", "")}
|
2023-05-13 13:40:41 +09:00
|
|
|
|
uri := r.URL.Path
|
2023-07-17 02:28:52 +09:00
|
|
|
|
lang := initloc(r)
|
2023-05-13 13:40:41 +09:00
|
|
|
|
|
2023-07-18 13:54:18 +09:00
|
|
|
|
i18n, err := goliblocale.GetLocale(cnf.webpath + "/locale/" + lang)
|
2023-05-10 15:29:26 +09:00
|
|
|
|
if err != nil {
|
2024-06-06 15:34:59 +09:00
|
|
|
|
fmt.Printf("liblocaleエラー:%v", err)
|
2023-07-17 02:28:52 +09:00
|
|
|
|
return
|
2023-05-10 15:29:26 +09:00
|
|
|
|
}
|
2023-07-17 02:28:52 +09:00
|
|
|
|
data.i18n = i18n
|
|
|
|
|
data.Lan = lang
|
2023-05-10 15:29:26 +09:00
|
|
|
|
|
2023-05-13 13:40:41 +09:00
|
|
|
|
// デフォルトページ=未検出
|
2023-07-17 02:28:52 +09:00
|
|
|
|
data.Tit = i18n["mikensyutu"]
|
|
|
|
|
data.Err = i18n["errurlnai"]
|
2023-05-13 13:40:41 +09:00
|
|
|
|
ftmpl[0] = cnf.webpath + "/view/404.html"
|
2023-05-13 12:17:41 +09:00
|
|
|
|
tmpl := template.Must(template.ParseFiles(ftmpl[0], ftmpl[1], ftmpl[2]))
|
2023-05-10 15:29:26 +09:00
|
|
|
|
|
|
|
|
|
if r.Method == "POST" {
|
|
|
|
|
err := r.ParseForm()
|
|
|
|
|
if err != nil { fmt.Println(err) }
|
|
|
|
|
if r.PostForm.Get("sosin") != "" {
|
|
|
|
|
if r.PostForm.Get("newadd") != "" {
|
|
|
|
|
addurl := r.PostForm.Get("newadd")
|
|
|
|
|
chkprx := checkprefix(addurl)
|
|
|
|
|
chklim := checkcharlim(addurl)
|
2023-05-13 13:40:41 +09:00
|
|
|
|
if !chkprx || !chklim {
|
2023-07-17 02:28:52 +09:00
|
|
|
|
data.Tit = i18n["fuseiurl"]
|
2023-05-13 13:40:41 +09:00
|
|
|
|
if !chkprx {
|
2023-07-17 02:28:52 +09:00
|
|
|
|
data.Err = i18n["errfusei"]
|
2023-05-13 13:40:41 +09:00
|
|
|
|
} else if !chklim {
|
2023-07-17 02:28:52 +09:00
|
|
|
|
data.Err = i18n["errcharlim"]
|
2023-05-13 13:40:41 +09:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2023-05-10 15:29:26 +09:00
|
|
|
|
chkfn, _ := geturl(addurl, cnf.linkpath, true)
|
|
|
|
|
if chkfn != "" {
|
|
|
|
|
http.Redirect(w, r, addurl, http.StatusSeeOther)
|
|
|
|
|
return
|
|
|
|
|
} else {
|
2023-05-13 13:15:09 +09:00
|
|
|
|
data.Url = insertjson(addurl, cnf.linkpath)
|
2023-05-10 17:35:18 +09:00
|
|
|
|
data.Dom = cnf.domain
|
2023-07-17 02:28:52 +09:00
|
|
|
|
data.Tit = i18n["tansyukuzumi"]
|
2023-05-13 12:17:41 +09:00
|
|
|
|
ftmpl[0] = cnf.webpath + "/view/submitted.html"
|
2023-05-10 15:29:26 +09:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2023-07-17 02:28:52 +09:00
|
|
|
|
data.Err = i18n["errurlent"]
|
2023-05-10 15:29:26 +09:00
|
|
|
|
}
|
|
|
|
|
} else if r.PostForm.Get("langchange") != "" {
|
2023-07-17 02:28:52 +09:00
|
|
|
|
lang := r.PostForm.Get("lang")
|
2024-06-06 15:34:59 +09:00
|
|
|
|
http.SetCookie(
|
|
|
|
|
w,
|
|
|
|
|
&http.Cookie{Name: "lang", Value: lang, MaxAge: 31536000, Path: "/"},
|
|
|
|
|
)
|
2023-05-10 15:29:26 +09:00
|
|
|
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-05-13 13:15:09 +09:00
|
|
|
|
} else { // r.Method == "GET"
|
2023-05-13 13:40:41 +09:00
|
|
|
|
if uri == "/" {
|
2023-07-17 02:28:52 +09:00
|
|
|
|
data.Tit = i18n["top"]
|
2023-05-13 12:17:41 +09:00
|
|
|
|
ftmpl[0] = cnf.webpath + "/view/index.html"
|
2023-05-13 13:40:41 +09:00
|
|
|
|
} else {
|
2023-05-10 15:29:26 +09:00
|
|
|
|
red, _ := geturl(uri[1:], cnf.linkpath, false)
|
|
|
|
|
if red != "" {
|
|
|
|
|
http.Redirect(w, r, red, http.StatusSeeOther)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-13 13:15:09 +09:00
|
|
|
|
} // r.Method
|
2023-05-10 15:29:26 +09:00
|
|
|
|
|
2023-05-13 13:40:41 +09:00
|
|
|
|
tmpl = template.Must(template.ParseFiles(ftmpl[0], ftmpl[1], ftmpl[2]))
|
2023-05-10 15:29:26 +09:00
|
|
|
|
tmpl.Execute(w, data)
|
|
|
|
|
data = nil
|
|
|
|
|
})
|
|
|
|
|
|
2024-06-06 15:34:59 +09:00
|
|
|
|
fmt.Println(fmt.Sprint(
|
|
|
|
|
"http://" + cnf.ip + ":",
|
|
|
|
|
port,
|
|
|
|
|
" でサーバーを実行中。終了するには、CTRL+Cを押して下さい。",
|
|
|
|
|
))
|
2023-07-17 02:28:52 +09:00
|
|
|
|
http.ListenAndServe(fmt.Sprint(cnf.ip + ":", port), nil)
|
2023-05-10 15:29:26 +09:00
|
|
|
|
}
|