minibin/server/main.go

122 lines
2.4 KiB
Go
Raw Normal View History

2024-02-19 16:12:57 +01:00
package main
import (
"embed"
2024-02-19 16:12:57 +01:00
"flag"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"database/sql"
2024-02-19 16:12:57 +01:00
"log"
"math/rand"
"net/http"
"time"
_ "github.com/mattn/go-sqlite3"
)
var db *sql.DB
var dbFilePath string
var port string
2024-02-19 16:12:57 +01:00
type Bin struct {
Content string `json:"content"`
Language string `json:"language"`
}
const (
shortIDCharset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
shortIDLength = 8
)
var (
//go:embed all:dist
dist embed.FS
)
2024-02-19 16:12:57 +01:00
func RegisterHandlers(e *echo.Echo) {
e.Use(middleware.StaticWithConfig(middleware.StaticConfig{
Skipper: nil,
Root: "dist",
Index: "index.html",
HTML5: true,
Filesystem: http.FS(dist),
}))
e.Use(middleware.CORS())
e.POST("/bin", postBin)
e.GET("/bin/:id", getBin)
2024-02-19 16:12:57 +01:00
}
func main() {
flag.StringVar(&port, "port", "8080", "HTTP server port")
flag.StringVar(&dbFilePath, "db", "minibin.db", "Path to SQLite database file")
flag.Parse()
2024-02-19 16:12:57 +01:00
initDatabase()
e := echo.New()
RegisterHandlers(e)
e.Logger.Fatal(e.Start(":" + port))
2024-02-19 16:12:57 +01:00
}
func initDatabase() {
var err error
db, err = sql.Open("sqlite3", dbFilePath)
if err != nil {
log.Fatal(err)
}
err = createTable()
if err != nil {
log.Fatal(err)
}
}
func postBin(echoContext echo.Context) error {
bin := Bin{}
err := echoContext.Bind(&bin)
if err != nil {
return err
2024-02-19 16:12:57 +01:00
}
id := generateShortID()
err = saveBin(id, bin)
2024-02-19 16:12:57 +01:00
if err != nil {
return err
2024-02-19 16:12:57 +01:00
}
return echoContext.JSON(http.StatusCreated, echo.Map{
"id": id,
})
2024-02-19 16:12:57 +01:00
}
func getBin(echoContext echo.Context) error {
id := echoContext.Param("id")
bin, err := getBinById(id)
if err != nil {
return err
}
return echoContext.JSON(http.StatusOK, bin)
2024-02-19 16:12:57 +01:00
}
func createTable() error {
_, err := db.Exec("CREATE TABLE IF NOT EXISTS bins (id TEXT PRIMARY KEY, content TEXT, language TEXT)")
2024-02-19 16:12:57 +01:00
return err
}
func getBinById(id string) (Bin, error) {
row := db.QueryRow("SELECT content, language FROM bins WHERE id = ?", id)
bin := Bin{}
err := row.Scan(&bin.Content, &bin.Language)
2024-02-19 16:12:57 +01:00
return bin, err
}
func saveBin(id string, bin Bin) error {
_, err := db.Exec("INSERT INTO bins (id, content, language) VALUES (?, ?, ?)", id, bin.Content, bin.Language)
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))]
}
return string(id)
}