parent
1400b40ff9
commit
e715ae40b1
@ -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))
|
||||
}
|
||||
|
||||
@ -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…
Reference in new issue