Compare commits
No commits in common. "693ac02c9d810a2ee5f2e2e538be71f12cb59c75" and "31767fd38a46cc7d3445b0a32ca2a59b1c42dd17" have entirely different histories.
693ac02c9d
...
31767fd38a
5 changed files with 39 additions and 52 deletions
7
.env
7
.env
|
@ -1,7 +0,0 @@
|
||||||
PLAKKEN_INTERFACE=0.0.0.0
|
|
||||||
PLAKKEN_PORT=3000
|
|
||||||
PLAKKEN_REDIS_ADDR=localhost:6379
|
|
||||||
PLAKKEN_REDIS_USER=
|
|
||||||
PLAKKEN_REDIS_PASSWORD=
|
|
||||||
PLAKKEN_REDIS_DB=0
|
|
||||||
PLAKKEN_REDIS_URL_LEN=5
|
|
42
config.go
42
config.go
|
@ -1,8 +1,6 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/joho/godotenv"
|
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
@ -17,40 +15,38 @@ type config struct {
|
||||||
urlLength int
|
urlLength int
|
||||||
}
|
}
|
||||||
|
|
||||||
func getConfig() config {
|
func setConfig() config {
|
||||||
err := godotenv.Load()
|
host := os.Getenv("PLAKKEN_HOST")
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Error loading .env file: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
port := os.Getenv("PLAKKEN_PORT")
|
port := os.Getenv("PLAKKEN_PORT")
|
||||||
|
if port == "" {
|
||||||
|
port = "3000"
|
||||||
|
}
|
||||||
redisAddr := os.Getenv("PLAKKEN_REDIS_ADDR")
|
redisAddr := os.Getenv("PLAKKEN_REDIS_ADDR")
|
||||||
db := os.Getenv("PLAKKEN_REDIS_DB")
|
if redisAddr == "" {
|
||||||
uLen := os.Getenv("PLAKKEN_REDIS_URL_LEN")
|
redisAddr = "localhost:6379"
|
||||||
|
|
||||||
if port == "" || redisAddr == "" {
|
|
||||||
log.Fatal("Missing or invalid PLAKKEN_PORT or PLAKKEN_REDIS_ADDR")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
redisDB, err := strconv.Atoi(db)
|
redisUser := os.Getenv("PLAKKEN_REDIS_USER")
|
||||||
|
redisPassword := os.Getenv("PLAKKEN_REDIS_PASSWORD")
|
||||||
|
redisDB, err := strconv.Atoi(os.Getenv("PLAKKEN_REDIS_DB"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Invalid PLAKKEN_REDIS_DB")
|
redisDB = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
urlLen, err := strconv.Atoi(uLen)
|
urlLength, err := strconv.Atoi("PLAKKEN_URL_LENGTH")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Invalid PLAKKEN_REDIS_URL_LEN")
|
urlLength = 3
|
||||||
}
|
}
|
||||||
|
s := config{
|
||||||
conf := config{
|
host: host,
|
||||||
host: os.Getenv("PLAKKEN_INTERFACE"),
|
|
||||||
port: port,
|
port: port,
|
||||||
redisAddr: redisAddr,
|
redisAddr: redisAddr,
|
||||||
redisUser: os.Getenv("PLAKKEN_REDIS_USER"),
|
redisUser: redisUser,
|
||||||
redisPassword: os.Getenv("PLAKKEN_REDIS_PASSWORD"),
|
redisPassword: redisPassword,
|
||||||
redisDB: redisDB,
|
redisDB: redisDB,
|
||||||
urlLength: urlLen,
|
urlLength: urlLength,
|
||||||
}
|
}
|
||||||
|
|
||||||
return conf
|
return s
|
||||||
}
|
}
|
||||||
|
|
6
go.mod
6
go.mod
|
@ -2,12 +2,8 @@ module plakken
|
||||||
|
|
||||||
go 1.21
|
go 1.21
|
||||||
|
|
||||||
require (
|
|
||||||
github.com/joho/godotenv v1.5.1
|
|
||||||
github.com/redis/go-redis/v9 v9.2.1
|
|
||||||
)
|
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/cespare/xxhash/v2 v2.2.0 // indirect
|
github.com/cespare/xxhash/v2 v2.2.0 // indirect
|
||||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||||
|
github.com/redis/go-redis/v9 v9.2.1 // indirect
|
||||||
)
|
)
|
||||||
|
|
22
main.go
22
main.go
|
@ -3,7 +3,6 @@ package main
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
@ -16,14 +15,18 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
|
||||||
case "GET":
|
case "GET":
|
||||||
if path == "/" {
|
if path == "/" {
|
||||||
http.ServeFile(w, r, "./static/index.html")
|
http.ServeFile(w, r, "./static/index.html")
|
||||||
|
|
||||||
} else if strings.HasPrefix(path, "/static/") {
|
} else if strings.HasPrefix(path, "/static/") {
|
||||||
fs := http.FileServer(http.Dir("./static"))
|
fs := http.FileServer(http.Dir("./static"))
|
||||||
http.Handle("/static/", http.StripPrefix("/static/", fs))
|
http.Handle("/static/", http.StripPrefix("/static/", fs))
|
||||||
} else {
|
} else {
|
||||||
if urlExist(path) {
|
if urlExist(path) {
|
||||||
_, err := io.WriteString(w, "This plak exists")
|
pasteContent := getContent(path)
|
||||||
if err != nil {
|
fmt.Println(pasteContent)
|
||||||
return
|
if strings.HasSuffix("/raw", path) {
|
||||||
|
io.WriteString(w, pasteContent)
|
||||||
|
} else {
|
||||||
|
io.WriteString(w, pasteContent)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
w.WriteHeader(http.StatusNotFound)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
@ -40,21 +43,16 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
|
||||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
currentConfig = getConfig()
|
currentConfig = setConfig()
|
||||||
listen := currentConfig.host + ":" + currentConfig.port
|
listen := currentConfig.host + ":" + currentConfig.port
|
||||||
http.HandleFunc("/", handleRequest)
|
http.HandleFunc("/", handleRequest)
|
||||||
|
|
||||||
if currentConfig.host == "" {
|
|
||||||
fmt.Println("Listening on port " + listen)
|
|
||||||
} else {
|
|
||||||
fmt.Println("Listening on " + listen)
|
|
||||||
}
|
|
||||||
|
|
||||||
err := http.ListenAndServe(listen, nil)
|
err := http.ListenAndServe(listen, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
14
utils.go
14
utils.go
|
@ -3,13 +3,13 @@ package main
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"log"
|
|
||||||
mathrand "math/rand"
|
mathrand "math/rand"
|
||||||
)
|
)
|
||||||
|
|
||||||
func generateUrl() string {
|
func generateUrl() string {
|
||||||
|
length := currentConfig.urlLength
|
||||||
listChars := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
|
listChars := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
|
||||||
b := make([]rune, currentConfig.urlLength)
|
b := make([]rune, length)
|
||||||
for i := range b {
|
for i := range b {
|
||||||
b[i] = listChars[mathrand.Intn(len(listChars))]
|
b[i] = listChars[mathrand.Intn(len(listChars))]
|
||||||
}
|
}
|
||||||
|
@ -21,13 +21,17 @@ func generateSecret() string {
|
||||||
key := make([]byte, 32)
|
key := make([]byte, 32)
|
||||||
_, err := rand.Read(key)
|
_, err := rand.Read(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Failed to generate secret")
|
// handle error here
|
||||||
}
|
}
|
||||||
|
|
||||||
return hex.EncodeToString(key)
|
secret := hex.EncodeToString(key)
|
||||||
|
return secret
|
||||||
}
|
}
|
||||||
|
|
||||||
func urlExist(url string) bool {
|
func urlExist(url string) bool {
|
||||||
exist := connectDB().Exists(ctx, url).Val()
|
exist := connectDB().Exists(ctx, url).Val()
|
||||||
return exist == 1
|
if exist == 1 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue