Use go to press the boss to write a gadget, use his script to download log files, and then let the development of their own to download

Source: Internet
Author: User

Package Mainimport ("Archive/tar" "Bufio" "bytes" "Compress/gzip" "Flag" "FMT" "io" "net/http" "OS" "Os/exec" "RegExp" " Sort "" "Text/template" "Time") type Id_info map[string]stringvar Config *id_infovar re *regexp. Regexpfunc Main () {listen: = flag. String ("L", ": 1789", "-l= 127.0.0.1:1789") Config: = flag. String ("C", "CFG", "-c=cfg") flag. Parse () Config = Parseconfig (*config) Re, _ = Regexp.compile ("[0-9]{7}$") http. Handle ("/download/", http. Fileserver (http. Dir ("./"))) HTTP. Handlefunc ("/", router) http. Listenandserve (*listen, nil)}func Router (w http. Responsewriter, R *http. Request) {if r.url. Path = = "/" {M: = template. New ("") M.parse (Index) M.execute (W, Config.sort ()) return}if s: = Re. FindString (R.url. Path); Len (s)! = 0 {IP, x: = Config.map () [S]if x {If _, E: = os. Stat (s); E = = Nil {if list: = Parse (R); Len (list) > 0 {var tar_list []stringfor _, V: = range List {desc: = fmt. Sprintf ("Download/%s/%s_%s.log", S, time. Now (). Format ("20060102"), v) tar_list = append (tar_list, desc) excute (IP, V, desc)}zip (s, tar_lIST) w.write ([]byte (FMT. Sprintf (": http://%s/download/%s/%s.tar.gz", R.host, S, time.) Now (). Format ("20060102"))) Return}w.write ([]byte (Package_page)})} else {if list: = Parse (R); Len (list) > 0 {os. Mkdirall (FMT. Sprintf ("download/%s", s), 0644) var tar_list []stringfor _, V: = range List {desc: = fmt. Sprintf ("Download/%s/%s_%s.log", S, time. Now (). Format ("20060102"), v) tar_list = append (tar_list, desc) excute (IP, V, desc)}zip (S, tar_list) w.write ([]byte] (FMT. Sprintf (": http://%s/download/%s/%s.tar.gz", R.host, S, time.) Now (). Format ("20060102"))) Return}w.write ([]byte (Package_page))}return}w.write ([]byte (FMT. Sprintf ("%s does not exist", s))) Return}}func Excute (IP, processname, name string) {FMT. Printf ("Ton%s:/data/gamehome/server/%s/nohub.out%s\n", IP, processname, name) cmd: = Exec.command ("Ton", FMT. Sprintf ("%s:/data/gamehome/server/%s/nohub.out", IP, processname), name) cmd. Run ()}func parseconfig (path string) *id_info {File, err: = OS. Open (path) defer file.close () if err! = Nil {panic (err)}result: = Make (Id_info) B: = Bufio. Newreader (File) for {line, _, E: = B.readline () if E! = Nil {if E = = io. EOF {break}panic (e)}list: = bytes. Split (line, []byte{58}) result[string (list[0]) "= string (List[1])}return &result}func (self *id_info) Map () Id_info {return *self}func (self *id_info) Sort () []string {var list []stringfor K, _: = Range *self {list = append (list, k)}sort. Strings (list) return List}func parse (R *http. Request) []string {var list []stringfor K, _: = Range R.url. Query () {list = append (list, k)}return list}func Zip (id string, list []string) {File, _: = OS. OpenFile (FMT. Sprintf ("download/%s/%s.tar.gz", ID, time. Now (). Format ("20060102")), OS. O_rdwr|os. O_create|os. O_trunc, 0644) defer file.close () gzip: = gzip. Newwriter (File) defer gzip.close () tar: = tar. Newwriter (Gzip) defer tar.close () for _, V: = Range list {T, err: = OS. Open (v) if err! = Nil {fmt. Printf ("Compressed file:%s\n", v) fmt. PRINTLN (Err) Continue}defer T.close () Head: = new (tar. Header) Fileinfo, _: = T.stat () head. Name = Fileinfo.name () head. Size =Fileinfo.size () head. Mode = Int64 (Fileinfo.mode ()) head. Modtime = Fileinfo.modtime () tar.writeheader (head) IO. Copy (Tar, T)}return}const Index = ' 

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Use go to press the boss to write a gadget, use his script to download log files, and then let the development of their own to download

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.