2020-07-05 01:02:57 +02:00
|
|
|
package route
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-chi/chi"
|
|
|
|
"github.com/go-chi/chi/middleware"
|
|
|
|
"github.com/go-chi/cors"
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2020-08-07 03:50:35 +02:00
|
|
|
"github.com/jordanknott/taskcafe/internal/config"
|
|
|
|
"github.com/jordanknott/taskcafe/internal/db"
|
|
|
|
"github.com/jordanknott/taskcafe/internal/frontend"
|
|
|
|
"github.com/jordanknott/taskcafe/internal/graph"
|
|
|
|
"github.com/jordanknott/taskcafe/internal/logger"
|
2020-07-16 01:20:08 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2020-07-05 01:02:57 +02:00
|
|
|
)
|
|
|
|
|
2020-07-16 01:20:08 +02:00
|
|
|
// spaHandler implements the http.Handler interface, so we can use it
|
|
|
|
// to respond to HTTP requests. The path to the static directory and
|
|
|
|
// path to the index file within that static directory are used to
|
|
|
|
// serve the SPA in the given static directory.
|
|
|
|
type FrontendHandler struct {
|
|
|
|
staticPath string
|
|
|
|
indexPath string
|
|
|
|
}
|
|
|
|
|
|
|
|
func IsDir(f http.File) bool {
|
|
|
|
fi, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return fi.IsDir()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h FrontendHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
path, err := filepath.Abs(r.URL.Path)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := frontend.Frontend.Open(path)
|
|
|
|
if os.IsNotExist(err) || IsDir(f) {
|
|
|
|
index, err := frontend.Frontend.Open("index.html")
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
http.ServeContent(w, r, "index.html", time.Now(), index)
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
http.ServeContent(w, r, path, time.Now(), f)
|
|
|
|
}
|
|
|
|
|
2020-08-07 03:50:35 +02:00
|
|
|
type TaskcafeHandler struct {
|
2020-07-16 01:20:08 +02:00
|
|
|
config config.AppConfig
|
|
|
|
repo db.Repository
|
2020-07-05 01:02:57 +02:00
|
|
|
}
|
|
|
|
|
2020-07-16 01:20:08 +02:00
|
|
|
func NewRouter(config config.AppConfig, dbConnection *sqlx.DB) (chi.Router, error) {
|
2020-07-05 01:02:57 +02:00
|
|
|
formatter := new(log.TextFormatter)
|
|
|
|
formatter.TimestampFormat = "02-01-2006 15:04:05"
|
|
|
|
formatter.FullTimestamp = true
|
|
|
|
|
|
|
|
routerLogger := log.New()
|
|
|
|
routerLogger.SetLevel(log.InfoLevel)
|
|
|
|
routerLogger.Formatter = formatter
|
|
|
|
r := chi.NewRouter()
|
|
|
|
cors := cors.New(cors.Options{
|
|
|
|
// AllowedOrigins: []string{"https://foo.com"}, // Use this to allow specific origin hosts
|
|
|
|
AllowedOrigins: []string{"*"},
|
|
|
|
// AllowOriginFunc: func(r *http.Request, origin string) bool { return true },
|
|
|
|
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
|
|
|
|
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token", "Cookie"},
|
|
|
|
ExposedHeaders: []string{"Link"},
|
|
|
|
AllowCredentials: true,
|
|
|
|
MaxAge: 300, // Maximum value not ignored by any of major browsers
|
|
|
|
})
|
|
|
|
|
|
|
|
r.Use(cors.Handler)
|
|
|
|
r.Use(middleware.RequestID)
|
|
|
|
r.Use(middleware.RealIP)
|
|
|
|
r.Use(logger.NewStructuredLogger(routerLogger))
|
|
|
|
r.Use(middleware.Recoverer)
|
|
|
|
r.Use(middleware.Timeout(60 * time.Second))
|
|
|
|
|
|
|
|
repository := db.NewRepository(dbConnection)
|
2020-08-07 03:50:35 +02:00
|
|
|
taskcafeHandler := TaskcafeHandler{config, *repository}
|
2020-07-05 01:02:57 +02:00
|
|
|
|
|
|
|
var imgServer = http.FileServer(http.Dir("./uploads/"))
|
|
|
|
r.Group(func(mux chi.Router) {
|
2020-08-07 03:50:35 +02:00
|
|
|
mux.Mount("/auth", authResource{}.Routes(taskcafeHandler))
|
2020-07-05 01:02:57 +02:00
|
|
|
mux.Handle("/__graphql", graph.NewPlaygroundHandler("/graphql"))
|
|
|
|
mux.Mount("/uploads/", http.StripPrefix("/uploads/", imgServer))
|
|
|
|
|
|
|
|
})
|
|
|
|
r.Group(func(mux chi.Router) {
|
|
|
|
mux.Use(AuthenticationMiddleware)
|
2020-08-07 03:50:35 +02:00
|
|
|
mux.Post("/users/me/avatar", taskcafeHandler.ProfileImageUpload)
|
|
|
|
mux.Post("/auth/install", taskcafeHandler.InstallHandler)
|
2020-07-16 01:20:08 +02:00
|
|
|
mux.Handle("/graphql", graph.NewHandler(config, *repository))
|
2020-07-05 01:02:57 +02:00
|
|
|
})
|
|
|
|
|
2020-07-16 01:20:08 +02:00
|
|
|
frontend := FrontendHandler{staticPath: "build", indexPath: "index.html"}
|
|
|
|
r.Handle("/*", frontend)
|
|
|
|
|
2020-07-05 01:02:57 +02:00
|
|
|
return r, nil
|
|
|
|
}
|