diff options
Diffstat (limited to 'server/main.go')
-rw-r--r-- | server/main.go | 73 |
1 files changed, 34 insertions, 39 deletions
diff --git a/server/main.go b/server/main.go index 5aaeba7..92bce1d 100644 --- a/server/main.go +++ b/server/main.go @@ -7,37 +7,36 @@ import ( "log" "math/rand" "net/http" - "time" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" - _ "github.com/mattn/go-sqlite3" ) -var db *sql.DB -var dbFilePath string -var port string +var ( + db *sql.DB + dbFilePath string + port string + shortIDCharset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" + shortIDLength = 8 +) type Bin struct { Content string `json:"content"` Language string `json:"language"` - IV string `json:"iv"` + IV string `json:"iv"` } const ( - shortIDCharset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" - shortIDLength = 8 + insertQuery = "INSERT INTO bins (id, content, language, iv) VALUES (?, ?, ?, ?)" + selectQuery = "SELECT content, language, iv FROM bins WHERE id = ?" ) -var ( - //go:embed all:dist - dist embed.FS -) +//go:embed all:dist +var dist embed.FS func RegisterHandlers(e *echo.Echo) { e.Use(middleware.StaticWithConfig(middleware.StaticConfig{ - Skipper: nil, Root: "dist", Index: "index.html", HTML5: true, @@ -55,6 +54,7 @@ func main() { flag.Parse() initDatabase() + e := echo.New() RegisterHandlers(e) e.Logger.Fatal(e.Start(":" + port)) @@ -67,47 +67,43 @@ func initDatabase() { log.Fatal(err) } - err = createTable() - if err != nil { + if err := createTable(); err != nil { log.Fatal(err) } } -func postBin(echoContext echo.Context) error { - bin := Bin{} - err := echoContext.Bind(&bin) - if err != nil { +func postBin(c echo.Context) error { + var bin Bin + if err := c.Bind(&bin); err != nil { return err } + id := generateShortID() - err = saveBin(id, bin) - if err != nil { + if err := saveBin(id, bin); err != nil { return err } - return echoContext.JSON(http.StatusCreated, echo.Map{ - "id": id, - }) + + return c.JSON(http.StatusCreated, echo.Map{"id": id}) } -func getBin(echoContext echo.Context) error { - id := echoContext.Param("id") - bin, err := getBinById(id) +func getBin(c echo.Context) error { + id := c.Param("id") + bin, err := getBinByID(id) if err != nil { return err } - return echoContext.JSON(http.StatusOK, bin) + return c.JSON(http.StatusOK, bin) } -func redirectToURL(echoContext echo.Context) error { - id := echoContext.Param("id") - bin, err := getBinById(id) +func redirectToURL(c echo.Context) error { + id := c.Param("id") + bin, err := getBinByID(id) if err != nil { - echoContext.Logger().Error(err) + c.Logger().Error(err) return err } - url := bin.Content - return echoContext.Redirect(http.StatusFound, url) + return c.Redirect(http.StatusFound, bin.Content) } func createTable() error { @@ -115,20 +111,19 @@ func createTable() error { return err } -func getBinById(id string) (Bin, error) { - row := db.QueryRow("SELECT content, language, iv FROM bins WHERE id = ?", id) - bin := Bin{} +func getBinByID(id string) (Bin, error) { + var bin Bin + row := db.QueryRow(selectQuery, id) err := row.Scan(&bin.Content, &bin.Language, &bin.IV) return bin, err } func saveBin(id string, bin Bin) error { - _, err := db.Exec("INSERT INTO bins (id, content, language, iv) VALUES (?, ?, ?, ?)", id, bin.Content, bin.Language, bin.IV) + _, err := db.Exec(insertQuery, id, bin.Content, bin.Language, bin.IV) return err } func generateShortID() string { - rand.Seed(time.Now().UnixNano()) id := make([]byte, shortIDLength) for i := range id { id[i] = shortIDCharset[rand.Intn(len(shortIDCharset))] |