flounder

A simple gemini site builder
Log | Files | Refs | README | LICENSE

commit 13bb0d3c427d66d8d1e717fd5a4bc59bac5088bf
parent 2dff9268624c71e83dc10145e6eb29058f3f0fb2
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Sat,  2 Jan 2021 10:54:34 -0800

Improve gemini log formatting

Diffstat:
Mgemini.go | 4++--
Mlog.go | 12++++++++++++
2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/gemini.go b/gemini.go @@ -53,7 +53,7 @@ func generateFolderPage(fullpath string) string { } func gmiIndex(w *gmi.ResponseWriter, r *gmi.Request) { - log.Println("Index request") + logGemini(r) // TODO move into wrapper t, err := template.ParseFiles("templates/index.gmi") if err != nil { log.Fatal(err) @@ -79,6 +79,7 @@ func gmiIndex(w *gmi.ResponseWriter, r *gmi.Request) { } func gmiPage(w *gmi.ResponseWriter, r *gmi.Request) { + logGemini(r) // TODO move into wrapper userName := filepath.Clean(strings.Split(r.URL.Host, ".")[0]) // clean probably unnecessary fileName := filepath.Clean(r.URL.Path) if fileName == "/" { @@ -87,7 +88,6 @@ func gmiPage(w *gmi.ResponseWriter, r *gmi.Request) { w.WriteStatus(gmi.StatusNotFound) return } - log.Println("Request for gemini file", fileName, "for user", userName) fullPath := path.Join(c.FilesDirectory, userName, fileName) if fileName == "/gemlog" { // temp hack _, err := os.Stat(path.Join(fullPath, "index.gmi")) diff --git a/log.go b/log.go @@ -1,8 +1,11 @@ package main import ( + "fmt" + gmi "git.sr.ht/~adnano/go-gemini" "github.com/gorilla/handlers" "io" + "log" "net" "net/http" "net/url" @@ -144,3 +147,12 @@ func appendQuoted(buf []byte, s string) []byte { // Parse logs and write to database // Anonymize user and IP? + +func logGemini(r *gmi.Request) { + line := fmt.Sprintf("gemini %s - [%s] %s %s\n", r.RemoteAddr.String(), + time.Now().Format("02/Jan/2006:15:04:05 -0700"), + r.URL.Host, + r.URL.Path) + buf := []byte(line) + log.Writer().Write(buf) +}