Top

Advanced -HTTP service

Development for HTTP services with Z1h is a very simple thing

Start

at the command line, type

z1h -web 30030 

Above example can be opened at 30030 http service port

Can also be used to configure File (conf.json file after decompression)

z1h -conf conf.json

You can see the command line output

Initing z1h...
[Zwei.Ren/Web] Running on port :30030 with 8 routers.
[Zwei.Ren/Web] You may visit at 192.168.1.2:30030

Visit the following address :

http://127.0.0.1:30030

you can see

Hello World! -- ZweiRen.Web

File Structure

The file structure is automatically created under the datas directory of this directory. Some examples are as follows:

File name Use
api Storge interface file
assets Store some resource files
front Generally used to store static front-end files

Interface

The interface is in the datas/api directory, create a file with the suffix .z1h, you can create an interface for example:

// datas/api/hello.z1h
"Hello World!"

You can access http://127.0.0.1:30030/api/hello and fetch the return of the interface data

Request and Response

In the z1h file, each request will have a global variable this (also can be request), the type is * SimpleRouter, which contains all the request information of the http request and response method

// Request
print(this.Method) // request method, such as get/post/put/delete/options 
print(this.Request.URL.String()) // request path
print(this.GetGetParamMap()) // parameter in the url
print(this.GetHeaders()) // request header information
print(string(this.GetBody())) // request body 
print(string(this.GetPostParams())) // The parsed request body 
print(this.IP()) // requester IP
print(Object.keys(this.GetFileParams())) // Get the file(field name) in the request body 

// Response 
this.Redirect(302, "https://z1h.org/") // Request 302 redirect 
this.ServeFile("datas/favicon.ico") // respond to the file 
this.ResponseHeader("Content-Type", "text/plain") // set the response header 
this.ResponseStatus(201) // Set the response status code 
this.Tpl("hello", {title: "Template Test"}) // Response template file(views/hello.tpl) 

For specific usage, please refer to the bottom of the appendix/SimpleRouter

Router

You can create a directory under the datas/api directory, and then create a .z1h file under the directory, you can automatically speak the corresponding path of the HTTP request to this interface.

For example:

// datas/api/account/info.z1h file 
`Your IP: ${this.IP()}, browser: ${this.GetHeader("User-Agent")}` 

Now you can access http://127.0.0.1:30030/api/account/info to see your IP and browser UserAgent information

Default handler

creates index.z1h under thedatas / api directory (or sub-directory), which can handle the default path

eg:

// datas/api/account/index.z1h
`current time Is ${now()}`

You can visit http://127.0.0.1:30030/api/account/index to see the returned time information

You can also ignore the last index, directly visit http://127.0.0.1:30030/api/account/ see the same response data

The 404 page

You can creates 404.z1h under the datas/api directory (or subdirectory), which can handle requests when the requested address does not exist.

For example:

// datas/api/account/404.z1h file 
`You go the wrong way`

Access http://127.0.0.1:30030/api/account/xxx to See the 404 message

assets resources

assets directory is suitable for storing some static resources, such as jQuery.js, etc.

For example, if you copy the image to datas/assets/logo.png, you can use http://127.0.0.1:30030/assets/logo.png Get the picture.

Try to put a js file in it, then put an html file to reference it

front

front folder is used to deploy front-end frameworks such as VUE

  • Firstly make sure it is configured through z1h -conf started by way of file
  • The content corresponding to "key" in the configuration file is not empty, it is the key to update the front end, and the line is not commented
  • Export the dist directory (such as using npm run build)
  • The dist The directory is compressed into a zip file
  • Open http://127.0.0.1:30030/vueupdate
  • Upload dist.zip, enter the key, confirm

You can go to http://127.0.0.1:30030/ See the project you deployed

Appendix

SimpleRouter

structure

name type description example
Method string Request method(lowercase) if (this.method!='Post')panic("Post Only!")
DisableGZip bool Whether to try to gzip the result this.DisableGZip = false // Make sure not to compress
Writer ResponseWriter Include Write, WriteHeader, Header methods this.Writer.Write (bytearray ("nihao"))
Request *net/http.Request Go standard library structure this.Request.URL.String()

Method to get the request content

name description parameter return value example
IP Get Requester IP - string
GetGetParamMap Get all query parameters in url - map [string] string
GetGetParam Get a query parameter in url string field name string field value if (this.GetGetParam ("name") == "admin") return `Admin Hello`
GetGetIntParam Get a query integer parameter in the url string field name, int default value int parameter value
GetHeaders Get all the header information in the request - map [string] [] string
GetHeader Get a certain header information in the request string header key string header value if (this.GetHeader ("User -Agent "). Contains (" android ")) return `Hello Android user`
GetBody Get the original request body - [] byte return `Request body length $ {len (this.GetBody())}`
GetPostParams Get the deserialized request body content - map
GetPostParam Get request body parameters string field name field value
GetFileParams get file submitted by form - map [string] * MultipartFileData
GetFileParam get a file submitted by form string field name * MultipartFileData

response related method

name Description Parameters Return Value Example
Redirect Redirect int Status code, string link address - this.Redirect(302, "https://z1h.org")
ServeFile response file string file path - this.ServeFile ("datas / favicon.ico")
ResponseHeader set response header string header key, string header value - this.ResponseHeader ("Content-Type", "text / plain")
ResponseStatus Set response status code int status code - this.ResponseStatus (201)
Tpl Response template file string template name, template fill content , string sub-template … - this.Tpl ("hello", {title: "Template test "})