2023-07-11 07:04:19 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-07-14 04:39:08 +00:00
|
|
|
"html/template"
|
2023-07-11 07:04:19 +00:00
|
|
|
"log"
|
|
|
|
"math/big"
|
|
|
|
"net/http"
|
2023-07-13 04:59:01 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2023-07-11 07:04:19 +00:00
|
|
|
|
|
|
|
"crypto/rand"
|
|
|
|
"database/sql"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
dsn = "/mnt/volume_tor1_01/gutenberg/chunker.db?cache=shared&mode=r"
|
|
|
|
maxID = 9739473
|
|
|
|
)
|
|
|
|
|
|
|
|
func connectDB() (*sql.DB, error) {
|
|
|
|
db, err := sql.Open("sqlite3", dsn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return db, nil
|
|
|
|
}
|
|
|
|
|
2023-07-14 04:39:08 +00:00
|
|
|
type payload struct {
|
|
|
|
ID int64
|
|
|
|
MaxID int
|
2023-07-11 07:04:19 +00:00
|
|
|
Chunk string
|
2023-07-13 04:59:01 +00:00
|
|
|
Tokens []string
|
2023-07-11 07:04:19 +00:00
|
|
|
Name string
|
|
|
|
Author string
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
r := gin.Default()
|
2023-07-14 04:39:08 +00:00
|
|
|
r.SetFuncMap(template.FuncMap{
|
|
|
|
"upper": strings.ToUpper,
|
|
|
|
})
|
2023-07-11 07:42:27 +00:00
|
|
|
r.LoadHTMLFiles("templates/index.tmpl")
|
2023-07-14 04:39:08 +00:00
|
|
|
r.StaticFile("/favicon.ico", "./assets/favicon.ico")
|
2023-07-11 07:04:19 +00:00
|
|
|
|
|
|
|
randMax := big.NewInt(maxID)
|
|
|
|
|
2023-07-13 04:59:01 +00:00
|
|
|
spaceRE := regexp.MustCompile(`[\t\v\f\r ]+`)
|
2023-07-18 06:37:37 +00:00
|
|
|
r.HEAD("/", func(c *gin.Context) {
|
|
|
|
c.String(http.StatusOK, "")
|
|
|
|
})
|
2023-07-13 04:59:01 +00:00
|
|
|
|
2023-07-11 07:04:19 +00:00
|
|
|
r.GET("/", func(c *gin.Context) {
|
|
|
|
db, err := connectDB()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err.Error())
|
|
|
|
c.String(http.StatusInternalServerError, "oh no.")
|
|
|
|
return
|
|
|
|
}
|
2023-07-18 06:37:56 +00:00
|
|
|
defer db.Close()
|
2023-07-11 07:04:19 +00:00
|
|
|
|
|
|
|
id, err := rand.Int(rand.Reader, randMax)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err.Error())
|
|
|
|
c.String(http.StatusInternalServerError, "oh no.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
stmt, err := db.Prepare("select c.chunk, f.name, f.author from chunks c join files f on c.sourceid = f.id where c.id = ?")
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err.Error())
|
|
|
|
c.String(http.StatusInternalServerError, "oh no.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
row := stmt.QueryRow(id.Int64())
|
2023-07-14 04:39:08 +00:00
|
|
|
var dest payload
|
2023-07-11 07:04:19 +00:00
|
|
|
err = row.Scan(&dest.Chunk, &dest.Name, &dest.Author)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err.Error())
|
|
|
|
c.String(http.StatusInternalServerError, "oh no.")
|
|
|
|
}
|
|
|
|
|
2023-07-14 04:39:08 +00:00
|
|
|
if dest.Author == "" {
|
|
|
|
dest.Author = "Unknown"
|
|
|
|
}
|
|
|
|
|
|
|
|
dest.MaxID = maxID
|
|
|
|
dest.ID = id.Int64()
|
|
|
|
|
2023-07-13 04:59:01 +00:00
|
|
|
dest.Tokens = []string{}
|
|
|
|
for _, t := range spaceRE.Split(dest.Chunk, -1) {
|
|
|
|
if t == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.Contains(t, "\n") {
|
|
|
|
sp := strings.Split(t, "\n")
|
|
|
|
for x, s := range sp {
|
|
|
|
nl := "\n"
|
|
|
|
if x == len(sp)-1 {
|
|
|
|
nl = ""
|
|
|
|
}
|
|
|
|
dest.Tokens = append(dest.Tokens, s+nl)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dest.Tokens = append(dest.Tokens, t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-11 07:42:27 +00:00
|
|
|
c.HTML(http.StatusOK, "index.tmpl", dest)
|
2023-07-11 07:04:19 +00:00
|
|
|
})
|
|
|
|
r.Run() // 8080
|
|
|
|
}
|