diff --git a/.gitignore b/.gitignore index 23e1857..0653454 100644 --- a/.gitignore +++ b/.gitignore @@ -6,7 +6,7 @@ *.db # Build -*.exe +bin # Environment .env \ No newline at end of file diff --git a/api/page.go b/api/page.go index 37baf48..36a7ff6 100644 --- a/api/page.go +++ b/api/page.go @@ -2,15 +2,23 @@ package api import ( "bytes" + "embed" "encoding/json" "github.com/go-chi/chi/v5" "html/template" + "io/fs" "net/http" "os" "path/filepath" "strings" ) +//go:embed web/templates/* +var templateFiles embed.FS + +//go:embed web/static +var staticFolder embed.FS + var contentTypes = map[string]string{ ".css": "text/css", ".js": "application/javascript", @@ -21,6 +29,8 @@ var contentTypes = map[string]string{ ".ico": "image/x-icon", } +const loginRoute = "/login" + type LoginPage struct { IsError bool `json:"isError"` Error string `json:"error"` @@ -37,7 +47,7 @@ func NewLoginPage(error string, username string) *LoginPage { func getPage(page string, data interface{}) ([]byte, error) { var processedHTML bytes.Buffer - tmpl, err := template.ParseFiles("./web/templates/" + page + ".html") + tmpl, err := template.ParseFS(templateFiles, "web/templates/"+page+".html") if err != nil { return nil, err } @@ -104,48 +114,52 @@ func getHomePage(w http.ResponseWriter, _ *http.Request) { } func getLogin(w http.ResponseWriter, r *http.Request) { - http.Redirect(w, r, "/login", http.StatusPermanentRedirect) + http.Redirect(w, r, loginRoute, http.StatusPermanentRedirect) } func getLogout(w http.ResponseWriter, r *http.Request) { removeJwtCookie(w) - http.Redirect(w, r, "/login", http.StatusSeeOther) + http.Redirect(w, r, loginRoute, http.StatusSeeOther) } func IsLoggedInMiddleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if !IsLoggedIn(r) { - http.Redirect(w, r, "/login", http.StatusSeeOther) + http.Redirect(w, r, loginRoute, http.StatusSeeOther) return } next.ServeHTTP(w, r) }) } -func staticFileServerWithContentType(dir http.Dir) http.Handler { +func staticFileServerWithContentType(fsys http.FileSystem) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - requestedFilePath := r.URL.Path - fileExtension := filepath.Ext(requestedFilePath) - - if contentType, ok := contentTypes[fileExtension]; ok { - w.Header().Set("Content-Type", contentType) + path := r.URL.Path + if ext := filepath.Ext(path); ext != "" { + if ct, ok := contentTypes[ext]; ok { + w.Header().Set("Content-Type", ct) + } } - - http.FileServer(dir).ServeHTTP(w, r) + http.FileServer(fsys).ServeHTTP(w, r) }) } func NewPageRouter() http.Handler { r := chi.NewRouter() - r.Handle("/static/*", http.StripPrefix("/static/", staticFileServerWithContentType("./web/static"))) + contentStatic, _ := fs.Sub(fs.FS(staticFolder), "web/static") + + r.Handle("/static/*", staticFileServerWithContentType(http.FS(contentStatic))) r.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) { - http.ServeFile(w, r, "./web/static/favicon.ico") + _, err := http.FS(staticFolder).Open("favicon.ico") + if err != nil { + return + } }) r.Get("/", getLogin) - r.Get("/login", getLoginPageHandler(NewLoginPage("", ""))) - r.Post("/login", getLoginPageHandler(NewLoginPage("", ""))) + r.Get(loginRoute, getLoginPageHandler(NewLoginPage("", ""))) + r.Post(loginRoute, getLoginPageHandler(NewLoginPage("", ""))) r.Get("/logout", getLogout) r.Group(func(r chi.Router) { diff --git a/web/static/bootstrap.min.css b/api/web/static/bootstrap.min.css similarity index 100% rename from web/static/bootstrap.min.css rename to api/web/static/bootstrap.min.css diff --git a/web/static/bootstrap.min.css.map b/api/web/static/bootstrap.min.css.map similarity index 100% rename from web/static/bootstrap.min.css.map rename to api/web/static/bootstrap.min.css.map diff --git a/web/static/bootstrap.min.js b/api/web/static/bootstrap.min.js similarity index 100% rename from web/static/bootstrap.min.js rename to api/web/static/bootstrap.min.js diff --git a/web/static/bootstrap.min.js.map b/api/web/static/bootstrap.min.js.map similarity index 100% rename from web/static/bootstrap.min.js.map rename to api/web/static/bootstrap.min.js.map diff --git a/web/static/favicon.ico b/api/web/static/favicon.ico similarity index 100% rename from web/static/favicon.ico rename to api/web/static/favicon.ico diff --git a/web/static/gohcms-favicon-128.png b/api/web/static/gohcms-favicon-128.png similarity index 100% rename from web/static/gohcms-favicon-128.png rename to api/web/static/gohcms-favicon-128.png diff --git a/web/templates/componentNavbar.html b/api/web/templates/componentNavbar.html similarity index 100% rename from web/templates/componentNavbar.html rename to api/web/templates/componentNavbar.html diff --git a/web/templates/login.html b/api/web/templates/login.html similarity index 100% rename from web/templates/login.html rename to api/web/templates/login.html diff --git a/super_build.bat b/super_build.bat new file mode 100644 index 0000000..535db9f --- /dev/null +++ b/super_build.bat @@ -0,0 +1,37 @@ +@echo off + +setlocal + +set "GOARCH=amd64" +set "OUTPUT_DIR=./bin" + +set "GO_FILE_PATH=./main/main.go" +set "PROGRAM_NAME=GohCMS" + +REM Compile for Windows +set "GOOS=windows" +go build -o "%OUTPUT_DIR%\%PROGRAM_NAME%_windows.exe" "%GO_FILE_PATH%" +if %errorlevel% neq 0 ( + echo Compilation for Windows failed. + exit /b %errorlevel% +) + +REM Compile for Linux +set "GOOS=linux" +go build -o "%OUTPUT_DIR%\%PROGRAM_NAME%_linux" "%GO_FILE_PATH%" +if %errorlevel% neq 0 ( + echo Compilation for Linux failed. + exit /b %errorlevel% +) + +REM Compile for macOS +set "GOOS=darwin" +go build -o "%OUTPUT_DIR%\%PROGRAM_NAME%_darwin" "%GO_FILE_PATH%" +if %errorlevel% neq 0 ( + echo Compilation for macOS failed. + exit /b %errorlevel% +) + +echo Compilation successful for all platforms. + +endlocal diff --git a/super_build.sh b/super_build.sh new file mode 100644 index 0000000..7e44e4f --- /dev/null +++ b/super_build.sh @@ -0,0 +1,30 @@ +#!/bin/sh + +export GOARCH=amd64 +OUTPUT_DIR=./bin + +GO_FILE_PATH="./main/main.go" +PROGRAM_NAME=GohCMS + +# Compile for Windows +GOOS=windows go build -o "$OUTPUT_DIR/${PROGRAM_NAME}_windows.exe" "$GO_FILE_PATH" +if [ $? -ne 0 ]; then + echo "Compilation for Windows failed." + exit $? +fi + +# Compile for Linux +GOOS=linux go build -o "$OUTPUT_DIR/${PROGRAM_NAME}_linux" "$GO_FILE_PATH" +if [ $? -ne 0 ]; then + echo "Compilation for Linux failed." + exit $? +fi + +# Compile for macOS +GOOS=darwin go build -o "$OUTPUT_DIR/${PROGRAM_NAME}_darwin" "$GO_FILE_PATH" +if [ $? -ne 0 ]; then + echo "Compilation for macOS failed." + exit $? +fi + +echo "Compilation successful for all platforms."