feat: add simple readme
This commit is contained in:
parent
f0fc5fa44f
commit
7c03a40ef0
43
app.go
43
app.go
@ -1,8 +1,11 @@
|
||||
package nf
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
@ -12,6 +15,7 @@ type App struct {
|
||||
config *Config
|
||||
router *router
|
||||
groups []*RouterGroup
|
||||
server *http.Server
|
||||
}
|
||||
|
||||
func (a *App) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
||||
@ -36,9 +40,42 @@ func (a *App) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) Run(address string) error {
|
||||
func (a *App) run(ln net.Listener) error {
|
||||
if !a.config.DisableBanner {
|
||||
fmt.Println(banner + "nf serve at: " + address + "\n")
|
||||
fmt.Println(banner + "nf serve at: " + a.server.Addr + "\n")
|
||||
}
|
||||
return http.ListenAndServe(address, a)
|
||||
|
||||
return a.server.Serve(ln)
|
||||
}
|
||||
|
||||
func (a *App) Run(address string) error {
|
||||
ln, err := net.Listen("tcp", address)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
a.server = &http.Server{}
|
||||
|
||||
return a.run(ln)
|
||||
}
|
||||
|
||||
func (a *App) RunTLS(address string, tlsConfig *tls.Config) error {
|
||||
ln, err := tls.Listen("tcp", address, tlsConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
a.server = &http.Server{}
|
||||
|
||||
return a.run(ln)
|
||||
}
|
||||
|
||||
func (a *App) RunListener(ln net.Listener) error {
|
||||
a.server = &http.Server{}
|
||||
|
||||
return a.run(ln)
|
||||
}
|
||||
|
||||
func (a *App) Shutdown(ctx context.Context) error {
|
||||
return a.server.Shutdown(ctx)
|
||||
}
|
||||
|
67
readme.md
Normal file
67
readme.md
Normal file
@ -0,0 +1,67 @@
|
||||
# NF Web Framework
|
||||
|
||||
### Usage
|
||||
|
||||
##### basic usage
|
||||
|
||||
- get param
|
||||
```go
|
||||
func main() {
|
||||
app := nf.New()
|
||||
|
||||
app.Get("/hello/:name", func(c *nf.Ctx) error {
|
||||
name := c.Param("name")
|
||||
return c.JSON(nf.Map{"status": 200, "data": "hello, " + name})
|
||||
})
|
||||
|
||||
log.Fatal(app.Run("0.0.0.0:80"))
|
||||
}
|
||||
```
|
||||
|
||||
- parse request query
|
||||
```go
|
||||
func handleQuery(c *nf.Ctx) error {
|
||||
type Req struct {
|
||||
Name string `query:"name"`
|
||||
Addr []string `query:"addr"`
|
||||
}
|
||||
|
||||
var (
|
||||
err error
|
||||
req = Req{}
|
||||
)
|
||||
|
||||
if err = c.QueryParser(&req); err != nil {
|
||||
return nf.NewNFError(400, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(nf.Map{"query": req})
|
||||
}
|
||||
```
|
||||
|
||||
- parse application/json body
|
||||
```go
|
||||
func handlePost(c *nf.Ctx) error {
|
||||
type Req struct {
|
||||
Name string `json:"name"`
|
||||
Addr []string `json:"addr"`
|
||||
}
|
||||
|
||||
var (
|
||||
err error
|
||||
req = Req{}
|
||||
reqMap = make(map[string]any)
|
||||
)
|
||||
|
||||
if err = c.BodyParser(&req); err != nil {
|
||||
return nf.NewNFError(400, err.Error())
|
||||
}
|
||||
|
||||
// can parse body multi times
|
||||
if err = c.BodyParser(&reqMap); err != nil {
|
||||
return nf.NewNFError(400, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(nf.Map{"struct": req, "map": reqMap})
|
||||
}
|
||||
```
|
Loading…
x
Reference in New Issue
Block a user