diff options
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/dump.go | 85 | ||||
-rw-r--r-- | cmd/root.go | 51 | ||||
-rw-r--r-- | cmd/serve.go | 125 |
3 files changed, 261 insertions, 0 deletions
diff --git a/cmd/dump.go b/cmd/dump.go new file mode 100644 index 0000000..281b7d6 --- /dev/null +++ b/cmd/dump.go @@ -0,0 +1,85 @@ +/* guestbook - Standalone guestbook server for static websites + * Copyright (C) 2025 Vidhu Kant Sharma <vidhukant@vidhukant.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +package cmd + +import ( + "fmt" + "log" + "bytes" + "text/template" + + "vidhukant.com/guestbook/db" + + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +var dumpCmd = &cobra.Command{ + Use: "dump", + Short: "Dump the entries into STDOUT", + Long: "Dump the entries into STDOUT", + Run: func(cmd *cobra.Command, args []string) { + guestbookName, err := cmd.Flags().GetString("guestbook") + if err != nil { + log.Fatal(err) + } + + outputTemplate := viper.GetString(guestbookName + ".template") + + conn := db.Connect() + defer conn.Close() + + rows, err := conn.Query(fmt.Sprintf("SELECT name, website, message FROM %s", guestbookName)) + if err != nil { + log.Fatal(err) + } + defer rows.Close() + + + var entries []db.GuestbookEntry + + for rows.Next() { + var entry db.GuestbookEntry + if err := rows.Scan(&entry.Name, &entry.Website, &entry.Message); err != nil { + log.Fatal(err) + } + entries = append(entries, entry) + } + if rows.Err() != nil { + log.Fatal(err) + } + + tmpl, err := template.New("render").Parse(outputTemplate) + if err != nil { + log.Fatal(err) + } + + var buf bytes.Buffer + if err := tmpl.Execute(&buf, entries); err != nil { + panic(err) + } + + fmt.Println(buf.String()) + }, +} + +func init() { + rootCmd.AddCommand(dumpCmd) + + dumpCmd.Flags().StringP("guestbook", "g", "my_guestbook", "Guestbook name (defined in /etc/guestbook.toml)") +} diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 0000000..2d1e63d --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,51 @@ +/* guestbook - Standalone guestbook server for static websites + * Copyright (C) 2025 Vidhu Kant Sharma <vidhukant@vidhukant.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +package cmd + +import ( + "os" + "log" + + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +var rootCmd = &cobra.Command{ + Use: "guestbook", + Version: "v1.0.0", + Short: "Standalone guestbook server for static websites", + Long: "Standalone guestbook server for static websites", +} + +func Execute() { + err := rootCmd.Execute() + if err != nil { + os.Exit(1) + } +} + +func init() { + cobra.OnInitialize(func () { + viper.SetConfigName("guestbook") + viper.AddConfigPath("/etc") + + if err := viper.ReadInConfig(); err != nil { + log.Fatal(err) + } + }) +} diff --git a/cmd/serve.go b/cmd/serve.go new file mode 100644 index 0000000..e3d6b36 --- /dev/null +++ b/cmd/serve.go @@ -0,0 +1,125 @@ +/* guestbook - Standalone guestbook server for static websites + * Copyright (C) 2025 Vidhu Kant Sharma <vidhukant@vidhukant.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +package cmd + +import ( + "fmt" + "log" + "net/http" + "encoding/json" + + "vidhukant.com/guestbook/db" + + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +var serveCmd = &cobra.Command{ + Use: "serve", + Short: "Start the guestbook server", + Long: "Start the guestbook server", + Run: func(cmd *cobra.Command, args []string) { + guestbookName, err := cmd.Flags().GetString("guestbook") + if err != nil { + log.Fatal(err) + } + + port := viper.GetString(guestbookName + ".port") + validationString := viper.GetString(guestbookName + ".validation_string") + + path := viper.GetString(guestbookName + ".path") + if path == "/" { + log.Fatalf("Setting %s.path to / is not allowed as it may cause issues.", guestbookName) + } + + successRedirect := viper.GetString(guestbookName + ".success_redirect") + if successRedirect == "" { + log.Fatalf("%s.success_redirect cannot be blank.", guestbookName) + } + + errorRedirect := viper.GetString(guestbookName + ".error_redirect") + if errorRedirect == "" { + log.Fatalf("%s.error_redirect cannot be blank.", guestbookName) + } + + + conn := db.Connect() + defer conn.Close() + + _, err = conn.Exec( + fmt.Sprintf( + "CREATE TABLE IF NOT EXISTS %s (" + + "id INT AUTO_INCREMENT PRIMARY KEY," + + "name TEXT," + + "website TEXT," + + "message TEXT" + + ");", + guestbookName, + ), + ) + if err != nil { + log.Fatalf("Error while creating table: %v\n", err) + } + + http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + http.Redirect(w, r, errorRedirect, http.StatusSeeOther) + return + } + + var data db.GuestbookEntry + err := json.NewDecoder(r.Body).Decode(&data) + if err != nil { + log.Printf("Error occurred while decoding data: %v") + http.Redirect(w, r, errorRedirect, http.StatusSeeOther) + return + } + + if data.Validation != validationString || data.Name == "" || data.Message == "" { + http.Redirect(w, r, errorRedirect, http.StatusSeeOther) + return + } + + stmt, err := conn.Prepare(fmt.Sprintf("INSERT INTO %s (name, website, message) VALUES (?, ?, ?)", guestbookName)) + if err != nil { + log.Printf("Error occurred while preparing statement data: %v\n") + http.Redirect(w, r, errorRedirect, http.StatusSeeOther) + return + } + defer stmt.Close() + + _, err = stmt.Exec(data.Name, data.Website, data.Message) + if err != nil { + log.Printf("Error occurred while saving data: %v\n") + http.Redirect(w, r, errorRedirect, http.StatusSeeOther) + return + } + + http.Redirect(w, r, successRedirect, http.StatusSeeOther) + }) + + log.Printf("Guestbook %s listening to POST %s on PORT %s.\n", guestbookName, path, port) + log.Fatal(http.ListenAndServe(":" + port, nil)) + }, +} + +func init() { + rootCmd.AddCommand(serveCmd) + + serveCmd.Flags().StringP("guestbook", "g", "my_guestbook", "Guestbook name (defined in /etc/guestbook.toml)") +} |