Add api router

main
DustyP 4 years ago
parent 1400b40ff9
commit e715ae40b1

@ -1,14 +1,24 @@
package main package main
import ( import (
"log"
"net/http"
"os" "os"
"github.com/dustinpianalto/quartermaster/internal/postgres" "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/dustinpianalto/quartermaster/pkg/services"
"github.com/gorilla/mux"
) )
func main() { func main() {
var migrationsVersion uint = 2 // Update when there is a new migration var migrationsVersion uint = 2 // Update when there is a new migration
postgres.ConnectDatabase(os.Getenv("DATABASE_URL"), migrationsVersion) postgres.ConnectDatabase(os.Getenv("DATABASE_URL"), migrationsVersion)
services.InitServices() services.InitServices()
router := mux.NewRouter().StrictSlash(true)
utils.Mount(router, "/api", api.GetRouter())
log.Fatal(http.ListenAndServe(":8000", router))
} }

@ -5,6 +5,7 @@ go 1.17
require ( require (
github.com/gobuffalo/here v0.6.0 // indirect github.com/gobuffalo/here v0.6.0 // indirect
github.com/golang-migrate/migrate/v4 v4.15.1 // 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/errwrap v1.0.0 // indirect
github.com/hashicorp/go-multierror v1.1.0 // indirect github.com/hashicorp/go-multierror v1.1.0 // indirect
github.com/lib/pq v1.10.4 // indirect github.com/lib/pq v1.10.4 // indirect

@ -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.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.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.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 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.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=

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

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

@ -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\"}")
}
Loading…
Cancel
Save