50 lines
684 B
Go
50 lines
684 B
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/loveuer/nf"
|
|
)
|
|
|
|
var (
|
|
addr string
|
|
hostname string
|
|
)
|
|
|
|
func init() {
|
|
flag.StringVar(&addr, "addr", ":80", "")
|
|
flag.Parse()
|
|
|
|
hostname = os.Getenv("HOSTNAME")
|
|
if hostname == "" {
|
|
hostname = fmt.Sprintf("unknown-%d", time.Now().UnixNano())
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
app := nf.New()
|
|
|
|
app.Use(func(c *nf.Ctx) error {
|
|
c.SetHeader("U-HOSTNAME", hostname)
|
|
return c.Next()
|
|
})
|
|
|
|
app.Get("/hello/:name", func(c *nf.Ctx) error {
|
|
return c.JSON(nf.Map{
|
|
"status": 200,
|
|
"data": nf.Map{
|
|
"name": c.Param("name"),
|
|
"time": time.Now(),
|
|
"host": hostname,
|
|
},
|
|
},
|
|
)
|
|
})
|
|
|
|
log.Fatal(app.Run(addr))
|
|
}
|