Golang Web Framework Tiny Go Web

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

Tiny Go web (TGW) is a very simple web framework, not even a framework. TGW does not intend to replace any framework, the birth of TGW because the author in the use of beego a sense of frustration, decided to re-write a suitable for their own website (private borrowing network, because the site is not completed the record, temporarily hosted in the US VPS reverse proxy to the Ucloud host, Access may have a certain delay), from the idea to complete a total of only one day, because it is enough to feel that it is sufficient, there is no continued to add new features.

Project Address: HTTP://GITHUB.COM/ICATTLECODER/TGW

Qiuck Start

Controller

The controller implements automatic route registration, for example with the following structure


The type Server struct {//member is determined by business logic, such as database connection information for MGO, etc.}func newserver (/* entry, e.g. read from config file */) *server {return &server{}}//to should be template index.html, return value data for render template func (S *server) Index () (Data map[string]interface{}) {data = map[string]interface{}{} Author: = author{Name: "Icattlecoder", Email: []string{"icattlecoder@gmail.com", "iwangming@hotmail.com" }, QQ: "405283013", Blog: "Http://blog.segmentfault.com/icattlecoder",} data["Author"] = author return}//because there is no json.html template, but has the data return value, this data will return the Func (S *server) JSON () (Data map[string]interface{}) in the format of the JSON string {data = map[string]interface{}{} Author: = author{Name: "Icattlecoder", Email: []string{] Icattlecoder@gmail.c Om "," iwangming@hotmail.com "}, QQ:" 405283013 ", Blog:" Http://blog.segmentfault.com/icattlecoder ",} data["Author"] = author return}//Here according to the request automatically parse out args//for example,/hello?msg=hello World's function can be resolved to testargs{msg: "Hello World"}//because there is no hello.html template, and there is no return value, you can passThe RW member in the ENV writes back data func (S *server) Hello (args Testargs, env TGW. reqenv) {env.rw. Write ([]byte (args. MSG)) Err = env. Session.set ("Key", args) if err! = Nil {log. PRINTLN (Err)}}func (S *server) Adminindex () {}

The following is the program startup code

func main() {    ser := controllers.NewServer()    t:=tgw.NewTGW()    log.Fatal(t.Register(&ser).Run(":8080"))}

The Register method of TGW automatically registers the following routes:

/hello       ===> func (s *Server) Hello(args TestArgs, env tgw.ReqEnv)/index       ===> func (s *Server) Index() (data map[string]interface{}) /Json        ===> func (s *Server) Json() (data map[string]interface{})/admin/index ===> func (s *Server) AdminIndex()

The handler function is localhost:8080/index service.Index that the localhost:8080/admin/index handler function isservice.AdminIndex

View

The view is placed by default in the View folder with the file name associated with the URL, for example: /index correspondingview/index.html
If a URL does not have a corresponding view, but its handler function has a return value, it returns the result of the JOSN serialization of the object.
The view can <include src="<src>" /> contain other files, such as the common header area, through directives.

Parameter resolution

Take the following code as an example:

type TestArgs struct {    Msg string}func (s *Server) Hello(args TestArgs, env tgw.ReqEnv)

For localhost:8080/Hello?msg=Hello world the request, TGW automatically recognizes and resolves the testargs variable based on the request method (post or get).
Types that are currently able to resolve automatically are int ,, string bool ,float64

Extended parameter Resolution

TGW self *Args -with parameter resolution, that is, the structure name conforms to *Args the parameters can be automatically resolved. If custom parsing is required, implement the parser interface

Session Support

The framework realizes a simple session management, basically satisfies the general requirement, if needs to use the session, the processing function must have a type of TGW. The reqenv parameter allows you to access the session through this function. In addition, the value of the session is stored by memcached, so the actual runtime requires a memecached service.

Custom output

If the function contains a type of TGW. The reqenv parameter, and no return value, can be written directly to the REQENV.RW to return the result.

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.