Files
go-zero/example/http/signature/server/server.go

60 lines
969 B
Go
Raw Normal View History

2020-07-26 17:09:05 +08:00
package main
import (
"flag"
"io"
"net/http"
"zero/core/logx"
"zero/core/service"
2020-07-31 11:14:48 +08:00
"zero/rest"
"zero/rest/httpx"
2020-07-26 17:09:05 +08:00
)
var keyPem = flag.String("prikey", "private.pem", "the private key file")
type Request struct {
User string `form:"user,optional"`
}
func handle(w http.ResponseWriter, r *http.Request) {
var req Request
err := httpx.Parse(r, &req)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
io.Copy(w, r.Body)
}
func main() {
flag.Parse()
2020-07-31 11:45:16 +08:00
engine := rest.MustNewServer(rest.RestConf{
2020-07-26 17:09:05 +08:00
ServiceConf: service.ServiceConf{
Log: logx.LogConf{
Path: "logs",
},
},
Port: 3333,
2020-07-31 11:14:48 +08:00
Signature: rest.SignatureConf{
2020-07-26 17:09:05 +08:00
Strict: true,
2020-07-31 11:14:48 +08:00
PrivateKeys: []rest.PrivateKeyConf{
2020-07-26 17:09:05 +08:00
{
Fingerprint: "bvw8YlnSqb+PoMf3MBbLdQ==",
KeyFile: *keyPem,
},
},
},
})
defer engine.Stop()
2020-07-31 11:14:48 +08:00
engine.AddRoute(rest.Route{
2020-07-26 17:09:05 +08:00
Method: http.MethodPost,
Path: "/a/b",
Handler: handle,
})
engine.Start()
}