From 48756a28100b3dec1fa383e3e7eb1fbebb87561b Mon Sep 17 00:00:00 2001 From: deepzz0 Date: Fri, 17 Feb 2017 23:35:51 +0800 Subject: [PATCH] generate by xml.go --- .gitignore | 5 +--- conf/tpl/crossdomainTpl.xml | 4 +++ conf/tpl/robotsTpl.xml | 3 +++ static/crossdomain.xml | 6 ++--- static/robots.txt | 3 --- xml.go | 54 ++++++++++++++++++++++++++++++++++--- 6 files changed, 60 insertions(+), 15 deletions(-) create mode 100644 conf/tpl/crossdomainTpl.xml create mode 100644 conf/tpl/robotsTpl.xml diff --git a/.gitignore b/.gitignore index 71eac3f..46a2ce5 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,4 @@ vendor vendor/** conf/ssl/domain.* eiblog -static/feed.xml -static/opensearch.xml -static/sitemap.xml - +static/*.* diff --git a/conf/tpl/crossdomainTpl.xml b/conf/tpl/crossdomainTpl.xml new file mode 100644 index 0000000..315e611 --- /dev/null +++ b/conf/tpl/crossdomainTpl.xml @@ -0,0 +1,4 @@ + + + + diff --git a/conf/tpl/robotsTpl.xml b/conf/tpl/robotsTpl.xml new file mode 100644 index 0000000..06c9129 --- /dev/null +++ b/conf/tpl/robotsTpl.xml @@ -0,0 +1,3 @@ +User-agent: * +Allow: / +Sitemap: https://{{.Domain}}/sitemap.xml diff --git a/static/crossdomain.xml b/static/crossdomain.xml index 527f133..dd1dc78 100644 --- a/static/crossdomain.xml +++ b/static/crossdomain.xml @@ -1,6 +1,4 @@ - - - - \ No newline at end of file + + diff --git a/static/robots.txt b/static/robots.txt index 4aa6d22..2b0c19b 100644 --- a/static/robots.txt +++ b/static/robots.txt @@ -1,6 +1,3 @@ User-agent: * Allow: / Sitemap: https://deepzz.com/sitemap.xml - -User-agent: MJ12bot -Disallow: / \ No newline at end of file diff --git a/xml.go b/xml.go index 4f8b48e..3a76b3f 100644 --- a/xml.go +++ b/xml.go @@ -25,9 +25,11 @@ func init() { if err != nil { logd.Fatal(err) } - doOpensearch() go doFeed() go doSitemap() + doOpensearch() + doRobots() + doCrossdomain() } func doFeed() { @@ -47,7 +49,7 @@ func doFeed() { "Artcs": artcs, } - f, err := os.OpenFile("static/feed.xml", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.ModePerm) + f, err := os.OpenFile("static/feed.xml", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666) if err != nil { logd.Error(err) return @@ -68,7 +70,7 @@ func doSitemap() { return } params := map[string]interface{}{"Artcs": Ei.Articles, "Domain": setting.Conf.Mode.Domain} - f, err := os.OpenFile("static/sitemap.xml", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.ModePerm) + f, err := os.OpenFile("static/sitemap.xml", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666) if err != nil { logd.Error(err) return @@ -93,7 +95,51 @@ func doOpensearch() { "SubTitle": Ei.SubTitle, "Domain": setting.Conf.Mode.Domain, } - f, err := os.OpenFile("static/opensearch.xml", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.ModePerm) + f, err := os.OpenFile("static/opensearch.xml", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666) + if err != nil { + logd.Error(err) + return + } + defer f.Close() + err = tpl.Execute(f, params) + if err != nil { + logd.Error(err) + return + } +} + +func doRobots() { + tpl := tpls.Lookup("robotsTpl.xml") + if tpl == nil { + logd.Error("not found robotsTpl.") + return + } + params := map[string]string{ + "Domain": setting.Conf.Mode.Domain, + } + f, err := os.OpenFile("static/robots.txt", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666) + if err != nil { + logd.Error(err) + return + } + defer f.Close() + err = tpl.Execute(f, params) + if err != nil { + logd.Error(err) + return + } +} + +func doCrossdomain() { + tpl := tpls.Lookup("crossdomainTpl.xml") + if tpl == nil { + logd.Error("not found crossdomainTpl.") + return + } + params := map[string]string{ + "Domain": setting.Conf.Mode.Domain, + } + f, err := os.OpenFile("static/crossdomain.xml", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666) if err != nil { logd.Error(err) return