mirror of
https://github.com/geektutu/7days-golang.git
synced 2024-04-21 12:32:11 +00:00
50 lines
949 B
Go
50 lines
949 B
Go
package main
|
|
|
|
/*
|
|
(1)
|
|
$ curl -i http://localhost:9999/
|
|
HTTP/1.1 200 OK
|
|
Date: Mon, 12 Aug 2019 16:52:52 GMT
|
|
Content-Length: 18
|
|
Content-Type: text/html; charset=utf-8
|
|
<h1>Hello Gee</h1>
|
|
|
|
(2)
|
|
$ curl "http://localhost:9999/hello?name=geektutu"
|
|
hello geektutu, you're at /hello
|
|
|
|
(3)
|
|
$ curl "http://localhost:9999/login" -X POST -d 'username=geektutu&password=1234'
|
|
{"password":"1234","username":"geektutu"}
|
|
|
|
(4)
|
|
$ curl "http://localhost:9999/xxx"
|
|
404 NOT FOUND: /xxx
|
|
*/
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"./gee"
|
|
)
|
|
|
|
func main() {
|
|
r := gee.New()
|
|
r.GET("/", func(c *gee.Context) {
|
|
c.HTML(http.StatusOK, "<h1>Hello Gee</h1>")
|
|
})
|
|
r.GET("/hello", func(c *gee.Context) {
|
|
// expect /hello?name=geektutu
|
|
c.String(http.StatusOK, "hello %s, you're at %s\n", c.Query("name"), c.Path)
|
|
})
|
|
|
|
r.POST("/login", func(c *gee.Context) {
|
|
c.JSON(http.StatusOK, gee.H{
|
|
"username": c.PostForm("username"),
|
|
"password": c.PostForm("password"),
|
|
})
|
|
})
|
|
|
|
r.Run(":9999")
|
|
}
|