urloli/srv.go

221 lines
5.7 KiB
Go
Raw Normal View History

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"
"gitler.moe/suwako/goliblocale"
2023-05-10 15:29:26 +09:00
)
2023-05-13 13:15:09 +09:00
type (
Page struct {
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
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)
}
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) {
lang := initloc(r)
2023-07-18 13:54:18 +09:00
i18n, err := goliblocale.GetLocale(cnf.webpath + "/locale/" + lang)
if err != nil {
fmt.Printf("liblocaleエラー%v", err)
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)
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 {
res = &Api{Cod: 400, Err: i18n["errfusei"]}
2023-05-13 13:15:09 +09:00
}
if !chklim {
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 != "" {
res = &Api{
Cod: 200,
Url: cnf.domain + "/" + key,
Mot: addurl,
New: false,
}
2023-05-13 13:15:09 +09:00
} else {
res = &Api{
Cod: 200,
Url: cnf.domain + "/" + insertjson(addurl, cnf.linkpath),
Mot: addurl,
New: true,
}
2023-05-13 13:15:09 +09:00
}
}
} else {
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, ".", "")}
uri := r.URL.Path
lang := initloc(r)
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 {
fmt.Printf("liblocaleエラー%v", err)
return
2023-05-10 15:29:26 +09:00
}
data.i18n = i18n
data.Lan = lang
2023-05-10 15:29:26 +09:00
// デフォルトページ=未検出
data.Tit = i18n["mikensyutu"]
data.Err = i18n["errurlnai"]
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)
if !chkprx || !chklim {
data.Tit = i18n["fuseiurl"]
if !chkprx {
data.Err = i18n["errfusei"]
} else if !chklim {
data.Err = i18n["errcharlim"]
}
} 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
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 {
data.Err = i18n["errurlent"]
2023-05-10 15:29:26 +09:00
}
} else if r.PostForm.Get("langchange") != "" {
lang := r.PostForm.Get("lang")
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"
if uri == "/" {
data.Tit = i18n["top"]
2023-05-13 12:17:41 +09:00
ftmpl[0] = cnf.webpath + "/view/index.html"
} 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
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
})
fmt.Println(fmt.Sprint(
"http://" + cnf.ip + ":",
port,
" でサーバーを実行中。終了するには、CTRL+Cを押して下さい。",
))
http.ListenAndServe(fmt.Sprint(cnf.ip + ":", port), nil)
2023-05-10 15:29:26 +09:00
}