Add api package serving our api routes using gorilla/mux

This commit is contained in:
Felix Auringer
2021-04-26 12:29:31 +02:00
parent 08eb05e00f
commit 38ecc02a79
5 changed files with 32 additions and 18 deletions

17
api/api.go Normal file
View File

@ -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
}

5
go.mod
View File

@ -2,4 +2,7 @@ module gitlab.hpi.de/codeocean/codemoon/coolcodeoceannomadmiddleware
go 1.16 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
)

2
go.sum
View File

@ -1,5 +1,7 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= 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/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 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 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= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

16
main.go
View File

@ -1,12 +1,14 @@
package main package main
import "fmt" import (
"gitlab.hpi.de/codeocean/codemoon/coolcodeoceannomadmiddleware/api"
// HelloCodeMoon returns 'Hello, CodeMoon!' "log"
func HelloCodeMoon() string { "net/http"
return "Hello, CodeMoon!" )
}
func main() { 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)
}
} }

View File

@ -1,10 +0,0 @@
package main
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestHelloCodeMoon(t *testing.T) {
assert.Equal(t, "Hello, CodeMoon!", HelloCodeMoon())
}