diff --git a/api/api.go b/api/api.go new file mode 100644 index 0000000..bec7d5d --- /dev/null +++ b/api/api.go @@ -0,0 +1,17 @@ +package api + +import "github.com/gorilla/mux" + +// NewRouter returns an HTTP handler (net.Handler) which can then +// be used by the net/http package to serve the api of our API. +// We use gorilla/mux because it is more convenient than net/http, +// e.g. when extracting path parameters. +func NewRouter() *mux.Router { + router := mux.NewRouter() + // this can later be restricted to a specific host with + // `router.Host(...)` and to HTTPS with `router.Schemes("https")` + v1 := router.PathPrefix("/api/v1").Subrouter() + v1.HandleFunc("/health", Health).Methods("GET") + + return v1 +} diff --git a/go.mod b/go.mod index bfac499..fc96fd0 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module gitlab.hpi.de/codeocean/codemoon/coolcodeoceannomadmiddleware go 1.16 -require github.com/stretchr/testify v1.7.0 +require ( + github.com/gorilla/mux v1.8.0 + github.com/stretchr/testify v1.7.0 +) diff --git a/go.sum b/go.sum index acb88a4..5f1152c 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= +github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= diff --git a/main.go b/main.go index 1af468d..d3df11b 100644 --- a/main.go +++ b/main.go @@ -1,12 +1,14 @@ package main -import "fmt" - -// HelloCodeMoon returns 'Hello, CodeMoon!' -func HelloCodeMoon() string { - return "Hello, CodeMoon!" -} +import ( + "gitlab.hpi.de/codeocean/codemoon/coolcodeoceannomadmiddleware/api" + "log" + "net/http" +) func main() { - fmt.Println(HelloCodeMoon()) + err := http.ListenAndServe("0.0.0.0:4000", api.NewRouter()) + if err != nil { + log.Fatal("Error during listening and serving: ", err) + } } diff --git a/main_test.go b/main_test.go deleted file mode 100644 index ae6c997..0000000 --- a/main_test.go +++ /dev/null @@ -1,10 +0,0 @@ -package main - -import ( - "github.com/stretchr/testify/assert" - "testing" -) - -func TestHelloCodeMoon(t *testing.T) { - assert.Equal(t, "Hello, CodeMoon!", HelloCodeMoon()) -}