feat: Add build scripts, refactor for binary embedding

This commit is contained in:
Florian Sylvain committed 2023-08-12 19:42:27 +02:00
1 parent 047e14df60
commit 7e8b415cab
12 files changed
+98 -17

No files matched your search

+1 -1
View File
@@ -6,7 +6,7 @@
*.db *.db
# Build # Build
*.exe bin
# Environment # Environment
.env .env
+30 -16
View File
@@ -2,15 +2,23 @@ package api
import ( import (
"bytes" "bytes"
"embed"
"encoding/json" "encoding/json"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
"html/template" "html/template"
"io/fs"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
) )
//go:embed web/templates/*
var templateFiles embed.FS
//go:embed web/static
var staticFolder embed.FS
var contentTypes = map[string]string{ var contentTypes = map[string]string{
".css": "text/css", ".css": "text/css",
".js": "application/javascript", ".js": "application/javascript",
@@ -21,6 +29,8 @@ var contentTypes = map[string]string{
".ico": "image/x-icon", ".ico": "image/x-icon",
} }
const loginRoute = "/login"
type LoginPage struct { type LoginPage struct {
IsError bool `json:"isError"` IsError bool `json:"isError"`
Error string `json:"error"` Error string `json:"error"`
@@ -37,7 +47,7 @@ func NewLoginPage(error string, username string) *LoginPage {
func getPage(page string, data interface{}) ([]byte, error) { func getPage(page string, data interface{}) ([]byte, error) {
var processedHTML bytes.Buffer var processedHTML bytes.Buffer
tmpl, err := template.ParseFiles("./web/templates/" + page + ".html") tmpl, err := template.ParseFS(templateFiles, "web/templates/"+page+".html")
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -104,48 +114,52 @@ func getHomePage(w http.ResponseWriter, _ *http.Request) {
} }
func getLogin(w http.ResponseWriter, r *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) { func getLogout(w http.ResponseWriter, r *http.Request) {
removeJwtCookie(w) removeJwtCookie(w)
http.Redirect(w, r, "/login", http.StatusSeeOther) http.Redirect(w, r, loginRoute, http.StatusSeeOther)
} }
func IsLoggedInMiddleware(next http.Handler) http.Handler { func IsLoggedInMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !IsLoggedIn(r) { if !IsLoggedIn(r) {
http.Redirect(w, r, "/login", http.StatusSeeOther) http.Redirect(w, r, loginRoute, http.StatusSeeOther)
return return
} }
next.ServeHTTP(w, r) 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) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestedFilePath := r.URL.Path path := r.URL.Path
fileExtension := filepath.Ext(requestedFilePath) if ext := filepath.Ext(path); ext != "" {
if ct, ok := contentTypes[ext]; ok {
if contentType, ok := contentTypes[fileExtension]; ok { w.Header().Set("Content-Type", ct)
w.Header().Set("Content-Type", contentType)
} }
}
http.FileServer(dir).ServeHTTP(w, r) http.FileServer(fsys).ServeHTTP(w, r)
}) })
} }
func NewPageRouter() http.Handler { func NewPageRouter() http.Handler {
r := chi.NewRouter() 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) { 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("/", getLogin)
r.Get("/login", getLoginPageHandler(NewLoginPage("", ""))) r.Get(loginRoute, getLoginPageHandler(NewLoginPage("", "")))
r.Post("/login", getLoginPageHandler(NewLoginPage("", ""))) r.Post(loginRoute, getLoginPageHandler(NewLoginPage("", "")))
r.Get("/logout", getLogout) r.Get("/logout", getLogout)
r.Group(func(r chi.Router) { r.Group(func(r chi.Router) {
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
+37
View File
@@ -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
+30
View File
@@ -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."