Browse Source

Add YAML tags to things so we can unmarshal them. Cannot partially unmarshal json.RawMessage-like though...

pull/98/head
Kegan Dougal 8 years ago
parent
commit
a7de9a7fdd
  1. 30
      config.sample.yaml
  2. 12
      src/github.com/matrix-org/go-neb/api.go
  3. 32
      src/github.com/matrix-org/go-neb/config.go
  4. 4
      src/github.com/matrix-org/go-neb/goneb.go
  5. 12
      src/github.com/matrix-org/go-neb/types/types.go

30
config.sample.yaml

@ -18,27 +18,27 @@
# Delete or modify this list as appropriate.
# See the docs for /configureClient for the full list of options.
clients:
- UserID: "@goneb:localhost"
AccessToken: "MDASDASJDIASDJASDAFGFRGER"
HomeserverURL: "http://localhost:8008"
Sync: true
AutoJoinRooms: true
DisplayName: "Go-NEB!"
- user_id: "@goneb:localhost"
access_token: "MDASDASJDIASDJASDAFGFRGER"
homeserver_url: "http://localhost:8008"
sync: true
auto_join_rooms: true
display_name: "Go-NEB!"
- UserID: "@another_goneb:localhost"
AccessToken: "MDASDASJDIASDJASDAFGFRGER"
HomeserverURL: "http://localhost:8008"
Sync: false
AutoJoinRooms: false
DisplayName: "Go-NEB!"
- user_id: "@another_goneb:localhost"
access_token: "MDASDASJDIASDJASDAFGFRGER"
homeserver_url: "http://localhost:8008"
sync: false
auto_join_rooms: false
display_name: "Go-NEB!"
# The list of realms which Go-NEB is aware of.
# Delete or modify this list as appropriate.
# See the docs for /configureAuthRealm for the full list of options.
realms:
- ID: "github_realm"
Type: "github"
Config:
- id: "github_realm"
type: "github"
config:
ClientSecret: "YOUR_CLIENT_SECRET"
ClientID: "YOUR_CLIENT_ID"

12
src/github.com/matrix-org/go-neb/api.go

@ -132,15 +132,17 @@ type configureAuthRealmHandler struct {
db *database.ServiceDB
}
type configureAuthRealmRequest struct {
ID string `yaml:"id"`
Type string `yaml:"type"`
Config json.RawMessage `yaml:"config"`
}
func (h *configureAuthRealmHandler) OnIncomingRequest(req *http.Request) (interface{}, *errors.HTTPError) {
if req.Method != "POST" {
return nil, &errors.HTTPError{nil, "Unsupported Method", 405}
}
var body struct {
ID string
Type string
Config json.RawMessage
}
var body configureAuthRealmRequest
if err := json.NewDecoder(req.Body).Decode(&body); err != nil {
return nil, &errors.HTTPError{err, "Error parsing request JSON", 400}
}

32
src/github.com/matrix-org/go-neb/config.go

@ -1,12 +1,38 @@
package main
import (
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/matrix-org/go-neb/database"
"github.com/matrix-org/go-neb/types"
"gopkg.in/yaml.v2"
"io/ioutil"
)
func loadFromConfig(db *database.ServiceDB, configFile string) error {
logger := log.WithField("config_file", configFile)
type configFile struct {
Clients []types.ClientConfig `yaml:"clients"`
Realms []configureAuthRealmRequest `yaml:"realms"`
// Sessions []sessionConfig `yaml:"sessions"`
// Services []serviceConfig `yaml:"services"`
}
func loadFromConfig(db *database.ServiceDB, configFilePath string) (*configFile, error) {
logger := log.WithField("config_file", configFilePath)
logger.Info("Loading from config file")
return nil
contents, err := ioutil.ReadFile(configFilePath)
if err != nil {
return nil, err
}
var cfg configFile
if err = yaml.Unmarshal(contents, &cfg); err != nil {
return nil, err
}
// sanity check (at least 1 client and 1 service)
if len(cfg.Clients) == 0 {
return nil, fmt.Errorf("At least 1 client and 1 service must be specified")
}
return &cfg, nil
}

4
src/github.com/matrix-org/go-neb/goneb.go

@ -63,9 +63,11 @@ func main() {
database.SetServiceDB(db)
if configYAML != "" {
if err := loadFromConfig(db, configYAML); err != nil {
var cfg *configFile
if cfg, err = loadFromConfig(db, configYAML); err != nil {
log.WithError(err).WithField("config_file", configYAML).Panic("Failed to load config file")
}
log.Info(cfg)
}
clients := clients.New(db)

12
src/github.com/matrix-org/go-neb/types/types.go

@ -14,12 +14,12 @@ import (
// A ClientConfig is the configuration for a matrix client for a bot to use.
type ClientConfig struct {
UserID string // The matrix UserId to connect with.
HomeserverURL string // A URL with the host and port of the matrix server. E.g. https://matrix.org:8448
AccessToken string // The matrix access token to authenticate the requests with.
Sync bool // True to start a sync stream for this user
AutoJoinRooms bool // True to automatically join all rooms for this user
DisplayName string // The display name to set for the matrix client
UserID string `yaml:"user_id"` // The matrix UserId to connect with.
HomeserverURL string `yaml:"homeserver_url"` // A URL with the host and port of the matrix server. E.g. https://matrix.org:8448
AccessToken string `yaml:"access_token"` // The matrix access token to authenticate the requests with.
Sync bool `yaml:"sync"` // True to start a sync stream for this user
AutoJoinRooms bool `yaml:"auto_join_rooms"` // True to automatically join all rooms for this user
DisplayName string `yaml:"display_name"` // The display name to set for the matrix client
}
// Check that the client has the correct fields.

Loading…
Cancel
Save