From 7860c21d65a6e96959ccb73e5021e5e128862ee4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AB=8F=E8=A8=AA=E5=AD=90?= Date: Mon, 8 Apr 2024 22:28:10 +0900 Subject: [PATCH] =?UTF-8?q?=E3=81=9D=E3=81=93=E3=82=82=E5=BE=93=E3=81=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/common.go | 12 +++++ go.mod | 2 +- main.go | 48 +++++++++++--------- config.go => src/config.go | 22 +++++----- getpage.go => src/getpage.go | 6 +-- mime.go => src/mime.go | 2 +- prep.go => src/prep.go | 8 ++-- scanpage.go => src/scanpage.go | 4 +- srv.go => src/srv.go | 80 ++++++++++++++++++++-------------- 9 files changed, 110 insertions(+), 74 deletions(-) create mode 100644 common/common.go rename config.go => src/config.go (75%) rename getpage.go => src/getpage.go (91%) rename mime.go => src/mime.go (96%) rename prep.go => src/prep.go (86%) rename scanpage.go => src/scanpage.go (98%) rename srv.go => src/srv.go (76%) diff --git a/common/common.go b/common/common.go new file mode 100644 index 0000000..9b50bf5 --- /dev/null +++ b/common/common.go @@ -0,0 +1,12 @@ +package common + +var sofname = "hozonsite" +var version = "1.2.0" + +func GetSofname() string { + return sofname +} + +func GetVersion() string { + return version +} diff --git a/go.mod b/go.mod index 1175266..a74783f 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module 076/hozonsite +module gitler.moe/suwako/hozonsite go 1.20 diff --git a/main.go b/main.go index efca5f9..9ef2235 100644 --- a/main.go +++ b/main.go @@ -5,34 +5,42 @@ import ( "os" "strings" "strconv" -) -var sofname = "hozonsite" -var version = "1.1.1" + "gitler.moe/suwako/hozonsite/src" + "gitler.moe/suwako/hozonsite/common" +) func help() { fmt.Println("使い方:") - fmt.Println(sofname + " -v :バージョンを表示") fmt.Println( - sofname + + common.GetSofname() + " -v :バージョンを表示", + ) + fmt.Println( + common.GetSofname() + " -s [ポート番号] :ポート番号でウェブサーバーを実行(デフォルト=9920)", ) - fmt.Println(sofname + " -h :ヘルプを表示") - fmt.Println(sofname + " :コマンドラインでウェブサイトを保存") + fmt.Println( + common.GetSofname() + + " -h :ヘルプを表示", + ) + fmt.Println( + common.GetSofname() + + " :コマンドラインでウェブサイトを保存", + ) } -func saveurlcmd(url string, cnf Config) { +func saveurlcmd(url string, cnf src.Config) { // 結局HTTPかHTTPSじゃないわね… - if !checkprefix(url) { + if !src.Checkprefix(url) { fmt.Println("URLは不正です。終了…") return } // パラメートルの文字(?、=等)を削除 - eurl := stripurl(url) + eurl := src.Stripurl(url) // 既に/usr/local/share/hozonsite/archiveに存在するかどうか - exist := checkexist(eurl, cnf.datapath) + exist := src.Checkexist(eurl, cnf.Datapath) // 既に存在したら、使う var confirm string @@ -44,26 +52,26 @@ func saveurlcmd(url string, cnf Config) { // 既に存在するページのURLを表示 for _, ex := range exist { - fmt.Println(strings.Replace(ex, cnf.datapath, cnf.domain, 1)) + fmt.Println(strings.Replace(ex, cnf.Datapath, cnf.Domain, 1)) } fmt.Scanf("%s", &confirm) } // 存在しない OR 「本当に手続きましょうか?」でYを入力した場合 if len(exist) == 0 || confirm == "y" || confirm == "Y" { - path := mkdirs(eurl, cnf.datapath) + path := src.Mkdirs(eurl, cnf.Datapath) // ページをダウンロード - getpage(url, path) + src.Getpage(url, path) // 色々の必須な編集 - scanpage(path, eurl, cnf.datapath) + src.Scanpage(path, eurl, cnf.Datapath) // 新しいURLを表示 - fmt.Println(cnf.domain + strings.Replace(path, cnf.datapath, "", 1)) + fmt.Println(cnf.Domain + strings.Replace(path, cnf.Datapath, "", 1)) } } func main() { // コンフィグファイル - cnf, err := getconf() + cnf, err := src.Getconf() if err != nil { fmt.Println(err) return @@ -75,10 +83,10 @@ func main() { if len(args) == 2 { // バージョンを表示 if args[1] == "-v" { - fmt.Println(sofname + "-" + version) + fmt.Println(common.GetSofname() + "-" + common.GetVersion()) return } else if args[1] == "-s" { // :9920でウェブサーバーを実行 - serv(cnf, 9920) + src.Serv(cnf, 9920) } else if args[1] == "-h" { // ヘルプを表示 help() return @@ -95,7 +103,7 @@ func main() { return } else { // OK、実行しよ〜 - serv(cnf, port) + src.Serv(cnf, port) } } else { // パラメートルは不明の場合、ヘルプを表示 diff --git a/config.go b/src/config.go similarity index 75% rename from config.go rename to src/config.go index 3ce6fe4..f9d69dd 100644 --- a/config.go +++ b/src/config.go @@ -1,4 +1,4 @@ -package main +package src import ( "os" @@ -10,12 +10,12 @@ import ( ) type Config struct { - configpath, webpath, datapath, domain, ip string + Configpath, Webpath, Datapath, Domain, IP string } var cnf Config -func getconf () (Config, error) { +func Getconf () (Config, error) { // バイナリ、データ、及びFreeBSDとNetBSDの場合、コンフィグ prefix := "/usr" // BSDだけはただの/usrではない @@ -26,22 +26,22 @@ func getconf () (Config, error) { } // コンフィグファイル - cnf.configpath = "/etc/hozonsite/config.json" - cnf.datapath = prefix + "/share/hozonsite" + cnf.Configpath = "/etc/hozonsite/config.json" + cnf.Datapath = prefix + "/share/hozonsite" // また、FreeBSDとNetBSDだけは違う場所だ。OpenBSDは正しい場所 // FreeBSD = /usr/local/etc/hozonsite/config.json // NetBSD = /usr/pkg/etc/hozonsite/config.json if runtime.GOOS == "freebsd" || runtime.GOOS == "netbsd" { - cnf.configpath = prefix + cnf.configpath + cnf.Configpath = prefix + cnf.Configpath } // コンフィグファイルがなければ、死ね - data, err := ioutil.ReadFile(cnf.configpath) + data, err := ioutil.ReadFile(cnf.Configpath) if err != nil { fmt.Println("confif.jsonを開けられません:", err) return cnf, errors.New( - "コンフィグファイルは " + cnf.configpath + " に創作して下さい。", + "コンフィグファイルは " + cnf.Configpath + " に創作して下さい。", ) } @@ -62,9 +62,9 @@ func getconf () (Config, error) { "mkdiorコマンドをつかって、 " + payload["webpath"].(string), ) } - cnf.webpath = payload["webpath"].(string) // データパス - cnf.domain = payload["domain"].(string) // ドメイン名 - cnf.ip = payload["ip"].(string) // IP + cnf.Webpath = payload["webpath"].(string) // データパス + cnf.Domain = payload["domain"].(string) // ドメイン名 + cnf.IP = payload["ip"].(string) // IP payload = nil // もういらなくなった return cnf, nil diff --git a/getpage.go b/src/getpage.go similarity index 91% rename from getpage.go rename to src/getpage.go index a053308..6c8460d 100644 --- a/getpage.go +++ b/src/getpage.go @@ -1,4 +1,4 @@ -package main +package src import ( "os" @@ -9,13 +9,13 @@ import ( ) // URLでパラメートル(?、=等)がある場合 -func stripurl (url string) string { +func Stripurl (url string) string { res := strings.ReplaceAll(url, "?", "") res = strings.ReplaceAll(res, "=", "") return res } -func getpage (url string, path string) { +func Getpage (url string, path string) { // ページを読み込む curl, err := http.Get(url) if err != nil { diff --git a/mime.go b/src/mime.go similarity index 96% rename from mime.go rename to src/mime.go index 8ebb15b..4d157e0 100644 --- a/mime.go +++ b/src/mime.go @@ -1,4 +1,4 @@ -package main +package src func getmime () map[string]string { return map[string]string { diff --git a/prep.go b/src/prep.go similarity index 86% rename from prep.go rename to src/prep.go index 4380a3b..aefe389 100644 --- a/prep.go +++ b/src/prep.go @@ -1,4 +1,4 @@ -package main +package src import ( "os" @@ -9,14 +9,14 @@ import ( ) // HTTPかHTTPSの確認 -func checkprefix (url string) bool { +func Checkprefix (url string) bool { return strings.HasPrefix( url, "http://") || strings.HasPrefix(url, "https://", ) } // ページは既に存在するの? -func checkexist (url string, prefix string) []string { +func Checkexist (url string, prefix string) []string { res, err := filepath.Glob(prefix + "/archive/*" + url2path(url)) if err != nil { fmt.Println("Err:", err) @@ -41,7 +41,7 @@ func url2path (url string) string { } // 必要なフォルダの創作 -func mkdirs (url string, prefix string) string { +func Mkdirs (url string, prefix string) string { rep := url2path(url) t := time.Now().Unix() diff --git a/scanpage.go b/src/scanpage.go similarity index 98% rename from scanpage.go rename to src/scanpage.go index e229789..331b154 100644 --- a/scanpage.go +++ b/src/scanpage.go @@ -1,4 +1,4 @@ -package main +package src import ( "os" @@ -12,7 +12,7 @@ import ( "path/filepath" ) -func scanpage (path string, domain string, thisdomain string) error { +func Scanpage (path string, domain string, thisdomain string) error { // 先に保存したページを読み込む fn, err := os.ReadFile(path + "/index.html") if err != nil { return err } diff --git a/srv.go b/src/srv.go similarity index 76% rename from srv.go rename to src/srv.go index ecf230d..502c31e 100644 --- a/srv.go +++ b/src/srv.go @@ -1,4 +1,4 @@ -package main +package src import ( "text/template" @@ -9,6 +9,8 @@ import ( "time" "os" "encoding/json" + + "gitler.moe/suwako/hozonsite/common" "gitler.moe/suwako/goliblocale" ) @@ -76,7 +78,7 @@ func handleStatic(path string, cnf Config, w http.ResponseWriter, r *http.Reques return } - fpath := cnf.datapath + "/archive/" + path + fpath := cnf.Datapath + "/archive/" + path http.ServeFile(w, r, fpath) } @@ -100,7 +102,7 @@ func handlePost(w http.ResponseWriter, r *http.Request, cnf Config) { var exist []string langu := initloc(r) - i18n, err := goliblocale.GetLocale(cnf.webpath + "/locale/" + langu) + i18n, err := goliblocale.GetLocale(cnf.Webpath + "/locale/" + langu) if err != nil { fmt.Printf("liblocaleエラー:%v", err) return @@ -108,34 +110,34 @@ func handlePost(w http.ResponseWriter, r *http.Request, cnf Config) { if r.PostForm.Get("hozonsite") == "" { data.Err = i18n["errfusei"] - ftmpl[0] = cnf.webpath + "/view/404.html" + ftmpl[0] = cnf.Webpath + "/view/404.html" return } url := r.PostForm.Get("hozonsite") // HTTPかHTTPSじゃない場合 - if !checkprefix(url) { + if !Checkprefix(url) { data.Err = i18n["errfuseiurl"] - ftmpl[0] = cnf.webpath + "/view/404.html" + ftmpl[0] = cnf.Webpath + "/view/404.html" return } - eurl := stripurl(url) - exist = checkexist(eurl, cnf.datapath) + eurl := Stripurl(url) + exist = Checkexist(eurl, cnf.Datapath) if len(exist) == 0 || r.PostForm.Get("agree") == "1" { - path := mkdirs(eurl, cnf.datapath) - getpage(url, path) - scanpage(path, eurl, cnf.datapath) + path := Mkdirs(eurl, cnf.Datapath) + Getpage(url, path) + Scanpage(path, eurl, cnf.Datapath) http.Redirect( w, r, - cnf.domain + strings.Replace(path, cnf.datapath, "", 1), + cnf.Domain + strings.Replace(path, cnf.Datapath, "", 1), http.StatusSeeOther, ) return } - ftmpl[0] = cnf.webpath + "/view/check.html" + ftmpl[0] = cnf.Webpath + "/view/check.html" data.Url = url var existing []Exist e := Exist{} @@ -149,7 +151,7 @@ func handlePost(w http.ResponseWriter, r *http.Request, cnf Config) { t := time.Unix(ti, 0) e.Date = t.Format("2006年01月02日 15:04:05") - e.Url = strings.Replace(ex, cnf.datapath, cnf.domain, 1) + e.Url = strings.Replace(ex, cnf.Datapath, cnf.Domain, 1) existing = append(existing, e) } @@ -160,22 +162,27 @@ func handlePost(w http.ResponseWriter, r *http.Request, cnf Config) { func siteHandler (cnf Config) func (http.ResponseWriter, *http.Request) { return func (w http.ResponseWriter, r *http.Request) { ftmpl = []string{ - cnf.webpath + "/view/index.html", - cnf.webpath + "/view/header.html", - cnf.webpath + "/view/footer.html", + cnf.Webpath + "/view/index.html", + cnf.Webpath + "/view/header.html", + cnf.Webpath + "/view/footer.html", + } + + version := common.GetVersion() + data = &Page{ + Ver: version, + Ves: strings.ReplaceAll(version, ".", ""), } - data = &Page{Ver: version, Ves: strings.ReplaceAll(version, ".", "")} lang := initloc(r) data.Lan = lang - i18n, err := goliblocale.GetLocale(cnf.webpath + "/locale/" + lang) + i18n, err := goliblocale.GetLocale(cnf.Webpath + "/locale/" + lang) if err != nil { fmt.Printf("liblocaleエラー:%v", err) return } data.i18n = i18n - ftmpl[0] = cnf.webpath + "/view/index.html" + ftmpl[0] = cnf.Webpath + "/view/index.html" tmpl := template.Must(template.ParseFiles(ftmpl[0], ftmpl[1], ftmpl[2])) if r.Method == "POST" { @@ -192,7 +199,13 @@ func apiHandler (cnf Config) func (http.ResponseWriter, *http.Request) { return 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}, "", " ") + version := common.GetVersion() + + buf, _ := json.MarshalIndent( + &Stat{Url: cnf.Domain, Ver: version}, + "", + " ", + ) _, _ = w.Write(buf) } } @@ -200,22 +213,25 @@ func apiHandler (cnf Config) func (http.ResponseWriter, *http.Request) { // /archive func archiveHandler (cnf Config) func (http.ResponseWriter, *http.Request) { return func (w http.ResponseWriter, r *http.Request) { + version := common.GetVersion() + ftmpl := []string{ - cnf.webpath + "/view/index.html", - cnf.webpath + "/view/header.html", - cnf.webpath + "/view/footer.html", + cnf.Webpath + "/view/index.html", + cnf.Webpath + "/view/header.html", + cnf.Webpath + "/view/footer.html", } + data := &Page{Ver: version, Ves: strings.ReplaceAll(version, ".", "")} lang := initloc(r) data.Lan = lang - i18n, err := goliblocale.GetLocale(cnf.webpath + "/locale/" + lang) + i18n, err := goliblocale.GetLocale(cnf.Webpath + "/locale/" + lang) if err != nil { fmt.Printf("liblocaleエラー:%v", err) return } data.i18n = i18n - ftmpl[0] = cnf.webpath + "/view/index.html" + ftmpl[0] = cnf.Webpath + "/view/index.html" tmpl := template.Must(template.ParseFiles(ftmpl[0], ftmpl[1], ftmpl[2])) path := strings.TrimPrefix(r.URL.Path, "/archive/") @@ -233,7 +249,7 @@ func archiveHandler (cnf Config) func (http.ResponseWriter, *http.Request) { pth += "index.html" } - file := cnf.datapath + pth + file := cnf.Datapath + pth if _, err := os.Stat(file); os.IsNotExist(err) { http.Redirect(w, r, "/404", http.StatusSeeOther) return @@ -247,7 +263,7 @@ func archiveHandler (cnf Config) func (http.ResponseWriter, *http.Request) { data.Body = string(bdy) tmpl = template.Must( - template.ParseFiles(cnf.webpath + "/view/archive.html"), + template.ParseFiles(cnf.Webpath + "/view/archive.html"), ) tmpl.Execute(w, data) data = nil @@ -255,11 +271,11 @@ func archiveHandler (cnf Config) func (http.ResponseWriter, *http.Request) { } // サーバー -func serv (cnf Config, port int) { +func Serv (cnf Config, port int) { http.Handle( "/static/", http.StripPrefix("/static/", - http.FileServer(http.Dir(cnf.webpath + "/static"))), + http.FileServer(http.Dir(cnf.Webpath + "/static"))), ) http.HandleFunc("/api/", apiHandler(cnf)) @@ -267,9 +283,9 @@ func serv (cnf Config, port int) { http.HandleFunc("/", siteHandler(cnf)) fmt.Println(fmt.Sprint( - "http://" + cnf.ip + ":", + "http://" + cnf.IP + ":", port, " でサーバーを実行中。終了するには、CTRL+Cを押して下さい。"), ) - http.ListenAndServe(fmt.Sprint(cnf.ip + ":", port), nil) + http.ListenAndServe(fmt.Sprint(cnf.IP + ":", port), nil) }