このコミットが含まれているのは:
守矢諏訪子 2023-05-14 11:37:15 +09:00
コミット 5bf5a468dc
4個のファイルの変更119行の追加63行の削除

ファイルの表示

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

2
go.mod
ファイルの表示

@ -1,3 +1,3 @@
module hozonsite
module 076/hozonsite
go 1.20

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

@ -0,0 +1,37 @@
package main
import (
"encoding/json"
"fmt"
)
func getlist (lang string) []byte {
var jloc = []byte(`{
"top": "トップ",
"errfuseiurl": "URLは「http://」又は「https://」で始めます。"
}`)
var eloc = []byte(`{
"top": "Top",
"errfuseiurl": "The URL should start with \"http://\" or \"https://\"."
}`)
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 ""
}

141
srv.go
ファイルの表示

@ -6,88 +6,107 @@ import (
"net/http"
"strings"
"os"
"encoding/json"
)
type Page struct {
Tit string
Err string
Lan string
Ver string
Ext []string // 既に存在する場合
Url string // 確認ページ用
Body string // 保存したページ用
type (
Page struct {
Tit string
Err string
Lan string
Ver string
Ves string
Ext []string // 既に存在する場合
Url string // 確認ページ用
Body string // 保存したページ用
}
Stat struct {
Url string
Ver string
}
)
func initloc (r *http.Request) string {
cookie, err := r.Cookie("lang")
if err != nil {
return "ja"
} else {
return cookie.Value
}
}
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"}
data := &Page{Ver: version, Ves: strings.ReplaceAll(version, ".", "")}
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("/archive", func(w http.ResponseWriter, r *http.Request) {
//lang := initloc(r)
pth := r.URL.Path
if !strings.HasSuffix(pth, "/") && !strings.HasSuffix(pth, "index.html") {
pth += "/index.html"
} else if strings.HasSuffix(pth, "/") && !strings.HasSuffix(pth, "index.html") {
pth += "index.html"
}
bdy, err := os.ReadFile(cnf.datapath + pth)
if err != nil {
http.Redirect(w, r, "/404", http.StatusSeeOther)
return
}
data.Body = string(bdy)
tmpl := template.Must(template.ParseFiles(cnf.webpath + "/view/archive.html"))
tmpl.Execute(w, data)
data = nil
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
data := &Page{Tit: "トップ", Ver: version}
cookie, err := r.Cookie("lang")
if err != nil {
data.Lan = "ja"
} else {
data.Lan = cookie.Value
}
if data.Lan == "en" {
data.Tit = "Top"
}
lang := initloc(r)
data.Tit = getloc("top", lang)
data.Lan = lang
ftmpl[0] = cnf.webpath + "/view/index.html"
tmpl := template.Must(template.ParseFiles(ftmpl[0], ftmpl[1], ftmpl[2]))
if strings.HasPrefix(r.URL.Path, "/archive") {
pth := r.URL.Path
if !strings.HasSuffix(pth, "/") && !strings.HasSuffix(pth, "index.html") {
pth += "/index.html"
} else if strings.HasSuffix(pth, "/") && !strings.HasSuffix(pth, "index.html") {
pth += "index.html"
}
bdy, err := os.ReadFile(cnf.datapath + pth)
if err != nil {
http.Redirect(w, r, "/404", http.StatusSeeOther)
if r.Method == "POST" {
err := r.ParseForm()
if err != nil { fmt.Println(err) }
// クッキー
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: "/"})
}
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
data.Body = string(bdy)
tmpl = template.Must(template.ParseFiles(cnf.webpath + "/view/archive.html"))
} else {
if r.Method == "POST" {
err := r.ParseForm()
if err != nil { fmt.Println(err) }
// クッキー
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: "/"})
}
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
if r.PostForm.Get("hozonsite") != "" {
url := r.PostForm.Get("hozonsite")
// HTTPかHTTPSじゃない場合
if !checkprefix(url) {
if data.Lan == "ja" {
data.Err = "URLは「http://」又は「https://」で始めます。"
} else {
data.Err = "The URL should start with \"http://\" or \"https://\"."
}
ftmpl[0] = cnf.webpath + "/view/404.html"
tmpl = template.Must(template.ParseFiles(ftmpl[0], ftmpl[1], ftmpl[2]))
} else {
//if r.PostForm.Get("sosin") != "" {}
}
if r.PostForm.Get("hozonsite") != "" {
url := r.PostForm.Get("hozonsite")
// HTTPかHTTPSじゃない場合
if !checkprefix(url) {
data.Err = getloc("errfuseiurl", lang)
ftmpl[0] = cnf.webpath + "/view/404.html"
} else {
//if r.PostForm.Get("sosin") != "" {}
}
}
}
tmpl = template.Must(template.ParseFiles(ftmpl[0], ftmpl[1], ftmpl[2]))
tmpl.Execute(w, data)
data = nil
})
fmt.Println(fmt.Sprint("http://127.0.0.1:", port, " でサーバーを実行中。終了するには、CTRL+Cを押して下さい。"))