+ Reading from files

+ Working Page access
This commit is contained in:
Daniel Legt 2022-05-20 00:46:14 +03:00
parent a3de00aabd
commit a76b9d02ba
10 changed files with 177 additions and 39 deletions

View File

@ -4,8 +4,14 @@
DOMAIN_BASE=http://localhost:8080
## ? Functionality Changes, these are optional but recommended to setup as you like
# The maximum map caching, from my testing 15 would reach 250mb of ram usage on docker
CACHE_MAP_LIMIT=15
# The folder in which to store all of the pads, please leave a trailing slash
PAD_STORAGE_PATH=./data/
# Maximum API call requests to the API, this will ban the api for 5 minutes after requesting more than API_RATE_LIMIT in 5 minutes.
API_BAN_LIMIT=300
# Wether or not to run it all in dev-mode
DEV_MODE=0

3
.gitignore vendored
View File

@ -1,4 +1,5 @@
dev/*
!dev/.keep
.env
docker-compose.yaml
docker-compose.yaml
data/Hello World

6
go.mod
View File

@ -2,4 +2,8 @@ module github.com/JustKato/FreePad
go 1.15
require github.com/gin-gonic/gin v1.7.7
require (
github.com/gin-gonic/gin v1.7.7
github.com/joho/godotenv v1.4.0
github.com/mrz1836/go-sanitize v1.1.5 // indirect
)

6
go.sum
View File

@ -16,6 +16,8 @@ github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn
github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I=
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
@ -26,12 +28,15 @@ github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OH
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/mrz1836/go-sanitize v1.1.5 h1:LOywG3ijK/B/D9ik3hsniyIzA1JVZlM2wmp3Q/CBk88=
github.com/mrz1836/go-sanitize v1.1.5/go.mod h1:HnnbbJTcBhbr770WyRL4SA95I4FFOnGg/RTLJybsuN8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
@ -52,3 +57,4 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -0,0 +1,69 @@
package objects
import (
"fmt"
"os"
)
type Post struct {
Name string `json:"name"`
Content string `json:"content"`
}
func getStorageDirectory() string {
baseStoragePath, exists := os.LookupEnv("PAD_STORAGE_PATH")
if !exists {
baseStoragePath = "/tmp/"
}
// Check if the base storage path exists
if _, err := os.Stat(baseStoragePath); os.IsNotExist(err) {
// Looks like the base storage path was NOT set, create the dir
err = os.Mkdir(baseStoragePath, 0777)
// Check for errors
if err != nil {
// No way this sends an error unless it goes horribly wrong.
panic(err)
}
}
// Return the base storage path
return baseStoragePath
}
func GetPost(fileName string) Post {
// Get the base storage directory and make sure it exists
storageDir := getStorageDirectory()
// Generate the file path
filePath := fmt.Sprintf("%s%s", storageDir, fileName)
fmt.Println("Reading: ", filePath)
p := Post{
Name: fileName,
Content: "",
}
// Check if the file exits
if _, err := os.Stat(filePath); !os.IsNotExist(err) {
fmt.Println("Found ", filePath)
// File does exist, read it and set the content
data, err := os.ReadFile(filePath)
if err != nil {
fmt.Println("Error:", err)
}
// Get the content of the file and put it in the response
p.Content = string(data)
fmt.Println("Loaded content for ", filePath)
}
return p
}
func WritePost(p Post) error {
return nil
}

View File

@ -1,8 +1,13 @@
package routes
import (
"fmt"
"net/url"
"github.com/JustKato/FreePad/lib/helper"
"github.com/JustKato/FreePad/lib/objects"
"github.com/gin-gonic/gin"
"github.com/mrz1836/go-sanitize"
)
func HomeRoutes(router *gin.Engine) {
@ -18,9 +23,22 @@ func HomeRoutes(router *gin.Engine) {
// Get the post we are looking for.
postName := c.Param("post")
fmt.Println("Sanitizing ", postName)
// Sanitize the postName
newPostName, err := url.QueryUnescape(postName)
if err == nil {
postName = newPostName
}
postName = sanitize.AlphaNumeric(postName, true)
fmt.Println("Fetching ", postName)
post := objects.GetPost(postName)
c.HTML(200, "page.html", gin.H{
"title": postName,
"post_content": "",
"post_content": post.Content,
"domain_base": helper.GetDomainBase(),
})
})

View File

@ -1,6 +0,0 @@
package types
type Post struct {
Name string `json:"name"`
Content string `json:"content"`
}

View File

@ -3,19 +3,23 @@ package main
import (
"os"
"github.com/JustKato/FreePad/lib/controllers"
"github.com/JustKato/FreePad/lib/routes"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
)
func main() {
// Load environment variables, ignore if any errors come up
godotenv.Load()
_, isDevelopment := os.LookupEnv("DEV_MODE")
if isDevelopment {
gin.SetMode(gin.ReleaseMode)
}
// Run the TaskManager
go controllers.TaskManager()
// go controllers.TaskManager()
// Initialize the router
router := gin.Default()

View File

@ -8,27 +8,32 @@
<main id="main-card" class="container rounded mt-5 shadow-sm">
<div class="p-3">
<div class="logo-container w-100 d-flex mb-4">
<img src="/static/img/logo_transparent.png" alt="Logo" style="max-width: 50%; margin: 0 auto;"
class="mx-auto">
<img src="/static/img/logo_transparent.png" alt="Logo" style="max-width: 50%; margin: 0 auto;" class="mx-auto">
</div>
<div class="form-group my-4">
<div class="search-action input-group">
<input type="text" class="form-control form-control-lg" placeholder="What's your pad?"
aria-label="What's your pad?" aria-describedby="pad-name-button" id="pad-name">
<button class="btn btn-primary" type="button" id="pad-name-button">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="currentColor"
class="bi bi-box-arrow-in-down" viewBox="0 0 16 16">
<path fill-rule="evenodd"
d="M3.5 6a.5.5 0 0 0-.5.5v8a.5.5 0 0 0 .5.5h9a.5.5 0 0 0 .5-.5v-8a.5.5 0 0 0-.5-.5h-2a.5.5 0 0 1 0-1h2A1.5 1.5 0 0 1 14 6.5v8a1.5 1.5 0 0 1-1.5 1.5h-9A1.5 1.5 0 0 1 2 14.5v-8A1.5 1.5 0 0 1 3.5 5h2a.5.5 0 0 1 0 1h-2z" />
<path fill-rule="evenodd"
d="M7.646 11.854a.5.5 0 0 0 .708 0l3-3a.5.5 0 0 0-.708-.708L8.5 10.293V1.5a.5.5 0 0 0-1 0v8.793L5.354 8.146a.5.5 0 1 0-.708.708l3 3z" />
<form class="search-action input-group" onsubmit="goToPad(); return false;">
<input type="text" class="form-control form-control-lg" placeholder="What's your pad?" aria-label="What's your pad?" aria-describedby="pad-name-button" id="pad-name">
<button class="btn btn-primary" type="submit" id="pad-name-button">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="currentColor" class="bi bi-box-arrow-in-down" viewBox="0 0 16 16">
<path fill-rule="evenodd" d="M3.5 6a.5.5 0 0 0-.5.5v8a.5.5 0 0 0 .5.5h9a.5.5 0 0 0 .5-.5v-8a.5.5 0 0 0-.5-.5h-2a.5.5 0 0 1 0-1h2A1.5 1.5 0 0 1 14 6.5v8a1.5 1.5 0 0 1-1.5 1.5h-9A1.5 1.5 0 0 1 2 14.5v-8A1.5 1.5 0 0 1 3.5 5h2a.5.5 0 0 1 0 1h-2z" />
<path fill-rule="evenodd" d="M7.646 11.854a.5.5 0 0 0 .708 0l3-3a.5.5 0 0 0-.708-.708L8.5 10.293V1.5a.5.5 0 0 0-1 0v8.793L5.354 8.146a.5.5 0 1 0-.708.708l3 3z" />
</svg>
</button>
</div>
</form>
</div>
<script>
function goToPad() {
// Go to the next apd
window.location.href = "/" + document.getElementById(`pad-name`).value;
}
</script>
<div class="why mb-4">
<p>
Ever wanted to transport information across platforms

View File

@ -1,29 +1,60 @@
{{ template "inc/header.html" .}}
<style>
#pad-content {
height: 16rem;
}
</style>
<body>
<main id="main-card" class="container rounded mt-5 shadow-sm">
<div class="p-3">
<div class="why mb-4">
<p>
[PLACEHOLDER TEXT] [PLACEHOLDER TEXT] [PLACEHOLDER TEXT]
[PLACEHOLDER TEXT] [PLACEHOLDER TEXT] [PLACEHOLDER TEXT]
[PLACEHOLDER TEXT] [PLACEHOLDER TEXT] [PLACEHOLDER TEXT]
[PLACEHOLDER TEXT] [PLACEHOLDER TEXT] [PLACEHOLDER TEXT]
[PLACEHOLDER TEXT] [PLACEHOLDER TEXT] [PLACEHOLDER TEXT]
[PLACEHOLDER TEXT] [PLACEHOLDER TEXT] [PLACEHOLDER TEXT]
[PLACEHOLDER TEXT] [PLACEHOLDER TEXT] [PLACEHOLDER TEXT]
[PLACEHOLDER TEXT] [PLACEHOLDER TEXT] [PLACEHOLDER TEXT]
[PLACEHOLDER TEXT] [PLACEHOLDER TEXT] [PLACEHOLDER TEXT]
[PLACEHOLDER TEXT] [PLACEHOLDER TEXT] [PLACEHOLDER TEXT]
</p>
<div class="logo-container w-100 d-flex mb-4">
<img src="/static/img/logo_transparent.png" alt="Logo" style="max-width: 50%; margin: 0 auto;" class="mx-auto">
</div>
</div>
<textarea name="pad-content" id="pad-content"
class="form-control">{{.post_content}}</textarea>
<div id="pad-status" class="my-4 row">
<div class="col-md-12 col-lg-6 col-xl-6" title="Current Viewers">
<div class="input-group">
<span class="input-group-text">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-eye" viewBox="0 0 16 16">
<path d="M16 8s-3-5.5-8-5.5S0 8 0 8s3 5.5 8 5.5S16 8 16 8zM1.173 8a13.133 13.133 0 0 1 1.66-2.043C4.12 4.668 5.88 3.5 8 3.5c2.12 0 3.879 1.168 5.168 2.457A13.133 13.133 0 0 1 14.828 8c-.058.087-.122.183-.195.288-.335.48-.83 1.12-1.465 1.755C11.879 11.332 10.119 12.5 8 12.5c-2.12 0-3.879-1.168-5.168-2.457A13.134 13.134 0 0 1 1.172 8z"></path>
<path d="M8 5.5a2.5 2.5 0 1 0 0 5 2.5 2.5 0 0 0 0-5zM4.5 8a3.5 3.5 0 1 1 7 0 3.5 3.5 0 0 1-7 0z"></path>
</svg>
</span>
<input type="text" class="form-control" readonly value="1">
</div>
</div>
<div class="col-md-12 col-lg-6 col-xl-6 mt-4 mt-lg-0 mt-xl-0" title="Last Modified">
<div class="input-group">
<span class="input-group-text">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-hourglass-split" viewBox="0 0 16 16">
<path d="M2.5 15a.5.5 0 1 1 0-1h1v-1a4.5 4.5 0 0 1 2.557-4.06c.29-.139.443-.377.443-.59v-.7c0-.213-.154-.451-.443-.59A4.5 4.5 0 0 1 3.5 3V2h-1a.5.5 0 0 1 0-1h11a.5.5 0 0 1 0 1h-1v1a4.5 4.5 0 0 1-2.557 4.06c-.29.139-.443.377-.443.59v.7c0 .213.154.451.443.59A4.5 4.5 0 0 1 12.5 13v1h1a.5.5 0 0 1 0 1h-11zm2-13v1c0 .537.12 1.045.337 1.5h6.326c.216-.455.337-.963.337-1.5V2h-7zm3 6.35c0 .701-.478 1.236-1.011 1.492A3.5 3.5 0 0 0 4.5 13s.866-1.299 3-1.48V8.35zm1 0v3.17c2.134.181 3 1.48 3 1.48a3.5 3.5 0 0 0-1.989-3.158C8.978 9.586 8.5 9.052 8.5 8.351z"/>
</svg>
</span>
<input type="text" class="form-control" readonly value="Never Before">
</div>
</div>
</div>
<footer class="text-muted py-5 text-center">
<p class="mb-1">
FreePad by <a href="https://justkato.me/">©Kato Twofold</a>
</p>
<p class="mb-0">
FreePad is freely available over on our <a href="https://github.com/JustKato/FreePad">GitHub</a>
</p>
</footer>
</main>
{{ template "inc/theme-toggle.html" .}}