don't serve frontend from api server anymore

This commit is contained in:
Sam 2022-07-11 22:35:20 +02:00
parent e31f96110b
commit b9c30379ee
2 changed files with 9 additions and 77 deletions

35
.gitignore vendored
View file

@ -1,28 +1,9 @@
.env .vscode
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*
node_modules node_modules
frontend/dist/* *.log*
dist-ssr .nuxt
!frontend/dist/.empty .nitro
*.local .cache
frontend/style.css .output
.env
# Editor directories and files dist
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

View file

@ -2,19 +2,13 @@ package main
import ( import (
"context" "context"
"io/fs"
"net/http" "net/http"
"os" "os"
"os/signal" "os/signal"
"strings"
"codeberg.org/u1f320/pronouns.cc/backend/log" "codeberg.org/u1f320/pronouns.cc/backend/log"
"codeberg.org/u1f320/pronouns.cc/backend/server" "codeberg.org/u1f320/pronouns.cc/backend/server"
"codeberg.org/u1f320/pronouns.cc/frontend"
"emperror.dev/errors"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
_ "github.com/joho/godotenv/autoload" _ "github.com/joho/godotenv/autoload"
) )
@ -29,14 +23,11 @@ func main() {
// mount api routes // mount api routes
mountRoutes(s) mountRoutes(s)
r := chi.NewMux()
setupFrontend(r, s)
e := make(chan error) e := make(chan error)
// run server in another goroutine (for gracefully shutting down, see below) // run server in another goroutine (for gracefully shutting down, see below)
go func() { go func() {
e <- http.ListenAndServe(port, r) e <- http.ListenAndServe(port, s.Router)
}() }()
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt) ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
@ -53,43 +44,3 @@ func main() {
log.Fatalf("Error running server: %v", err) log.Fatalf("Error running server: %v", err)
} }
} }
func setupFrontend(r chi.Router, s *server.Server) {
r.Use(middleware.Recoverer)
r.Get("/@{user}", nil)
r.Get("/@{user}/{member}", nil)
r.Mount("/api", s.Router)
r.NotFound(func(w http.ResponseWriter, r *http.Request) {
data, err := frontend.Data.ReadFile("dist" + r.URL.Path)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
data, err = frontend.Data.ReadFile("dist/index.html")
if err != nil && !errors.Is(err, fs.ErrNotExist) {
log.Errorf("serving frontend file: %v", err)
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
http.Error(w, "Not found", http.StatusNotFound)
return
}
log.Errorf("serving frontend file: %v", err)
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
if strings.HasSuffix(r.URL.Path, ".js") {
w.Header().Add("content-type", "application/javascript")
} else if strings.HasSuffix(r.URL.Path, ".css") {
w.Header().Add("content-type", "text/css")
} else {
w.Header().Add("content-type", "text/html")
}
w.Write(data)
})
}