Use the Go language to view small features in a log set.

Source: Internet
Author: User
Tags sprintf
This is a creation in Article, where the information may have evolved or changed.

The program is divided into the site side and the center side (equivalent to a proxy).

This small function is mainly to solve, the program often let us go to drag the log, especially annoying. So make a small program. So that the program can go to the online view.

One problem is that the game server cannot be exposed, and there is a public network bandwidth that cannot occupy the business machine.

This small program is mainly the central side (proxy) to obtain the file through the intranet, and then forwarded to the access side.

The central end has two files, one is the program file. One is my file named JSON (mainly based on ID to identify the sub-station)

Main program file contents:


Package Mainimport ("Encoding/json" "Flag" "FMT" "io" "io/ioutil" "Net/http" "Net/rpc" "RegExp" "Strings" "Text/template" "Time") var port *string = flag. String ("L", "127.0.0.1:2789", "-l 127.0.0.1:2789") var Index stringvar Filelist stringtype Info struct {Name stringtime tim E.time}type newlist []*infotype pathlist struct {List newlistdir string}var configlist Map[string]stringvar re *regexp. Regexpfunc Main () {flag. Parse () Index = Fmt. Sprintf (' 


</pre><p></p><pre>
JSON file contents:

{"7400006": "192.168.80.247:6987", "7400007": "127.0.0.1:1987"}

Sub-station program:

Directory structure I'm using:--agent-|

---http.go

---sort.go

--maste.go

Http.go content:

Package Agentimport ("Errors" "FMT" "Net/http" "Net/rpc" "OS") var http_path stringtype info_list IntFunc httpserver (Port, Path string) {Http_path = Pathrpc. Register (New (info_list)) RPC. Handlehttp () http. Handle ("/", HTTP. Fileserver (http. Dir (path)) Err: = http. Listenandserve (FMT. Sprintf (port), nil) fmt. PRINTLN (Err)}func (i *info_list) list (path string, result *newlist) error {path_l: = Http_path + "/" + PathInfo, E: = OS.S TAT (path_l) if E! = Nil {return e}if info. Isdir () {L, _: = Getfilelist (path_l) *result = Lreturn Nil} else {return errors. New (Path)}}
Sort.go content:

Package Agentimport ("Io/ioutil" "Sort" "time") type Info struct {Name stringtime time. Time}type NewList []*infofunc getfilelist (Path string) (NewList, error) {L, err: = Ioutil. ReadDir (PATH) if err! = nil {return []*info{}, Err}var list []*infofor _, V: = range L {list = append (list, &info{v.nam E (), V.modtime ()})}sort.  Sort (NewList (list)) return list, Nil}func (i newlist) len () int {return Len (i)}func (i newlist) less (i, J int) bool {return I[i]. Time.unix () < i[j]. Time.unix ()}func (i newlist) Swap (i, J int) {I[i], i[j] = I[j], I[i]}

Maste.go content:

Package Mainimport ("Agent" "flag") Func main () {var port *string = flag. String ("L", ": 1789", "-l:1789 or-l 127.0.0.1:1789 is the default listener on Port 1789") var path *string = flag. String ("P", "./", "-p./or-P D:\\code") flag. Parse () Go agent. Httpserver (*port, *path) select {}}





var configlist map[string]stringvar re *regexp. Regexp

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

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.