-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
19ddc31
commit cc63744
Showing
6 changed files
with
120 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,7 @@ | |
uuid-server | ||
|
||
# Coverage | ||
coverage.txt | ||
coverage.txt | ||
|
||
# Releases | ||
dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,9 @@ changelog: | |
exclude: | ||
- '^docs:' | ||
- '^test:' | ||
release: | ||
github: | ||
owner: liampulles | ||
name: uuid-server | ||
prerelease: auto | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package run | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
|
||
googleUuid "github.com/google/uuid" | ||
"github.com/liampulles/go-config" | ||
|
||
"github.com/liampulles/uuid-server/pkg/api" | ||
uuidConfig "github.com/liampulles/uuid-server/pkg/config" | ||
"github.com/liampulles/uuid-server/pkg/logger" | ||
"github.com/liampulles/uuid-server/pkg/uuid" | ||
) | ||
|
||
// Run will run the uuid-server | ||
func Run(source config.Source, listenAndServe func(string, http.Handler) error) int { | ||
cfg, err := uuidConfig.InitUUIDServerConfig(source) | ||
if err != nil { | ||
fmt.Printf("Error parsing config: %v", err) | ||
return 1 | ||
} | ||
|
||
logSvc := logger.NewServiceImpl(cfg.LogLevel, log.Printf) | ||
uuidSvc := uuid.NewServiceImpl(googleUuid.NewRandom) | ||
handler := api.NewUUIDHandler(logSvc, uuidSvc) | ||
|
||
logSvc.Infof("Now serving on port %d!", cfg.Port) | ||
if err := listenAndServe(fmt.Sprintf(":%d", cfg.Port), handler); err != nil { | ||
fmt.Printf("Error during serving: %v", err) | ||
return 2 | ||
} | ||
return 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package run_test | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/liampulles/go-config" | ||
"github.com/liampulles/uuid-server/pkg/run" | ||
) | ||
|
||
func TestRun_WhenConfigFails_ShouldReturnOne(t *testing.T) { | ||
// Setup fixture | ||
source := config.MapSource(map[string]string{ | ||
"PORT": "not an int", | ||
}) | ||
expected := 1 | ||
|
||
// Exercise SUT | ||
actual := run.Run(source, nil) | ||
|
||
// Verify results | ||
if actual != expected { | ||
t.Errorf("Unexpected response\nActual: %d\nExpected: %d", actual, expected) | ||
} | ||
} | ||
|
||
func TestRun_WhenServeFails_ShouldReturnTwo(t *testing.T) { | ||
// Setup fixture | ||
source := config.MapSource(map[string]string{}) | ||
serveFunc := func(string, http.Handler) error { | ||
return fmt.Errorf("some error") | ||
} | ||
expected := 2 | ||
|
||
// Exercise SUT | ||
actual := run.Run(source, serveFunc) | ||
|
||
// Verify results | ||
if actual != expected { | ||
t.Errorf("Unexpected response\nActual: %d\nExpected: %d", actual, expected) | ||
} | ||
} | ||
|
||
func TestRun_WhenServeSucceeds_ShouldReturnZero(t *testing.T) { | ||
// Setup fixture | ||
source := config.MapSource(map[string]string{}) | ||
serveFunc := func(string, http.Handler) error { | ||
return nil | ||
} | ||
expected := 0 | ||
|
||
// Exercise SUT | ||
actual := run.Run(source, serveFunc) | ||
|
||
// Verify results | ||
if actual != expected { | ||
t.Errorf("Unexpected response\nActual: %d\nExpected: %d", actual, expected) | ||
} | ||
} |