urloli/srv.go

149 行
5.5 KiB
Go
Raw 通常表示 履歴

2023-05-10 15:29:26 +09:00
package main
import (
"text/template"
"fmt"
"net/http"
)
type Page struct {
Tit string
Err string
Url string
Dom string
Lan string
2023-05-10 17:40:34 +09:00
Ver string
2023-05-10 15:29:26 +09:00
}
func serv (cnf Config, port int) {
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
cookie, err := r.Cookie("lang")
if err != nil {
http.SetCookie(w, &http.Cookie {Name: "lang", Value: "ja", MaxAge: 31536000, Path: "/"})
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
uri := r.URL.Path
query := r.URL.Query()
qnewurl := query.Get("newurl")
2023-05-10 17:40:34 +09:00
data := &Page{Tit: "トップ", Lan: cookie.Value, Ver: version}
2023-05-10 15:29:26 +09:00
if cookie.Value == "en" {
2023-05-10 17:35:18 +09:00
data.Tit = "Top"
2023-05-10 15:29:26 +09:00
}
tmpl := template.Must(template.ParseFiles(cnf.webpath + "/view/index.html", cnf.webpath + "/view/header.html", cnf.webpath + "/view/footer.html"))
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 {
if cookie.Value == "ja" {
2023-05-10 17:35:18 +09:00
data.Tit = "不正なURL"
data.Err = "URLは「http://」又は「https://」で始めます。"
2023-05-10 15:29:26 +09:00
} else {
2023-05-10 17:35:18 +09:00
data.Tit = "Invalid URL"
data.Err = "The URL should start with \"http://\" or \"https://\"."
2023-05-10 15:29:26 +09:00
}
tmpl = template.Must(template.ParseFiles(cnf.webpath + "/view/404.html", cnf.webpath + "/view/header.html", cnf.webpath + "/view/footer.html"))
}
if !chklim {
if cookie.Value == "ja" {
2023-05-10 17:35:18 +09:00
data.Tit = "不正なURL"
data.Err = "URLは500文字以内です。"
2023-05-10 15:29:26 +09:00
} else {
2023-05-10 17:35:18 +09:00
data.Tit = "Invalid URL"
data.Err = "The URL should be less than 500 characters."
2023-05-10 15:29:26 +09:00
}
tmpl = template.Must(template.ParseFiles(cnf.webpath + "/view/404.html", cnf.webpath + "/view/header.html", cnf.webpath + "/view/footer.html"))
}
if chklim && chkprx {
chkfn, _ := geturl(addurl, cnf.linkpath, true)
if chkfn != "" {
http.Redirect(w, r, addurl, http.StatusSeeOther)
return
} else {
res := insertjson(addurl, cnf.linkpath)
2023-05-10 17:35:18 +09:00
data.Url = res
data.Dom = cnf.domain
2023-05-10 15:29:26 +09:00
if cookie.Value == "ja" {
2023-05-10 17:35:18 +09:00
data.Tit = "短縮済み"
2023-05-10 15:29:26 +09:00
} else {
2023-05-10 17:35:18 +09:00
data.Tit = "Shortened"
2023-05-10 15:29:26 +09:00
}
tmpl = template.Must(template.ParseFiles(cnf.webpath + "/view/submitted.html", cnf.webpath + "/view/header.html", cnf.webpath + "/view/footer.html"))
}
}
} else {
if cookie.Value == "ja" {
2023-05-10 17:35:18 +09:00
data.Tit = "未検出"
data.Err = "URLをご入力下さい。"
2023-05-10 15:29:26 +09:00
} else {
2023-05-10 17:35:18 +09:00
data.Tit = "Not found"
data.Err = "Please enter a URL."
2023-05-10 15:29:26 +09:00
}
tmpl = template.Must(template.ParseFiles(cnf.webpath + "/view/404.html", cnf.webpath + "/view/header.html", cnf.webpath + "/view/footer.html"))
}
} else if r.PostForm.Get("langchange") != "" {
if cookie.Value == "ja" {
http.SetCookie(w, &http.Cookie {Name: "lang", Value: "en"})
} else {
http.SetCookie(w, &http.Cookie {Name: "lang", Value: "ja"})
}
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 == "" {
red, _ := geturl(uri[1:], cnf.linkpath, false)
if red != "" {
http.Redirect(w, r, red, http.StatusSeeOther)
return
} else {
if cookie.Value == "ja" {
2023-05-10 17:35:18 +09:00
data.Tit = "未検出"
data.Err = "このURLを見つけられませんでした。"
2023-05-10 15:29:26 +09:00
} else {
2023-05-10 17:35:18 +09:00
data.Tit = "Not found"
data.Err = "This URL could not be found."
2023-05-10 15:29:26 +09:00
}
tmpl = template.Must(template.ParseFiles(cnf.webpath + "/view/404.html", cnf.webpath + "/view/header.html", cnf.webpath + "/view/footer.html"))
}
} else if uri == "/" && qnewurl != "" {
2023-05-10 17:35:18 +09:00
data.Url = qnewurl
data.Dom = cnf.domain
if cookie.Value == "ja" {
data.Tit = "短縮済み"
} else {
data.Tit = "Shortened"
}
2023-05-10 15:29:26 +09:00
tmpl = template.Must(template.ParseFiles(cnf.webpath + "/view/submitted.html", cnf.webpath + "/view/header.html", cnf.webpath + "/view/footer.html"))
} else {
if cookie.Value == "ja" {
2023-05-10 17:35:18 +09:00
data.Tit = "未検出"
data.Err = "このURLを見つけられませんでした。"
2023-05-10 15:29:26 +09:00
} else {
2023-05-10 17:35:18 +09:00
data.Tit = "Not found"
data.Err = "This URL could not be found."
2023-05-10 15:29:26 +09:00
}
tmpl = template.Must(template.ParseFiles(cnf.webpath + "/view/404.html", cnf.webpath + "/view/header.html", cnf.webpath + "/view/footer.html"))
}
}
tmpl.Execute(w, data)
data = nil
})
fmt.Println(fmt.Sprint("http://127.0.0.1:", port, " でサーバーを実行中。終了するには、CTRL+Cを押して下さい。"))
http.ListenAndServe(fmt.Sprint(":", port), nil)
}