Skip to content

Commit

Permalink
added static and template embedding, updated build
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-D-Karpov committed Jul 16, 2024
1 parent c3bee12 commit cf8bee0
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 20 deletions.
54 changes: 39 additions & 15 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,48 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: Go
name: Release

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]
branches:
- master

jobs:
build:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.22'

- name: Build
run: |
go build -v -o webring cmd/server/main.go
- name: Create Release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.sha }}
release_name: Release ${{ github.sha }}
draft: false
prerelease: false

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.22'
- name: Upload Release Asset
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./webring
asset_name: webring
asset_content_type: application/octet-stream

- name: Build
run: go build -v cmd/server/main.go
- name: Upload Binary to Repository
uses: actions/upload-artifact@v2
with:
name: webring
path: webring
18 changes: 17 additions & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package main
import (
"database/sql"
"fmt"
"html/template"
"io"
"io/fs"
"log"
"net/http"
"os"
"path/filepath"
"webring"

"webring/internal/api"
"webring/internal/dashboard"
Expand Down Expand Up @@ -81,7 +84,20 @@ func main() {
dashboard.RegisterHandlers(r, db)

// Serve static files
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
staticFiles, err := fs.Sub(webring.Files, "static")
if err != nil {
log.Fatalf("Error accessing static files: %v", err)
}
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.FS(staticFiles))))

// Parse templates
t, err := template.ParseFS(webring.Files, "internal/dashboard/templates/*.html")
if err != nil {
log.Fatalf("Error parsing templates: %v", err)
}

// Initialize dashboard templates
dashboard.InitTemplates(t)

port := os.Getenv("PORT")
if port == "" {
Expand Down
8 changes: 8 additions & 0 deletions embedded.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package webring

import (
"embed"
)

//go:embed static internal/dashboard/templates
var Files embed.FS
26 changes: 22 additions & 4 deletions internal/dashboard/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,22 @@ import (
"net/http"
"os"
"strconv"
"sync"

"webring/internal/models"

"github.com/gorilla/mux"
)

var templates *template.Template
var (
templates *template.Template
templatesMu sync.RWMutex
)

func init() {
templates = template.Must(template.ParseGlob("internal/dashboard/templates/*.html"))
func InitTemplates(t *template.Template) {
templatesMu.Lock()
defer templatesMu.Unlock()
templates = t
}

func RegisterHandlers(r *mux.Router, db *sql.DB) {
Expand All @@ -44,14 +50,26 @@ func basicAuthMiddleware(next http.Handler) http.Handler {

func dashboardHandler(db *sql.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
templatesMu.RLock()
t := templates
templatesMu.RUnlock()

if t == nil {
log.Println("Templates not initialized")
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}

sites, err := getAllSites(db)
if err != nil {
log.Printf("Error fetching sites: %v", err)
http.Error(w, "Error fetching sites", http.StatusInternalServerError)
return
}

err = templates.ExecuteTemplate(w, "dashboard.html", sites)
err = t.ExecuteTemplate(w, "dashboard.html", sites)
if err != nil {
log.Printf("Error rendering template: %v", err)
http.Error(w, "Error rendering template", http.StatusInternalServerError)
}
}
Expand Down

0 comments on commit cf8bee0

Please sign in to comment.