├── .gitignore ├── README.md ├── common_static_homepage.go ├── emlog_static_articles.go └── license.txt /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | go_static 2 | ==================== 3 | ###功能### 4 | 1. 新闻博客类非数据交互网站通用首页静态化 5 | 2. emlog博客系统文章页静态化 6 | 3. 使用Go语言编写,跨平台 7 | 8 | ###条件### 9 | 1. 需要有服务器管理权限,因为要编译、运行软件 10 | 2. 需要Go语言编译环境,请去[The Go Programming Language](http://golang.org/) 下载 11 | 12 | ###使用方法### 13 | 1. 两个文件单独编译: 14 | 15 | ` 16 | go build common_static_homepage.go 17 | ` 18 | 19 | ` 20 | go build emlog_static_articles 21 | ` 22 | 2. common_static_homepage `通用首页`静态化 使用方法为: 23 | 24 | ` 25 | ./common_static_homepage -url=http://example.com/index.php -t=30 26 | ` 27 | 28 | t 为间隔时间,单位是秒 29 | 30 | 3. emlog_static_articles `emlog文章页`静态化 使用方法为: 31 | 32 | ` 33 | ./emlog_static_articles -url=http://example.com/index.php -c=10 -n=100 34 | ` 35 | 36 | c 为并发数,n 为总文章数,即文章id最大值 37 | -------------------------------------------------------------------------------- /common_static_homepage.go: -------------------------------------------------------------------------------- 1 | /* 2 | Use: 3 | go build common_static_homepage.go 4 | ./common_static_homepage -url=http://example.com/index.php -t=30 5 | 6 | //请将上面的example.com替换成你自己的网址,注意后面要加上index.php 7 | //t的单位是秒,默认为一分钟 8 | 9 | //默认在网站根目录下编译运行,如需改动,可直接修改os.OpenFile第一个参数 10 | 11 | 12 | */ 13 | 14 | package main 15 | 16 | import ( 17 | "flag" 18 | "fmt" 19 | "net/http" 20 | "os" 21 | "time" 22 | ) 23 | 24 | func main() { 25 | var home_url = flag.String("url", "http://lvwenhan.com", "your url, include /index.php") 26 | var interval_time = flag.Int("t", 60, "interval time /s ") 27 | flag.Parse() 28 | tc := time.Tick(time.Second * time.Duration(*interval_time)) 29 | for i := 0; i < 5256000; i++ { 30 | <-tc 31 | action(*home_url) 32 | fmt.Println(i) 33 | } 34 | } 35 | 36 | func action(home_url string) { 37 | 38 | content, err := http.Get(home_url) 39 | 40 | defer content.Body.Close() 41 | 42 | if err != nil { 43 | fmt.Println(err) 44 | } else { 45 | buf := make([]byte, 10240) 46 | //createfile 47 | f, err1 := os.OpenFile("index.html", os.O_RDWR|os.O_CREATE|os.O_TRUNC, os.ModePerm) 48 | if err1 != nil { 49 | panic(err1) 50 | return 51 | } 52 | defer f.Close() 53 | 54 | for { 55 | n, _ := content.Body.Read(buf) 56 | if 0 == n { 57 | break 58 | } 59 | f.WriteString(string(buf[:n])) 60 | } 61 | } 62 | 63 | t := time.Now().Unix() 64 | 65 | fmt.Printf("%s\n", "首页生成成功! 在:"+time.Unix(t, 0).String()) 66 | 67 | } 68 | -------------------------------------------------------------------------------- /emlog_static_articles.go: -------------------------------------------------------------------------------- 1 | /* 2 | Use: 3 | go build emlog_static_articles.go 4 | ./emlog_static_articles -url=http://example.com/index.php -c=10 -n=100 5 | 6 | //请将上面的example.com替换成你自己的网址,注意后面要加上index.php 7 | //c为同时运行的协程数,n为文章总数 8 | 9 | //默认在网站根目录下编译运行,建议修改os.OpenFile第一个参数,全部归到static目录下,注意先创建此目录 10 | 11 | 12 | */ 13 | 14 | package main 15 | 16 | import ( 17 | "flag" 18 | "fmt" 19 | "net/http" 20 | "os" 21 | "strconv" 22 | ) 23 | 24 | func main() { 25 | 26 | var home_url = flag.String("url", "http://lvwenhan.com/blog/index.php", "your url, include /index.php") 27 | var xiancheng = flag.Int("c", 10, "interval time /s ") 28 | var wenzhangzongshu = flag.Int("n", 100, "interval time /s ") 29 | flag.Parse() 30 | 31 | var wenzhangshu int = *wenzhangzongshu 32 | var number int = *xiancheng 33 | var blog_url string = *home_url 34 | 35 | yushu := wenzhangshu % number 36 | beishu := wenzhangshu / number 37 | fmt.Println(beishu) 38 | for j := 0; j < beishu; j++ { 39 | do_100(number*j, number, blog_url) 40 | } 41 | 42 | do_100(number*beishu, yushu+1, blog_url) 43 | 44 | } 45 | 46 | func do_100(begin, number int, blog_url string) { 47 | 48 | chs := make([]chan int, number) 49 | 50 | for m := 0; m < len(chs); m++ { 51 | aid := begin + m 52 | chs[m] = make(chan int) 53 | go action(aid, chs[m], blog_url) 54 | 55 | } 56 | 57 | for _, ch := range chs { 58 | <-ch 59 | } 60 | 61 | } 62 | 63 | func action(aid int, ch chan int, blog_url string) { 64 | 65 | blog_url = blog_url + "?post=" + strconv.Itoa(aid) 66 | 67 | content, err := http.Get(blog_url) 68 | 69 | defer content.Body.Close() 70 | 71 | if err != nil { 72 | fmt.Println(err) 73 | } else { 74 | buf := make([]byte, 10240) 75 | //createfile 76 | f, err1 := os.OpenFile(strconv.Itoa(aid)+".html", os.O_RDWR|os.O_CREATE|os.O_TRUNC, os.ModePerm) 77 | if err1 != nil { 78 | panic(err1) 79 | return 80 | } 81 | defer f.Close() 82 | 83 | for { 84 | n, _ := content.Body.Read(buf) 85 | if 0 == n { 86 | break 87 | } 88 | f.WriteString(string(buf[:n])) 89 | } 90 | } 91 | 92 | ch <- aid * 2 93 | 94 | fmt.Printf("%s\n", "第"+strconv.Itoa(aid)+"篇文章生成成功") 95 | 96 | } 97 | -------------------------------------------------------------------------------- /license.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/johnlui/go_static/2c9393f3925375d6bd9f907e0510d2b580f034a7/license.txt --------------------------------------------------------------------------------