You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.4 KiB

4 years ago
  1. package command
  2. import (
  3. "net/http"
  4. "strconv"
  5. "strings"
  6. "time"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. "github.com/chrislusf/seaweedfs/weed/server"
  9. "github.com/chrislusf/seaweedfs/weed/util"
  10. )
  11. var (
  12. gatewayOptions GatewayOptions
  13. )
  14. type GatewayOptions struct {
  15. masters *string
  16. filers *string
  17. bindIp *string
  18. port *int
  19. maxMB *int
  20. }
  21. func init() {
  22. cmdGateway.Run = runGateway // break init cycle
  23. gatewayOptions.masters = cmdGateway.Flag.String("master", "localhost:9333", "comma-separated master servers")
  24. gatewayOptions.filers = cmdGateway.Flag.String("filer", "localhost:8888", "comma-separated filer servers")
  25. gatewayOptions.bindIp = cmdGateway.Flag.String("ip.bind", "localhost", "ip address to bind to")
  26. gatewayOptions.port = cmdGateway.Flag.Int("port", 5647, "gateway http listen port")
  27. gatewayOptions.maxMB = cmdGateway.Flag.Int("maxMB", 4, "split files larger than the limit")
  28. }
  29. var cmdGateway = &Command{
  30. UsageLine: "gateway -port=8888 -master=<ip:port>[,<ip:port>]* -filer=<ip:port>[,<ip:port>]*",
  31. Short: "start a gateway server that points to a list of master servers or a list of filers",
  32. Long: `start a gateway server which accepts REST operation to write any blobs, files, or topic messages.
  33. POST /blobs/
  34. upload the blob and return a chunk id
  35. DELETE /blobs/<chunk_id>
  36. delete a chunk id
  37. /*
  38. POST /files/path/to/a/file
  39. save /path/to/a/file on filer
  40. DELETE /files/path/to/a/file
  41. delete /path/to/a/file on filer
  42. POST /topics/topicName
  43. save on filer to /topics/topicName/<ds>/ts.json
  44. */
  45. `,
  46. }
  47. func runGateway(cmd *Command, args []string) bool {
  48. util.LoadConfiguration("security", false)
  49. gatewayOptions.startGateway()
  50. return true
  51. }
  52. func (gw *GatewayOptions) startGateway() {
  53. defaultMux := http.NewServeMux()
  54. _, gws_err := weed_server.NewGatewayServer(defaultMux, &weed_server.GatewayOption{
  55. Masters: strings.Split(*gw.masters, ","),
  56. Filers: strings.Split(*gw.filers, ","),
  57. MaxMB: *gw.maxMB,
  58. })
  59. if gws_err != nil {
  60. glog.Fatalf("Gateway startup error: %v", gws_err)
  61. }
  62. glog.V(0).Infof("Start Seaweed Gateway %s at %s:%d", util.Version(), *gw.bindIp, *gw.port)
  63. gatewayListener, e := util.NewListener(
  64. *gw.bindIp+":"+strconv.Itoa(*gw.port),
  65. time.Duration(10)*time.Second,
  66. )
  67. if e != nil {
  68. glog.Fatalf("Filer listener error: %v", e)
  69. }
  70. httpS := &http.Server{Handler: defaultMux}
  71. if err := httpS.Serve(gatewayListener); err != nil {
  72. glog.Fatalf("Gateway Fail to serve: %v", e)
  73. }
  74. }