server.go 969 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package main
  2. import (
  3. "flag"
  4. "io"
  5. "net/http"
  6. "zero/core/logx"
  7. "zero/core/service"
  8. "zero/rest"
  9. "zero/rest/httpx"
  10. )
  11. var keyPem = flag.String("prikey", "private.pem", "the private key file")
  12. type Request struct {
  13. User string `form:"user,optional"`
  14. }
  15. func handle(w http.ResponseWriter, r *http.Request) {
  16. var req Request
  17. err := httpx.Parse(r, &req)
  18. if err != nil {
  19. http.Error(w, err.Error(), http.StatusBadRequest)
  20. return
  21. }
  22. io.Copy(w, r.Body)
  23. }
  24. func main() {
  25. flag.Parse()
  26. engine := rest.MustNewServer(rest.RestConf{
  27. ServiceConf: service.ServiceConf{
  28. Log: logx.LogConf{
  29. Path: "logs",
  30. },
  31. },
  32. Port: 3333,
  33. Signature: rest.SignatureConf{
  34. Strict: true,
  35. PrivateKeys: []rest.PrivateKeyConf{
  36. {
  37. Fingerprint: "bvw8YlnSqb+PoMf3MBbLdQ==",
  38. KeyFile: *keyPem,
  39. },
  40. },
  41. },
  42. })
  43. defer engine.Stop()
  44. engine.AddRoute(rest.Route{
  45. Method: http.MethodPost,
  46. Path: "/a/b",
  47. Handler: handle,
  48. })
  49. engine.Start()
  50. }