home | blog | art | now | git gpg | email | rss

guestbook

Code for a website guestbook. Written in Go.
git clone git://pollux.codes/git/guestbook.git
Log | Files | Refs | README | LICENSE
commit 354f35728e14fb6acd140ad751f5d3226a9356c5
parent 51ecc9b770a720b8cce0dc6145c5064df2c00b02
Author: Pollux <pollux@pollux.codes>
Date:   Sun,  1 Jun 2025 16:39:03 -0500

feat: Add configuration file

Signed-off-by: Pollux <pollux@pollux.codes>

Diffstat:
Aconfig.toml | 4++++
Mgo.mod | 5++++-
Mgo.sum | 2++
Aguestbook.html | 15+++++++++++++++
Mmain.go | 74+++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------
5 files changed, 76 insertions(+), 24 deletions(-)

diff --git a/config.toml b/config.toml @@ -0,0 +1,4 @@ +GetPath = "/" +PostPath = "/post" +DBFile = "guestbook.sqlite" +TemplateFile = "guestbook.html" diff --git a/go.mod b/go.mod @@ -2,7 +2,10 @@ module guestbook go 1.24.3 -require modernc.org/sqlite v1.37.1 +require ( + github.com/pelletier/go-toml/v2 v2.2.4 + modernc.org/sqlite v1.37.1 +) require ( github.com/dustin/go-humanize v1.0.1 // indirect diff --git a/go.sum b/go.sum @@ -8,6 +8,8 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4= github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls= +github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4= +github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= golang.org/x/exp v0.0.0-20250408133849-7e4ce0ab07d0 h1:R84qjqJb5nVJMxqWYb3np9L5ZsaDtB+a39EqjV0JSUM= diff --git a/guestbook.html b/guestbook.html @@ -0,0 +1,15 @@ +<html lang="en"> + <head> + </head> + <body> + <main> +{{ range . }} +{{ if .Website }} +<a href="{{ .Website }}">{{ .Name }}</a>: {{ .Message }} {{ .Time.Format "2006-01-02T15:04:05Z" }}<br> +{{ else }} +{{ .Name }}: {{ .Message }} {{ .Time.Format "2006-01-02T15:04:05Z" }}<br> +{{ end }} +{{ end }} + </main> + </body> +</html> diff --git a/main.go b/main.go @@ -6,29 +6,20 @@ import ( "html/template" "log" "net/http" + "os" "time" + _ "modernc.org/sqlite" -) -var database *sql.DB + "github.com/pelletier/go-toml/v2" +) -var message_template string = ` -<html lang="en"> - <head> - </head> - <body> - <main> -{{ range . }} -{{ if .Website }} -<a href="{{ .Website }}">{{ .Name }}</a>: {{ .Message }} {{ .Time.Format "2006-01-02T15:04:05Z" }}<br> -{{ else }} -{{ .Name }}: {{ .Message }} {{ .Time.Format "2006-01-02T15:04:05Z" }}<br> -{{ end }} -{{ end }} - </main> - </body> -</html> -` +type Config struct { + GetPath string + PostPath string + DBFile string + TemplateFile string +} type Message struct { Name string @@ -38,6 +29,10 @@ type Message struct { Time time.Time } +var cfg Config +var database *sql.DB +var message_template string + func GetGuestbook(w http.ResponseWriter, r *http.Request) { if r.Method != "GET" { @@ -127,9 +122,19 @@ func PostToGuestbook(w http.ResponseWriter, r *http.Request) { } func main() { - + var err error - database, err = sql.Open("sqlite", "guestbook.db") + + config_str, err := os.ReadFile("config.toml") + if err != nil { + log.Fatal(err) + } + err = toml.Unmarshal(config_str, &cfg) + if err != nil { + log.Fatal(err) + } + + database, err = sql.Open("sqlite", cfg.DBFile) if err != nil { log.Fatal(err) @@ -141,8 +146,31 @@ func main() { log.Fatal(err) } - http.HandleFunc("/", GetGuestbook) - http.HandleFunc("/post", PostToGuestbook) + _, table_check := database.Query("SELECT * FROM Messages;") + + if table_check != nil { + _, err = database.Exec(` + CREATE TABLE Messages ( + name VARCHAR(256) NOT NULL, + website VARCHAR(256), + email VARCHAR(256), + message VARCHAR(4096) NOT NULL, + time TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL + ) + `) + if err != nil { + log.Fatal(err) + } + } + + message_template_bytes, err := os.ReadFile(cfg.TemplateFile) + if err != nil { + log.Fatal(err) + } + message_template = string(message_template_bytes) + + http.HandleFunc(cfg.GetPath, GetGuestbook) + http.HandleFunc(cfg.PostPath, PostToGuestbook) fmt.Println("Starting server at port 48378") if err := http.ListenAndServe(":48378", nil); err != nil {