そこも従い

このコミットが含まれているのは:
守矢諏訪子 2024-04-08 22:28:10 +09:00
コミット 7860c21d65
9個のファイルの変更110行の追加74行の削除

12
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
}

2
go.mod
ファイルの表示

@ -1,4 +1,4 @@
module 076/hozonsite
module gitler.moe/suwako/hozonsite
go 1.20

48
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 + " <URL> :コマンドラインでウェブサイトを保存")
fmt.Println(
common.GetSofname() +
" -h :ヘルプを表示",
)
fmt.Println(
common.GetSofname() +
" <URL> :コマンドラインでウェブサイトを保存",
)
}
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 {
// パラメートルは不明の場合、ヘルプを表示

ファイルの表示

@ -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

ファイルの表示

@ -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 {

ファイルの表示

@ -1,4 +1,4 @@
package main
package src
func getmime () map[string]string {
return map[string]string {

ファイルの表示

@ -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()

ファイルの表示

@ -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 }

ファイルの表示

@ -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)
}