diff --git a/.env.example b/.env.example index 22fbde8..6a28fcb 100644 --- a/.env.example +++ b/.env.example @@ -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 \ No newline at end of file diff --git a/.gitignore b/.gitignore index 41c7a0e..595e7d2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ dev/* !dev/.keep .env -docker-compose.yaml \ No newline at end of file +docker-compose.yaml +data/Hello World diff --git a/go.mod b/go.mod index 0c368d5..6f1a39e 100644 --- a/go.mod +++ b/go.mod @@ -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 +) diff --git a/go.sum b/go.sum index 5ee9be1..d905fde 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/lib/objects/objects_post.go b/lib/objects/objects_post.go new file mode 100644 index 0000000..ce3c2aa --- /dev/null +++ b/lib/objects/objects_post.go @@ -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 +} diff --git a/lib/routes/routes_home.go b/lib/routes/routes_home.go index ecdfa4d..240ab85 100644 --- a/lib/routes/routes_home.go +++ b/lib/routes/routes_home.go @@ -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(), }) }) diff --git a/lib/types/types_post.go b/lib/types/types_post.go deleted file mode 100644 index f1f4505..0000000 --- a/lib/types/types_post.go +++ /dev/null @@ -1,6 +0,0 @@ -package types - -type Post struct { - Name string `json:"name"` - Content string `json:"content"` -} diff --git a/main.go b/main.go index 01643de..c080fba 100644 --- a/main.go +++ b/main.go @@ -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() diff --git a/templates/pages/index.html b/templates/pages/index.html index 6f80e9f..a29d737 100644 --- a/templates/pages/index.html +++ b/templates/pages/index.html @@ -8,27 +8,32 @@
+
- Logo + Logo
-
- -
+ +
+ +

Ever wanted to transport information across platforms diff --git a/templates/pages/page.html b/templates/pages/page.html index 05b350f..8cf6459 100644 --- a/templates/pages/page.html +++ b/templates/pages/page.html @@ -1,29 +1,60 @@ {{ template "inc/header.html" .}}

-
-

- [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] -

+ +
+ Logo
+
+ + +
+
+
+ + + + + + + +
+
+ +
+
+ + + + + + +
+
+ +
+ + +
{{ template "inc/theme-toggle.html" .}}