You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/example/http/signature/server/server.go

60 lines
969 B
Go

4 years ago
package main
import (
"flag"
"io"
"net/http"
"zero/core/logx"
"zero/core/service"
"zero/rest"
"zero/rest/httpx"
4 years ago
)
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()
engine := rest.MustNewServer(rest.RestConf{
4 years ago
ServiceConf: service.ServiceConf{
Log: logx.LogConf{
Path: "logs",
},
},
Port: 3333,
Signature: rest.SignatureConf{
4 years ago
Strict: true,
PrivateKeys: []rest.PrivateKeyConf{
4 years ago
{
Fingerprint: "bvw8YlnSqb+PoMf3MBbLdQ==",
KeyFile: *keyPem,
},
},
},
})
defer engine.Stop()
engine.AddRoute(rest.Route{
4 years ago
Method: http.MethodPost,
Path: "/a/b",
Handler: handle,
})
engine.Start()
}