Skip to content

Commit e149477

Browse files
authored
(#35)
1 parent dd53314 commit e149477

16 files changed

+86
-37
lines changed

.github/workflows/offline-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
2424
- name: Wait for API to be ready
2525
run: |
26-
timeout 60s bash -c 'while [[ "$(curl -s -o /dev/null -w ''%{http_code}'' 127.0.0.1:8080)" != "200" ]]; do sleep 2; done' || false
26+
timeout 60s bash -c 'while [[ "$(curl -s -o /dev/null -w ''%{http_code}'' 127.0.0.1:8081)" != "200" ]]; do sleep 2; done' || false
2727
2828
- name: Install Hurl
2929
run: |

config/terrable_toml.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@ type TerrableToml struct {
1616
type OfflineConfig struct {
1717
File string `toml:"file"`
1818
Module string `toml:"module"`
19+
Port string `toml:"port"`
1920
}
2021

21-
func ParseTerrableToml(directory string) (*TerrableToml, error) {
22+
func ParseTerrableToml() (*TerrableToml, error) {
23+
workingDir, _ := os.Getwd()
24+
2225
// Attempt to find a .terrable.toml file in the active directory
23-
filePath := filepath.Join(directory, ".terrable.toml")
26+
filePath := filepath.Join(workingDir, ".terrable.toml")
2427
_, err := os.Stat(filePath)
2528

2629
if os.IsNotExist(err) {

main.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ func main() {
1919
Name: "offline",
2020
Usage: "",
2121
Action: func(cCtx *cli.Context) error {
22-
executablePath, _ := os.Getwd()
23-
tomlConfig, _ := config.ParseTerrableToml(executablePath)
22+
tomlConfig, _ := config.ParseTerrableToml()
2423

2524
filePath := cCtx.String("file")
2625
moduleName := cCtx.String("module")
26+
port := cCtx.String("port")
2727

2828
if filePath == "" {
2929
filePath = tomlConfig.Offline.File
@@ -33,7 +33,10 @@ func main() {
3333
moduleName = tomlConfig.Offline.Module
3434
}
3535

36-
port := cCtx.String("port")
36+
if port == "" {
37+
port = tomlConfig.Offline.Port
38+
}
39+
3740
err := offline.Run(filePath, moduleName, port)
3841

3942
if err != nil {
@@ -59,7 +62,6 @@ func main() {
5962
Name: "port",
6063
Aliases: []string{"p"},
6164
Required: false,
62-
Value: "8080",
6365
Usage: "The port number that the local instance of the API should listen for requests at",
6466
},
6567
},

offline/offline.go

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package offline
33
import (
44
"fmt"
55
"log"
6+
"net"
67
"net/http"
7-
"path/filepath"
88
"strings"
99
"sync"
1010

@@ -22,43 +22,86 @@ func Run(filePath string, moduleName string, port string) error {
2222

2323
// TODO: Validate config
2424

25-
tomlConfig, err := config.ParseTerrableToml(filepath.Dir(filePath))
25+
tomlConfig, err := config.ParseTerrableToml()
2626

2727
if err != nil {
2828
panic(fmt.Errorf("error parsing .terrable.toml file: %w", err))
2929
}
3030

31-
printConfig(*terrableConfig, port)
31+
listener, activePort, err := getListener(port)
32+
33+
if err != nil {
34+
return err
35+
}
36+
37+
defer listener.Close()
38+
39+
printConfig(*terrableConfig, activePort)
3240

3341
var wg sync.WaitGroup
34-
defer wg.Done()
42+
defer wg.Wait()
3543

3644
r := mux.NewRouter()
3745

46+
// Start compiling and serving each handler
3847
for _, handler := range terrableConfig.Handlers {
39-
go ServeHandler(&HandlerInstance{
40-
handlerConfig: handler,
41-
envVars: tomlConfig.Environment,
42-
}, r)
48+
wg.Add(1)
49+
50+
go func(handler config.HandlerMapping) {
51+
defer wg.Done()
52+
53+
ServeHandler(&HandlerInstance{
54+
handlerConfig: handler,
55+
envVars: tomlConfig.Environment,
56+
}, r)
57+
}(handler)
4358
}
4459

45-
fmt.Printf("Starting server on :%s\n", port)
60+
fmt.Printf("Starting server on :%d\n", activePort)
61+
62+
server := &http.Server{
63+
Handler: r,
64+
}
4665

47-
if err := http.ListenAndServe(fmt.Sprintf("127.0.0.1:%s", port), r); err != nil {
48-
return fmt.Errorf("could not start server on port %s. Error: %s", port, err.Error())
66+
if err := server.Serve(listener); err != nil && err != http.ErrServerClosed {
67+
return fmt.Errorf("could not start server on port %d. Error: %w", activePort, err)
4968
}
5069

5170
return nil
5271
}
5372

54-
func printConfig(config config.TerrableConfig, port string) {
73+
func getListener(port string) (net.Listener, int, error) {
74+
var specificPortDesired bool = (port != "")
75+
76+
if port == "" {
77+
port = "8080"
78+
}
79+
80+
listener, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%s", port))
81+
82+
if err != nil {
83+
if specificPortDesired {
84+
return nil, 0, fmt.Errorf("could not start server on specified port %s: %w", port, err)
85+
}
86+
87+
// Try with a random free-port
88+
listener, err = net.Listen("tcp", "127.0.0.1:0")
89+
if err != nil {
90+
return nil, 0, fmt.Errorf("could not start server on any available port: %w", err)
91+
}
92+
}
93+
94+
return listener, listener.Addr().(*net.TCPAddr).Port, nil
95+
}
96+
97+
func printConfig(config config.TerrableConfig, port int) {
5598
totalEndpoints := 0
5699
printlines := []string{}
57100

58101
for _, handler := range config.Handlers {
59102
for method, path := range handler.Http {
60103
totalEndpoints += 1
61-
printlines = append(printlines, fmt.Sprintf(" %-*s http://localhost:%s%s\n", 5, method, port, path))
104+
printlines = append(printlines, fmt.Sprintf(" %-*s http://localhost:%d%s\n", 5, method, port, path))
62105
}
63106
}
64107

offline/offline_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func TestPrintConfig(t *testing.T) {
3838
os.Stdout = w
3939

4040
// Call the function
41-
printConfig(testConfig, "1234")
41+
printConfig(testConfig, 1234)
4242

4343
// Restore stdout
4444
w.Close()

samples/simple/.terrable.toml

Lines changed: 0 additions & 6 deletions
This file was deleted.

terrable_build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version = 0.1.6
1+
version = 0.1.7

tests/.terrable.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[environment]
2+
TOML_VAR = "hello toml"
3+
4+
[offline]
5+
file = "../samples/simple/simple-api.tf"
6+
module = "simple_api"
7+
port = "8081"

tests/_start.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
go build ../
2-
./terrable offline -f "../samples/simple/simple-api.tf" -m "simple_api"
2+
./terrable offline

tests/parallel/echo_bad_method.hurl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
DELETE http://127.0.0.1:8080/
1+
DELETE http://127.0.0.1:8081/
22
HTTP 405

tests/parallel/echo_get.hurl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
GET http://127.0.0.1:8080/
1+
GET http://127.0.0.1:8081/
22
HTTP 200
33

44
[Asserts]

tests/parallel/echo_post.hurl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
POST http://127.0.0.1:8080/
1+
POST http://127.0.0.1:8081/
22
HTTP 200
33

44
[Asserts]

tests/parallel/env_vars.hurl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
GET http://127.0.0.1:8080/
1+
GET http://127.0.0.1:8081/
22
HTTP 200
33

44
[Asserts]
5-
jsonpath "$.env.TEST_VAR" == "hello world"
5+
jsonpath "$.env.TOML_VAR" == "hello toml"

tests/parallel/query_string_parameters.hurl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
GET http://127.0.0.1:8080/?firstQuery=123&secondQuery=hello
1+
GET http://127.0.0.1:8081/?firstQuery=123&secondQuery=hello
22
HTTP 200
33

44
[Asserts]

tests/parallel/response_headers.hurl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
GET http://127.0.0.1:8080/
1+
GET http://127.0.0.1:8081/
22
HTTP 200
33

44
[Asserts]

tests/serial/delayed.hurl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
GET http://127.0.0.1:8080/delayed
1+
GET http://127.0.0.1:8081/delayed
22

33
HTTP 200
44
[Asserts]

0 commit comments

Comments
 (0)