diff --git a/cmd/quartermaster/main.go b/cmd/quartermaster/main.go index 8c2562b..6e1b327 100644 --- a/cmd/quartermaster/main.go +++ b/cmd/quartermaster/main.go @@ -1,14 +1,24 @@ package main import ( + "log" + "net/http" "os" "github.com/dustinpianalto/quartermaster/internal/postgres" + "github.com/dustinpianalto/quartermaster/internal/utils" + "github.com/dustinpianalto/quartermaster/pkg/api" "github.com/dustinpianalto/quartermaster/pkg/services" + "github.com/gorilla/mux" ) func main() { var migrationsVersion uint = 2 // Update when there is a new migration postgres.ConnectDatabase(os.Getenv("DATABASE_URL"), migrationsVersion) services.InitServices() + + router := mux.NewRouter().StrictSlash(true) + utils.Mount(router, "/api", api.GetRouter()) + + log.Fatal(http.ListenAndServe(":8000", router)) } diff --git a/go.mod b/go.mod index 67f033e..3887853 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.17 require ( github.com/gobuffalo/here v0.6.0 // indirect github.com/golang-migrate/migrate/v4 v4.15.1 // indirect + github.com/gorilla/mux v1.8.0 // indirect github.com/hashicorp/errwrap v1.0.0 // indirect github.com/hashicorp/go-multierror v1.1.0 // indirect github.com/lib/pq v1.10.4 // indirect diff --git a/go.sum b/go.sum index 77ba76f..d38a678 100644 --- a/go.sum +++ b/go.sum @@ -492,6 +492,8 @@ github.com/gorilla/handlers v1.4.2/go.mod h1:Qkdc/uu4tH4g6mTK6auzZ766c4CA0Ng8+o/ github.com/gorilla/mux v1.7.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= +github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= +github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= diff --git a/internal/utils/utils.go b/internal/utils/utils.go new file mode 100644 index 0000000..8268802 --- /dev/null +++ b/internal/utils/utils.go @@ -0,0 +1,17 @@ +package utils + +import ( + "net/http" + "strings" + + "github.com/gorilla/mux" +) + +func Mount(r *mux.Router, path string, handler http.Handler) { + r.PathPrefix(path).Handler( + http.StripPrefix( + strings.TrimSuffix(path, "/"), + handler, + ), + ) +} diff --git a/pkg/api/api.go b/pkg/api/api.go new file mode 100644 index 0000000..42360cc --- /dev/null +++ b/pkg/api/api.go @@ -0,0 +1,12 @@ +package api + +import ( + "github.com/gorilla/mux" +) + +func GetRouter() *mux.Router { + router := mux.NewRouter().StrictSlash(true) + router.HandleFunc("/", homePage).Methods("GET") + router.HandleFunc("/healthcheck", healthcheck).Methods("GET") + return router +} diff --git a/pkg/api/home.go b/pkg/api/home.go new file mode 100644 index 0000000..3561bd4 --- /dev/null +++ b/pkg/api/home.go @@ -0,0 +1,21 @@ +package api + +import ( + "fmt" + "log" + "net/http" +) + +func homePage(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", "application/json") + fmt.Fprintf(w, "{\"status\": \"success\"}") + log.Println("{\"endpoint\": \"/\", \"status\": \"success\"}") +} + +func healthcheck(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", "application/json") + fmt.Fprintf(w, "{\"status\": \"success\"}") + log.Println("{\"endpoint\": \"/healthcheck\", \"status\": \"success\"}") +}