Skip to content
Snippets Groups Projects
Commit 8c19ba4e authored by ale's avatar ale
Browse files

Move server-side code to the server package

parent 26a3d5dc
Branches
No related tags found
No related merge requests found
......@@ -4,14 +4,11 @@ import (
"flag"
"io/ioutil"
"log"
"os"
"strings"
"gopkg.in/yaml.v2"
"git.autistici.org/ai3/go-common/serverutil"
"git.autistici.org/id/keystore"
"git.autistici.org/id/keystore/server"
)
......@@ -26,10 +23,10 @@ var (
configFile = flag.String("config", "/etc/keystore/config.yml", "path of config file")
)
// Wrap the keystore.Config together with the server setup in a single
// configuration object.
// Config wraps the keystore.Config together with the server setup in
// a single configuration object.
type Config struct {
KeyStoreConfig *keystore.Config `yaml:"keystore"`
KeyStoreConfig *server.Config `yaml:"keystore"`
ServerConfig *serverutil.ServerConfig `yaml:"http_server"`
}
......@@ -46,19 +43,8 @@ func loadConfig() (*Config, error) {
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() {
setFlagDefaultsFromEnv()
log.SetFlags(0)
flag.Parse()
config, err := loadConfig()
......@@ -66,12 +52,12 @@ func main() {
log.Fatal(err)
}
ks, err := keystore.New(config.KeyStoreConfig)
ks, err := server.NewKeyStore(config.KeyStoreConfig)
if err != nil {
log.Fatal(err)
}
srv := server.New(ks)
srv := server.NewServer(ks)
if err := serverutil.Serve(srv, config.ServerConfig, *addr); err != nil {
log.Fatal(err)
......
package keystore
package server
import (
"github.com/miscreant/miscreant/go"
......
package keystore
package server
import (
"context"
......@@ -79,8 +79,8 @@ type KeyStore struct {
validator sso.Validator
}
// New creates a new KeyStore with the given config and returns it.
func New(config *Config) (*KeyStore, error) {
// NewKeyStore creates a new KeyStore with the given config and returns it.
func NewKeyStore(config *Config) (*KeyStore, error) {
if err := config.check(); err != nil {
return nil, err
}
......
......@@ -12,7 +12,7 @@ import (
var emptyResponse struct{}
type keyStoreServer struct {
*keystore.KeyStore
*KeyStore
}
func (s *keyStoreServer) handleOpen(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}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment