Compare commits

..

No commits in common. "513d0e57cecf0e592c40c136ee56aec2026de7a5" and "fb876226dde1fac6b4c45ae5247464790be87ec1" have entirely different histories.

11 changed files with 28 additions and 44 deletions

View file

@ -12,10 +12,8 @@ COPY go.mod go.sum ./
# Download dependencies # Download dependencies
RUN go mod download RUN go mod download
COPY ./*.go . COPY main.go .
COPY internal ./internal COPY internal ./internal
COPY pages ./pages
# Enable CGO for go-sqlite3 # Enable CGO for go-sqlite3
ENV CGO_ENABLED=1 ENV CGO_ENABLED=1
@ -28,6 +26,7 @@ WORKDIR /app
COPY --from=builder /app/main . COPY --from=builder /app/main .
COPY keys ./keys COPY keys ./keys
COPY pages ./pages
EXPOSE 8080 EXPOSE 8080

View file

@ -2,7 +2,6 @@ package main
import ( import (
"encoding/json" "encoding/json"
"errors"
"log/slog" "log/slog"
"os" "os"
"sync" "sync"
@ -19,13 +18,7 @@ type RequestCounter struct {
} }
// NewRequestCounter creates a new request counter with periodic snapshots // NewRequestCounter creates a new request counter with periodic snapshots
func NewRequestCounter(snapshotInterval time.Duration) (*RequestCounter, error) { func NewRequestCounter(snapshotPath string, snapshotInterval time.Duration) (*RequestCounter, error) {
snapshotPath := os.Getenv("REQUEST_COUNTS_PATH")
if snapshotPath == "" {
err := errors.New("REQUEST_COUNTS_PATH environment variable must be set and non-empty")
slog.Error("failed to initialize request counter", "error", err)
os.Exit(1)
}
c := &RequestCounter{ c := &RequestCounter{
counts: make(map[string]int64), counts: make(map[string]int64),
snapshotPath: snapshotPath, snapshotPath: snapshotPath,

View file

@ -1,7 +1,6 @@
package gemlog package gemlog
import ( import (
_ "embed"
"fmt" "fmt"
"log/slog" "log/slog"
"os" "os"
@ -52,14 +51,16 @@ func (g *Gemlog) HandleRequest(w gemini.ResponseWriter, req *gemini.Request) {
} }
} }
//go:embed gemlog.gmi
var pageContnet string
func (g *Gemlog) serveIndex(w gemini.ResponseWriter, req *gemini.Request) { func (g *Gemlog) serveIndex(w gemini.ResponseWriter, req *gemini.Request) {
w.WriteStatusMsg(gemini.StatusSuccess, "text/gemini") w.WriteStatusMsg(gemini.StatusSuccess, "text/gemini")
var content strings.Builder var content strings.Builder
content.Write([]byte(pageContnet)) page, err := os.ReadFile("./pages/gemlog.gmi")
if err != nil {
slog.Error("Problem reading gemlog page", "error", err)
return
}
content.Write(page)
content.WriteString("\n") content.WriteString("\n")
posts, err := gemlog.ListGemLogs(g.config) posts, err := gemlog.ListGemLogs(g.config)

View file

@ -4,10 +4,10 @@ import (
"crypto/sha256" "crypto/sha256"
"crypto/x509" "crypto/x509"
"database/sql" "database/sql"
_ "embed"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"log/slog" "log/slog"
"os"
"strings" "strings"
"time" "time"
@ -215,13 +215,15 @@ func censorString(s string) string {
return string(masked) return string(masked)
} }
//go:embed guestbook.gmi
var pageContnet string
func (book *GuestBook) serveIndex(w gemini.ResponseWriter, req *gemini.Request) { func (book *GuestBook) serveIndex(w gemini.ResponseWriter, req *gemini.Request) {
w.WriteStatusMsg(gemini.StatusSuccess, "text/gemini") w.WriteStatusMsg(gemini.StatusSuccess, "text/gemini")
var content strings.Builder var content strings.Builder
content.Write([]byte(pageContnet)) page, err := os.ReadFile("./pages/guestbook.gmi")
if err != nil {
slog.Error("Problem reading guestbook.gmi", "error", err)
return
}
content.Write(page)
content.WriteString("\n") content.WriteString("\n")
rows, err := book.db.Query("SELECT id, name, approved, created_at, message FROM guestbook ORDER BY created_at DESC") rows, err := book.db.Query("SELECT id, name, approved, created_at, message FROM guestbook ORDER BY created_at DESC")

View file

@ -1,11 +1,11 @@
package microblog package microblog
import ( import (
_ "embed"
"encoding/json" "encoding/json"
"fmt" "fmt"
"gemini_site/internal/pocketbase" "gemini_site/internal/pocketbase"
"log/slog" "log/slog"
"os"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
@ -216,9 +216,6 @@ func drawPost(builder *strings.Builder, p post) {
// p.Timestamp.Format("2006-01-02 15:04"))) // p.Timestamp.Format("2006-01-02 15:04")))
} }
//go:embed microblog.gmi
var pageContnet string
// serveBlogIndex serves the main blog page with recent posts // serveBlogIndex serves the main blog page with recent posts
func (mb *MicroBlog) serveIndex(w gemini.ResponseWriter, req *gemini.Request, pageNum int) { func (mb *MicroBlog) serveIndex(w gemini.ResponseWriter, req *gemini.Request, pageNum int) {
w.WriteStatusMsg(gemini.StatusSuccess, "text/gemini") w.WriteStatusMsg(gemini.StatusSuccess, "text/gemini")
@ -228,7 +225,12 @@ func (mb *MicroBlog) serveIndex(w gemini.ResponseWriter, req *gemini.Request, pa
// content.WriteString("Here are my microblog posts from various plantforms\n") // content.WriteString("Here are my microblog posts from various plantforms\n")
// Read and include the contents of ../../pages/microblog.gmi // Read and include the contents of ../../pages/microblog.gmi
if pageNum == 1 { if pageNum == 1 {
content.Write([]byte(pageContnet)) page, err := os.ReadFile("./pages/microblog.gmi")
if err != nil {
slog.Error("Problem reading microblog page", "error", err)
return
}
content.Write(page)
content.WriteString("\n") content.WriteString("\n")
} }

18
main.go
View file

@ -2,8 +2,6 @@ package main
import ( import (
"database/sql" "database/sql"
_ "embed"
"errors"
"flag" "flag"
"fmt" "fmt"
"log/slog" "log/slog"
@ -23,9 +21,6 @@ import (
gemini "github.com/kulak/gemini" gemini "github.com/kulak/gemini"
) )
//go:embed home.gmi
var homePageContent string
type MainHandler struct { type MainHandler struct {
blog microblog.Handler blog microblog.Handler
gemlog gemlog.Handler gemlog gemlog.Handler
@ -63,8 +58,11 @@ func (h MainHandler) ServeGemini(w gemini.ResponseWriter, req *gemini.Request) {
switch req.URL.Path { switch req.URL.Path {
case "/": case "/":
w.WriteStatusMsg(gemini.StatusSuccess, "text/gemini") w.WriteStatusMsg(gemini.StatusSuccess, "text/gemini")
data, err := os.ReadFile("pages/home.gmi")
requireNoError(err)
page := string(data)
var content strings.Builder var content strings.Builder
content.WriteString(homePageContent) content.WriteString(page)
content.WriteString(fmt.Sprintf("\n\n------ stats: total requests served %d, this page %d ------", h.counter.GetTotal(), h.counter.Get(req.URL.Path))) content.WriteString(fmt.Sprintf("\n\n------ stats: total requests served %d, this page %d ------", h.counter.GetTotal(), h.counter.Get(req.URL.Path)))
w.WriteBody([]byte(content.String())) w.WriteBody([]byte(content.String()))
// case "/user": // case "/user":
@ -143,13 +141,7 @@ func main() {
flag.Parse() flag.Parse()
// Initialize request counter with 30-second snapshot interval // Initialize request counter with 30-second snapshot interval
requestCountsPath := os.Getenv("REQUEST_COUNTS_PATH") counter, err := NewRequestCounter("request_counts.json", 30*time.Second)
if requestCountsPath == "" {
err = errors.New("REQUEST_COUNTS_PATH environment variable must be set and non-empty")
slog.Error("failed to initialize request counter", "error", err)
os.Exit(1)
}
counter, err := NewRequestCounter(30 * time.Second)
if err != nil { if err != nil {
slog.Error("failed to initialize request counter", "error", err) slog.Error("failed to initialize request counter", "error", err)
os.Exit(1) os.Exit(1)

View file

@ -1,5 +0,0 @@
task: add hexidecimal numbering to gemlog
-----------DONE LINE-----------
DONE task: add request counter