Compare commits
2 commits
fb876226dd
...
513d0e57ce
| Author | SHA1 | Date | |
|---|---|---|---|
| 513d0e57ce | |||
| be89141c06 |
11 changed files with 44 additions and 28 deletions
|
|
@ -12,8 +12,10 @@ COPY go.mod go.sum ./
|
|||
# Download dependencies
|
||||
RUN go mod download
|
||||
|
||||
COPY main.go .
|
||||
COPY ./*.go .
|
||||
COPY internal ./internal
|
||||
COPY pages ./pages
|
||||
|
||||
|
||||
# Enable CGO for go-sqlite3
|
||||
ENV CGO_ENABLED=1
|
||||
|
|
@ -26,7 +28,6 @@ WORKDIR /app
|
|||
|
||||
COPY --from=builder /app/main .
|
||||
COPY keys ./keys
|
||||
COPY pages ./pages
|
||||
|
||||
EXPOSE 8080
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"os"
|
||||
"sync"
|
||||
|
|
@ -18,7 +19,13 @@ type RequestCounter struct {
|
|||
}
|
||||
|
||||
// NewRequestCounter creates a new request counter with periodic snapshots
|
||||
func NewRequestCounter(snapshotPath string, snapshotInterval time.Duration) (*RequestCounter, error) {
|
||||
func NewRequestCounter(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{
|
||||
counts: make(map[string]int64),
|
||||
snapshotPath: snapshotPath,
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package gemlog
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
|
|
@ -51,16 +52,14 @@ 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) {
|
||||
w.WriteStatusMsg(gemini.StatusSuccess, "text/gemini")
|
||||
|
||||
var content strings.Builder
|
||||
page, err := os.ReadFile("./pages/gemlog.gmi")
|
||||
if err != nil {
|
||||
slog.Error("Problem reading gemlog page", "error", err)
|
||||
return
|
||||
}
|
||||
content.Write(page)
|
||||
content.Write([]byte(pageContnet))
|
||||
content.WriteString("\n")
|
||||
|
||||
posts, err := gemlog.ListGemLogs(g.config)
|
||||
|
|
|
|||
|
|
@ -4,10 +4,10 @@ import (
|
|||
"crypto/sha256"
|
||||
"crypto/x509"
|
||||
"database/sql"
|
||||
_ "embed"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
|
|
@ -215,15 +215,13 @@ func censorString(s string) string {
|
|||
return string(masked)
|
||||
}
|
||||
|
||||
//go:embed guestbook.gmi
|
||||
var pageContnet string
|
||||
|
||||
func (book *GuestBook) serveIndex(w gemini.ResponseWriter, req *gemini.Request) {
|
||||
w.WriteStatusMsg(gemini.StatusSuccess, "text/gemini")
|
||||
var content strings.Builder
|
||||
page, err := os.ReadFile("./pages/guestbook.gmi")
|
||||
if err != nil {
|
||||
slog.Error("Problem reading guestbook.gmi", "error", err)
|
||||
return
|
||||
}
|
||||
content.Write(page)
|
||||
content.Write([]byte(pageContnet))
|
||||
content.WriteString("\n")
|
||||
|
||||
rows, err := book.db.Query("SELECT id, name, approved, created_at, message FROM guestbook ORDER BY created_at DESC")
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
package microblog
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"gemini_site/internal/pocketbase"
|
||||
"log/slog"
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
|
@ -216,6 +216,9 @@ func drawPost(builder *strings.Builder, p post) {
|
|||
// p.Timestamp.Format("2006-01-02 15:04")))
|
||||
}
|
||||
|
||||
//go:embed microblog.gmi
|
||||
var pageContnet string
|
||||
|
||||
// serveBlogIndex serves the main blog page with recent posts
|
||||
func (mb *MicroBlog) serveIndex(w gemini.ResponseWriter, req *gemini.Request, pageNum int) {
|
||||
w.WriteStatusMsg(gemini.StatusSuccess, "text/gemini")
|
||||
|
|
@ -225,12 +228,7 @@ func (mb *MicroBlog) serveIndex(w gemini.ResponseWriter, req *gemini.Request, pa
|
|||
// content.WriteString("Here are my microblog posts from various plantforms\n")
|
||||
// Read and include the contents of ../../pages/microblog.gmi
|
||||
if pageNum == 1 {
|
||||
page, err := os.ReadFile("./pages/microblog.gmi")
|
||||
if err != nil {
|
||||
slog.Error("Problem reading microblog page", "error", err)
|
||||
return
|
||||
}
|
||||
content.Write(page)
|
||||
content.Write([]byte(pageContnet))
|
||||
content.WriteString("\n")
|
||||
}
|
||||
|
||||
|
|
|
|||
18
main.go
18
main.go
|
|
@ -2,6 +2,8 @@ package main
|
|||
|
||||
import (
|
||||
"database/sql"
|
||||
_ "embed"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
|
@ -21,6 +23,9 @@ import (
|
|||
gemini "github.com/kulak/gemini"
|
||||
)
|
||||
|
||||
//go:embed home.gmi
|
||||
var homePageContent string
|
||||
|
||||
type MainHandler struct {
|
||||
blog microblog.Handler
|
||||
gemlog gemlog.Handler
|
||||
|
|
@ -58,11 +63,8 @@ func (h MainHandler) ServeGemini(w gemini.ResponseWriter, req *gemini.Request) {
|
|||
switch req.URL.Path {
|
||||
case "/":
|
||||
w.WriteStatusMsg(gemini.StatusSuccess, "text/gemini")
|
||||
data, err := os.ReadFile("pages/home.gmi")
|
||||
requireNoError(err)
|
||||
page := string(data)
|
||||
var content strings.Builder
|
||||
content.WriteString(page)
|
||||
content.WriteString(homePageContent)
|
||||
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()))
|
||||
// case "/user":
|
||||
|
|
@ -141,7 +143,13 @@ func main() {
|
|||
flag.Parse()
|
||||
|
||||
// Initialize request counter with 30-second snapshot interval
|
||||
counter, err := NewRequestCounter("request_counts.json", 30*time.Second)
|
||||
requestCountsPath := os.Getenv("REQUEST_COUNTS_PATH")
|
||||
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 {
|
||||
slog.Error("failed to initialize request counter", "error", err)
|
||||
os.Exit(1)
|
||||
|
|
|
|||
5
tasks.txt
Normal file
5
tasks.txt
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
task: add hexidecimal numbering to gemlog
|
||||
|
||||
-----------DONE LINE-----------
|
||||
|
||||
DONE task: add request counter
|
||||
Loading…
Add table
Add a link
Reference in a new issue