Golang Log Center fetch log code sharing

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
Package Mainimport ("Archive/zip" "bytes" "Encoding/json" "FMT" "io" "io/ioutil" "Net" "Net/http" "OS" "Path/filepath" " Strings "" Time ") var tempdir string =" tmp "Const zone Int64 = +8func init () {OS. Mkdirall (TempDir, 0644)}const bodyType = "application/x-www-form-urlencoded" Func main () {go Server () var x job = Job{id: "C p_19216823 ", Name: []string{' Game/logs/log ', ' gate/logs/log ', ' game-fight ', ' mail/logs/log ', ' Room-manage/logs/log ', '/oem8.log '},path: ' C:\test\server ', Tag: ' GetFile '}b, _: = json. Marshal (x) buf: = bytes. Newreader (b) resp, err: = http. Post ("http://172.18.0.12:1789/", BodyType, BUF) if err! = Nil {fmt. PRINTLN (Err)}resp. Body.close ()}type job struct {Id string ' json:id ' Name []string ' json:name ' Path string ' json:path ' Tag string ' json : "Tag,omitempty" ' temp string ' json:tempfile '}func (self *job) Init () {self. Path = filepath. Toslash (self. Path) if!filepath. Hasprefix (self. Path, "/") {self. Path = self. Path + "/"}self.temp = TempDir + "/" + self. Id + "/" OS. Mkdirall (Self.temp, 0644)}fUNC (self *job) Copy () []string {self. Init () var list []stringfor _, V: = Range self. Name {v = filepath. Toslash (v) var path, DST Stringif filepath. Isabs (v) {list: = strings. Split (V, "/") If Len (list) <= 1 {continue}dst = self.temp + strings. Join (list[1:], "/") Err: = CopyFile (V, DST) if err! = Nil {list = append (list, v)}continue}path = self. Path + Vbasedir: = filepath. Toslash (filepath. Dir (v)) if basedir! = FilePath. Base (v) {OS. Mkdirall (Self.temp+basedir, 0644)}dst = self.temp + Verr: = CopyFile (path, DST) if err! = Nil {list = append (list, V) contin Ue}}return List}func (self *job) listfile () map[string][]string {self. Init () var m map[string][]string = Make (map[string][]string) for _, V: = Range self. Name {var list []STRINGV = filepath. Toslash (v) if filepath. Isabs (v) {_, Err: = OS. Stat (v) if err! = Nil {Continue}m[v] = append (list, v) Continue}path: = self. Path + vinfo, err: = OS. Stat (PATH) if err! = Nil {continue}if!info. Isdir () {list = append (list, path) M[path] = listcontinue}l, err: = Ioutil.ReadDir (PATH) if err! = Nil {continue}for _, Name: = Range L {list = append (list, name. Name ())}m[path] = List}return m}func (self *job) Run () {switch self. Tag {case ' getfile ': self. Copy () Err: = Zip (tempdir+ "/" +self. Id, tempdir+ "/" +self. id+ ". zip") if err! = Nil {fmt. PRINTLN (Err)}os. RemoveAll (TempDir + "/" + self.) Id) Case ' listfile ': self. ListFile ()}}func CopyFile (src, DST string) error {SFile, err: = OS. Open (SRC) if err! = Nil {return err}defer sfile.close () dfile, err: = OS. Create (DST) if err! = Nil {return err}defer dfile.close () io. Copy (Dfile, sFile) return Nil}func Zip (source, target string) error {ZipFile, err: = OS. Create (target) if err! = Nil {return Err}defer zipfile. Close () Archive: = Zip. Newwriter (ZipFile) defer archive. Close () return filepath. Walk (source, func (path string, info OS). FileInfo, err Error) error {if Err! = Nil {return Err}header, err: = Zip. Fileinfoheader (info) If err! = Nil {return err}if!info. Isdir () {header. Method = zip. Deflate}header. Setmodtime (time. Unix (info. Modtime (). Unix() + (zone*60*60), 0)) header. Name = Pathwriter, err: = archive. CreateHeader (header) If err! = Nil {return err}if info. Isdir () {return nil}file, err: = OS. Open (PATH) if err! = Nil {return Err}defer file. Close () _, err = Io. Copy (writer, file) return err})}func Route (W http. Responsewriter, R *http. Request) {defer r.body.close () IP, _, Err: = Net. Splithostport (R.REMOTEADDR) if err! = Nil {return}if!ipislanip (IP) {return}buf, err: = Ioutil. ReadAll (r.body) if err! = Nil {return}job: = new (JOB) Err = json. Unmarshal (buf, Job) if err! = Nil {return}job.run ()}func Server () {http. Handlefunc ("/", route) http. Listenandserve (": 1789", nil)}var (mask net. IPMask = net. Cidrmask (+) IP192 net. Ipnet = net. Ipnet{net. Parseip ("192.168.0.0"), mask}ip172 net. Ipnet = net. Ipnet{net. Parseip ("172.18.0.0"), MASK}IP10 net. Ipnet = net. Ipnet{net. Parseip ("10.0.0.0"), Mask}) Func Ipislanip (str string) bool {IP: = net. PARSEIP (str) return IP192. Contains (IP) | | IP172. Contains (IP) | | IP10. Contains (IP)}

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.