Commit 8c19ba4e authored by ale's avatar ale
Browse files

Move server-side code to the server package

parent 26a3d5dc
...@@ -4,14 +4,11 @@ import ( ...@@ -4,14 +4,11 @@ import (
"flag" "flag"
"io/ioutil" "io/ioutil"
"log" "log"
"os"
"strings"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"git.autistici.org/ai3/go-common/serverutil" "git.autistici.org/ai3/go-common/serverutil"
"git.autistici.org/id/keystore"
"git.autistici.org/id/keystore/server" "git.autistici.org/id/keystore/server"
) )
...@@ -26,10 +23,10 @@ var ( ...@@ -26,10 +23,10 @@ var (
configFile = flag.String("config", "/etc/keystore/config.yml", "path of config file") configFile = flag.String("config", "/etc/keystore/config.yml", "path of config file")
) )
// Wrap the keystore.Config together with the server setup in a single // Config wraps the keystore.Config together with the server setup in
// configuration object. // a single configuration object.
type Config struct { type Config struct {
KeyStoreConfig *keystore.Config `yaml:"keystore"` KeyStoreConfig *server.Config `yaml:"keystore"`
ServerConfig *serverutil.ServerConfig `yaml:"http_server"` ServerConfig *serverutil.ServerConfig `yaml:"http_server"`
} }
...@@ -46,19 +43,8 @@ func loadConfig() (*Config, error) { ...@@ -46,19 +43,8 @@ func loadConfig() (*Config, error) {
return &config, nil return &config, nil
} }
// Set defaults for command-line flags using variables from the environment.
func setFlagDefaultsFromEnv() {
flag.VisitAll(func(f *flag.Flag) {
envVar := "KEYSTORE_" + strings.ToUpper(strings.Replace(f.Name, "-", "_", -1))
if value := os.Getenv(envVar); value != "" {
f.DefValue = value
f.Value.Set(value)
}
})
}
func main() { func main() {
setFlagDefaultsFromEnv() log.SetFlags(0)
flag.Parse() flag.Parse()
config, err := loadConfig() config, err := loadConfig()
...@@ -66,12 +52,12 @@ func main() { ...@@ -66,12 +52,12 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
ks, err := keystore.New(config.KeyStoreConfig) ks, err := server.NewKeyStore(config.KeyStoreConfig)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
srv := server.New(ks) srv := server.NewServer(ks)
if err := serverutil.Serve(srv, config.ServerConfig, *addr); err != nil { if err := serverutil.Serve(srv, config.ServerConfig, *addr); err != nil {
log.Fatal(err) log.Fatal(err)
......
package keystore package server
import ( import (
"github.com/miscreant/miscreant/go" "github.com/miscreant/miscreant/go"
......
package keystore package server
import ( import (
"context" "context"
...@@ -79,8 +79,8 @@ type KeyStore struct { ...@@ -79,8 +79,8 @@ type KeyStore struct {
validator sso.Validator validator sso.Validator
} }
// New creates a new KeyStore with the given config and returns it. // NewKeyStore creates a new KeyStore with the given config and returns it.
func New(config *Config) (*KeyStore, error) { func NewKeyStore(config *Config) (*KeyStore, error) {
if err := config.check(); err != nil { if err := config.check(); err != nil {
return nil, err return nil, err
} }
......
...@@ -12,7 +12,7 @@ import ( ...@@ -12,7 +12,7 @@ import (
var emptyResponse struct{} var emptyResponse struct{}
type keyStoreServer struct { type keyStoreServer struct {
*keystore.KeyStore *KeyStore
} }
func (s *keyStoreServer) handleOpen(w http.ResponseWriter, r *http.Request) { func (s *keyStoreServer) handleOpen(w http.ResponseWriter, r *http.Request) {
...@@ -71,6 +71,7 @@ func (s *keyStoreServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { ...@@ -71,6 +71,7 @@ func (s *keyStoreServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
} }
func New(ks *keystore.KeyStore) http.Handler { // NewServer wraps the HTTP API around a KeyStore.
func NewServer(ks *KeyStore) http.Handler {
return &keyStoreServer{ks} return &keyStoreServer{ks}
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment