-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from DrmagicE/refactor
Refactor: refactor the way to expose grpc and http endpoint
- Loading branch information
Showing
33 changed files
with
1,046 additions
and
336 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// +build !windows | ||
|
||
package main | ||
|
||
var ( | ||
DefaultConfigDir = "/etc/gmqtt" | ||
) | ||
|
||
func getDefaultConfigDir() (string, error) { | ||
return DefaultConfigDir, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// +build windows | ||
|
||
package main | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
func getDefaultConfigDir() (string, error) { | ||
return filepath.Join(os.Getenv("programdata"), "gmqtt"), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"strings" | ||
) | ||
|
||
// API is the configuration for API server. | ||
// The API server use gRPC-gateway to provide both gRPC and HTTP endpoints. | ||
type API struct { | ||
// GRPC is the gRPC endpoint configuration. | ||
GRPC []*Endpoint `yaml:"grpc"` | ||
// HTTP is the HTTP endpoint configuration. | ||
HTTP []*Endpoint `yaml:"http"` | ||
} | ||
|
||
// Endpoint represents a gRPC or HTTP server endpoint. | ||
type Endpoint struct { | ||
// Address is the bind address of the endpoint. | ||
// Format: [tcp|unix://][<host>]:<port> | ||
// e.g : | ||
// * unix:///var/run/gmqttd.sock | ||
// * tcp://127.0.0.1:8080 | ||
// * :8081 (equal to tcp://:8081) | ||
Address string `yaml:"address"` | ||
// Map maps the HTTP endpoint to gRPC endpoint. | ||
// Must be set if the endpoint is representing a HTTP endpoint. | ||
Map string `yaml:"map"` | ||
// TLS is the tls configuration. | ||
TLS *TLSOptions `yaml:"tls"` | ||
} | ||
|
||
var DefaultAPI API | ||
|
||
func (a API) validateAddress(address string, fieldName string) error { | ||
if address == "" { | ||
return fmt.Errorf("%s cannot be empty", fieldName) | ||
} | ||
epParts := strings.SplitN(address, "://", 2) | ||
if len(epParts) == 1 && epParts[0] != "" { | ||
epParts = []string{"tcp", epParts[0]} | ||
} | ||
if len(epParts) != 0 { | ||
switch epParts[0] { | ||
case "tcp": | ||
_, _, err := net.SplitHostPort(epParts[1]) | ||
if err != nil { | ||
return fmt.Errorf("invalid %s: %s", fieldName, err.Error()) | ||
} | ||
case "unix": | ||
default: | ||
return fmt.Errorf("invalid %s schema: %s", fieldName, epParts[0]) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (a API) Validate() error { | ||
for _, v := range a.GRPC { | ||
err := a.validateAddress(v.Address, "endpoint") | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
for _, v := range a.HTTP { | ||
err := a.validateAddress(v.Address, "endpoint") | ||
if err != nil { | ||
return err | ||
} | ||
err = a.validateAddress(v.Map, "map") | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
Oops, something went wrong.