示例#1
0
func TestValidConfigFile(t *testing.T) {
	fixtures := []string{
		`{}`,
		`{"listener":{}}`,
		`{"backend":{}}`,
		`{"listener":{"address":"localhost:1234"}}`,
		`{"backend":{"address":"localhost:4321"}}`,
		`{"backend":{"prefix":"/api"}}`,
		`{"listener":{"prefix":"/v1"}}`,
	}

	for testn, fix := range fixtures {
		func() {

			// Setup
			config, err := ioutil.TempFile("", "rapi-config")
			require.Nil(t, err)
			if os.Getenv("TEST_PRESERVE") == "" {
				defer os.Remove(config.Name())
			}

			_, err = config.WriteString(fix)
			require.Nil(t, err)
			err = config.Close()
			require.Nil(t, err)

			// Verification
			var conf *rapi.Config

			assert.NotPanics(t, func() { conf = rapi.NewConfigFile(config.Name()) }, "[%d:%s] parse errors", testn, fix)
			assert.NotNil(t, conf, "[%d:%s] invalid config", testn, fix)
		}()
	}
}
示例#2
0
func main() {
	var configPath string

	flag.StringVar(&configPath, "c", "config.json", "Path to config file")
	flag.Parse()
	flag.Visit(func(v *flag.Flag) {
		fmt.Printf("%s - %s: %s\n", v.Usage, v.Name, v.Value)
	})

	config := rapi.NewConfigFile(configPath)
	api := rapi.New(config)

	api.NewEndpoint("POST", "/auth/login")
	api.NewEndpoint("GET", "/users")
	api.NewEndpoint("GET", "/invoices").
		TransformResponse(&InvoiceInternal{}, &InvoiceExternal{})
	api.NewEndpoint("GET", "/invoices/{Id:[A-Z0-9]+}").
		InternalPath("/invoices/{{.Id}}").
		TransformResponse(&InvoiceInternal{}, &InvoiceExternal{})

	api.Run()
}