Skip to content

Commit

Permalink
Allow Eval without variants (#145)
Browse files Browse the repository at this point in the history
* WIP: allow rules without variants; fix modal css issues; smaller assets

* Add *_vfsdata.go to gitignore

* Add back vfsdata assets for now

* Upgrade go; move config to own package

* Fix configs in dockerfiles

* Update CHANGELOG
  • Loading branch information
markphelps authored Oct 2, 2019
1 parent 2dbd116 commit 936a26a
Show file tree
Hide file tree
Showing 25 changed files with 871 additions and 677 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
This format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Added

* Support evaluating flags without variants: [https://github.com/markphelps/flipt/issues/138](https://github.com/markphelps/flipt/issues/138)

## [v0.8.0](https://github.com/markphelps/flipt/releases/tag/v0.8.0) - 2019-09-15

### Added
Expand Down
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ARG GO_VERSION=1.12
ARG GO_VERSION=1.13.1

FROM golang:$GO_VERSION-alpine AS build

Expand Down Expand Up @@ -34,7 +34,8 @@ RUN mkdir -p /etc/flipt && \
mkdir -p /var/opt/flipt

COPY --from=build /go/bin/flipt /
COPY config /etc/flipt/config
COPY config/migrations/ /etc/flipt/config/migrations/
COPY config/*.yml /etc/flipt/config/

EXPOSE 8080
EXPOSE 9000
Expand Down
38 changes: 31 additions & 7 deletions cmd/flipt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"net"
"net/http"
Expand Down Expand Up @@ -30,6 +31,7 @@ import (
"github.com/golang-migrate/migrate/database/postgres"
"github.com/golang-migrate/migrate/database/sqlite3"
grpc_gateway "github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/markphelps/flipt/config"
pb "github.com/markphelps/flipt/rpc"
"github.com/markphelps/flipt/server"
"github.com/markphelps/flipt/storage"
Expand Down Expand Up @@ -67,7 +69,7 @@ const (

var (
logger = logrus.New()
cfg *config
cfg *config.Config

cfgPath string
printVersion bool
Expand Down Expand Up @@ -119,7 +121,7 @@ func printVersionHeader() {
func runMigrations() error {
var err error

cfg, err = configure(cfgPath)
cfg, err = config.Load(cfgPath)
if err != nil {
return err
}
Expand Down Expand Up @@ -177,7 +179,7 @@ func execute() error {

var err error

cfg, err = configure(cfgPath)
cfg, err = config.Load(cfgPath)
if err != nil {
return errors.Wrap(err, "loading configuration")
}
Expand Down Expand Up @@ -301,7 +303,7 @@ func execute() error {
grpc_recovery.UnaryServerInterceptor(),
))

if cfg.Server.Protocol == HTTPS {
if cfg.Server.Protocol == config.HTTPS {
creds, err := credentials.NewServerTLSFromFile(cfg.Server.CertFile, cfg.Server.CertKey)
if err != nil {
return errors.Wrap(err, "loading TLS credentials")
Expand All @@ -327,15 +329,15 @@ func execute() error {
)

switch cfg.Server.Protocol {
case HTTPS:
case config.HTTPS:
creds, err := credentials.NewClientTLSFromFile(cfg.Server.CertFile, "")
if err != nil {
return errors.Wrap(err, "loading TLS credentials")
}

opts = append(opts, grpc.WithTransportCredentials(creds))
httpPort = cfg.Server.HTTPSPort
case HTTP:
case config.HTTP:
opts = append(opts, grpc.WithInsecure())
httpPort = cfg.Server.HTTPPort
}
Expand Down Expand Up @@ -400,7 +402,7 @@ func execute() error {
logger.Infof("ui available at: %s://%s:%d", cfg.Server.Protocol, cfg.Server.Host, httpPort)
}

if cfg.Server.Protocol == HTTPS {
if cfg.Server.Protocol == config.HTTPS {
httpServer.TLSConfig = &tls.Config{
MinVersion: tls.VersionTLS12,
PreferServerCipherSuites: true,
Expand Down Expand Up @@ -458,3 +460,25 @@ func execute() error {

return g.Wait()
}

type info struct {
Version string `json:"version,omitempty"`
Commit string `json:"commit,omitempty"`
BuildDate string `json:"buildDate,omitempty"`
GoVersion string `json:"goVersion,omitempty"`
}

func (i info) ServeHTTP(w http.ResponseWriter, r *http.Request) {
out, err := json.Marshal(i)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}

if _, err = w.Write(out); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}

w.WriteHeader(http.StatusOK)
}
44 changes: 9 additions & 35 deletions cmd/flipt/config.go → config/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package config

import (
"encoding/json"
Expand All @@ -11,7 +11,7 @@ import (
"github.com/spf13/viper"
)

type config struct {
type Config struct {
LogLevel string `json:"logLevel,omitempty"`
UI uiConfig `json:"ui,omitempty"`
Cors corsConfig `json:"cors,omitempty"`
Expand Down Expand Up @@ -76,8 +76,8 @@ type databaseConfig struct {
URL string `json:"url,omitempty"`
}

func defaultConfig() *config {
return &config{
func Default() *Config {
return &Config{
LogLevel: "INFO",

UI: uiConfig{
Expand Down Expand Up @@ -140,7 +140,7 @@ const (
cfgDBMigrationsPath = "db.migrations.path"
)

func configure(path string) (*config, error) {
func Load(path string) (*Config, error) {
viper.SetEnvPrefix("FLIPT")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()
Expand All @@ -151,7 +151,7 @@ func configure(path string) (*config, error) {
return nil, errors.Wrap(err, "loading config")
}

cfg := defaultConfig()
cfg := Default()

// Logging
if viper.IsSet(cfgLogLevel) {
Expand Down Expand Up @@ -213,13 +213,13 @@ func configure(path string) (*config, error) {
}

if err := cfg.validate(); err != nil {
return &config{}, err
return &Config{}, err
}

return cfg, nil
}

func (c *config) validate() error {
func (c *Config) validate() error {
if c.Server.Protocol == HTTPS {
if c.Server.CertFile == "" {
return errors.New("cert_file cannot be empty when using HTTPS")
Expand All @@ -238,40 +238,14 @@ func (c *config) validate() error {
return nil
}

func (c *config) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func (c *Config) ServeHTTP(w http.ResponseWriter, r *http.Request) {
out, err := json.Marshal(c)
if err != nil {
logger.WithError(err).Error("getting config")
w.WriteHeader(http.StatusInternalServerError)
return
}

if _, err = w.Write(out); err != nil {
logger.WithError(err).Error("writing response")
w.WriteHeader(http.StatusInternalServerError)
return
}

w.WriteHeader(http.StatusOK)
}

type info struct {
Version string `json:"version,omitempty"`
Commit string `json:"commit,omitempty"`
BuildDate string `json:"buildDate,omitempty"`
GoVersion string `json:"goVersion,omitempty"`
}

func (i info) ServeHTTP(w http.ResponseWriter, r *http.Request) {
out, err := json.Marshal(i)
if err != nil {
logger.WithError(err).Error("getting metadata")
w.WriteHeader(http.StatusInternalServerError)
return
}

if _, err = w.Write(out); err != nil {
logger.WithError(err).Error("writing response")
w.WriteHeader(http.StatusInternalServerError)
return
}
Expand Down
53 changes: 15 additions & 38 deletions cmd/flipt/config_test.go → config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package config

import (
"io/ioutil"
Expand All @@ -10,22 +10,22 @@ import (
"github.com/stretchr/testify/require"
)

func TestConfigure(t *testing.T) {
func TestLoad(t *testing.T) {
tests := []struct {
name string
path string
wantErr bool
expected *config
expected *Config
}{
{
name: "defaults",
path: "./testdata/config/default.yml",
expected: defaultConfig(),
expected: Default(),
},
{
name: "configured",
path: "./testdata/config/advanced.yml",
expected: &config{
expected: &Config{
LogLevel: "WARN",
UI: uiConfig{
Enabled: false,
Expand Down Expand Up @@ -65,7 +65,7 @@ func TestConfigure(t *testing.T) {
)

t.Run(tt.name, func(t *testing.T) {
cfg, err := configure(path)
cfg, err := Load(path)

if wantErr {
require.Error(t, err)
Expand All @@ -83,13 +83,13 @@ func TestConfigure(t *testing.T) {
func TestValidate(t *testing.T) {
tests := []struct {
name string
cfg *config
cfg *Config
wantErr bool
wantErrMsg string
}{
{
name: "https: valid",
cfg: &config{
cfg: &Config{
Server: serverConfig{
Protocol: HTTPS,
CertFile: "./testdata/config/ssl_cert.pem",
Expand All @@ -99,7 +99,7 @@ func TestValidate(t *testing.T) {
},
{
name: "http: valid",
cfg: &config{
cfg: &Config{
Server: serverConfig{
Protocol: HTTP,
CertFile: "foo.pem",
Expand All @@ -109,7 +109,7 @@ func TestValidate(t *testing.T) {
},
{
name: "https: empty cert_file path",
cfg: &config{
cfg: &Config{
Server: serverConfig{
Protocol: HTTPS,
CertFile: "",
Expand All @@ -121,7 +121,7 @@ func TestValidate(t *testing.T) {
},
{
name: "https: empty key_file path",
cfg: &config{
cfg: &Config{
Server: serverConfig{
Protocol: HTTPS,
CertFile: "./testdata/config/ssl_cert.pem",
Expand All @@ -133,7 +133,7 @@ func TestValidate(t *testing.T) {
},
{
name: "https: missing cert_file",
cfg: &config{
cfg: &Config{
Server: serverConfig{
Protocol: HTTPS,
CertFile: "foo.pem",
Expand All @@ -145,7 +145,7 @@ func TestValidate(t *testing.T) {
},
{
name: "https: missing key_file",
cfg: &config{
cfg: &Config{
Server: serverConfig{
Protocol: HTTPS,
CertFile: "./testdata/config/ssl_cert.pem",
Expand Down Expand Up @@ -178,9 +178,9 @@ func TestValidate(t *testing.T) {
}
}

func TestConfigServeHTTP(t *testing.T) {
func TestServeHTTP(t *testing.T) {
var (
cfg = defaultConfig()
cfg = Default()
req = httptest.NewRequest("GET", "http://example.com/foo", nil)
w = httptest.NewRecorder()
)
Expand All @@ -195,26 +195,3 @@ func TestConfigServeHTTP(t *testing.T) {
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.NotEmpty(t, body)
}

func TestInfoServeHTTP(t *testing.T) {
var (
i = info{
Version: "1.0.0",
Commit: "12345",
BuildDate: "2019-09-01",
GoVersion: "1.12.9",
}
req = httptest.NewRequest("GET", "http://example.com/foo", nil)
w = httptest.NewRecorder()
)

i.ServeHTTP(w, req)

resp := w.Result()
defer resp.Body.Close()

body, _ := ioutil.ReadAll(resp.Body)

assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.NotEmpty(t, body)
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion dev/centos/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ set -euxo pipefail

NODE_VERSION='12.4.0'
YARN_VERSION='1.16.0'
GO_VERSION='1.12.5'
GO_VERSION='1.13.1'
PROTOC_VERSION='3.6.1'

echo '=== Installing necessary system libraries to build'
Expand Down
2 changes: 1 addition & 1 deletion dev/ubuntu/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ set -euxo pipefail

NODE_VERSION='12.4.0'
YARN_VERSION='1.16.0'
GO_VERSION='1.12.5'
GO_VERSION='1.13.1'
PROTOC_VERSION='3.6.1'

echo '=== Installing necessary system libraries to build'
Expand Down
Loading

0 comments on commit 936a26a

Please sign in to comment.