11package offline
22
33import (
4+ "errors"
45 "fmt"
56 "log"
67 "net"
78 "net/http"
89 "os"
10+ "strings"
911 "sync"
1012
1113 "github.com/fatih/color"
@@ -22,10 +24,10 @@ func Run(filePath string, moduleName string, port string) error {
2224 log .Fatalf ("error running offline: %s" , err )
2325 }
2426
25- // TODO: Validate config
27+ err = validateConfig ( terrableConfig )
2628
2729 if err != nil {
28- panic ( fmt .Errorf (" error parsing .terrable.toml file : %w" , err ))
30+ return fmt .Errorf (` error validating configuration : %s` , err . Error ( ))
2931 }
3032
3133 listener , activePort , err := getListener (port )
@@ -81,6 +83,24 @@ func Run(filePath string, moduleName string, port string) error {
8183 return nil
8284}
8385
86+ func validateConfig (config * config.TerrableConfig ) error {
87+ var errs []string
88+
89+ for _ , handler := range config .Handlers {
90+ for method , path := range handler .Http {
91+ if ! strings .HasPrefix (path , "/" ) {
92+ errs = append (errs , fmt .Sprintf ("Handler '%s' does not have a '/' prefix for the HTTP route %s '%s'." , handler .Name , method , path ))
93+ }
94+ }
95+ }
96+
97+ if len (errs ) > 0 {
98+ return errors .New (strings .Join (errs , "\n " ))
99+ }
100+
101+ return nil
102+ }
103+
84104func getListener (port string ) (net.Listener , int , error ) {
85105 var specificPortDesired bool = (port != "" )
86106
@@ -133,7 +153,7 @@ func printConfig(config config.TerrableConfig, port int) {
133153
134154 url := fmt .Sprintf ("%s%s" ,
135155 hostColor (fmt .Sprintf ("http://localhost:%d" , port )),
136- pathColor (utils . NormalisePath ( path ) ))
156+ pathColor (path ))
137157
138158 t .AppendRow (table.Row {
139159 methodColor (method ),
0 commit comments