Compare commits

...

4 commits

5 changed files with 52 additions and 39 deletions

7
.env Normal file
View file

@ -0,0 +1,7 @@
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

View file

@ -1,6 +1,8 @@
package main package main
import ( import (
"github.com/joho/godotenv"
"log"
"os" "os"
"strconv" "strconv"
) )
@ -15,38 +17,40 @@ type config struct {
urlLength int urlLength int
} }
func setConfig() config { func getConfig() config {
host := os.Getenv("PLAKKEN_HOST") err := godotenv.Load()
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")
if redisAddr == "" { db := os.Getenv("PLAKKEN_REDIS_DB")
redisAddr = "localhost:6379" uLen := os.Getenv("PLAKKEN_REDIS_URL_LEN")
if port == "" || redisAddr == "" {
log.Fatal("Missing or invalid PLAKKEN_PORT or PLAKKEN_REDIS_ADDR")
} }
redisUser := os.Getenv("PLAKKEN_REDIS_USER") redisDB, err := strconv.Atoi(db)
redisPassword := os.Getenv("PLAKKEN_REDIS_PASSWORD")
redisDB, err := strconv.Atoi(os.Getenv("PLAKKEN_REDIS_DB"))
if err != nil { if err != nil {
redisDB = 0 log.Fatal("Invalid PLAKKEN_REDIS_DB")
} }
urlLength, err := strconv.Atoi("PLAKKEN_URL_LENGTH") urlLen, err := strconv.Atoi(uLen)
if err != nil { if err != nil {
urlLength = 3 log.Fatal("Invalid PLAKKEN_REDIS_URL_LEN")
} }
s := config{
host: host, conf := config{
host: os.Getenv("PLAKKEN_INTERFACE"),
port: port, port: port,
redisAddr: redisAddr, redisAddr: redisAddr,
redisUser: redisUser, redisUser: os.Getenv("PLAKKEN_REDIS_USER"),
redisPassword: redisPassword, redisPassword: os.Getenv("PLAKKEN_REDIS_PASSWORD"),
redisDB: redisDB, redisDB: redisDB,
urlLength: urlLength, urlLength: urlLen,
} }
return s return conf
} }

6
go.mod
View file

@ -2,8 +2,12 @@ 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
View file

@ -3,6 +3,7 @@ package main
import ( import (
"fmt" "fmt"
"io" "io"
"log"
"net/http" "net/http"
"strings" "strings"
) )
@ -15,18 +16,14 @@ 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) {
pasteContent := getContent(path) _, err := io.WriteString(w, "This plak exists")
fmt.Println(pasteContent) if err != nil {
if strings.HasSuffix("/raw", path) { return
io.WriteString(w, pasteContent)
} else {
io.WriteString(w, pasteContent)
} }
} else { } else {
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
@ -43,16 +40,21 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusMethodNotAllowed) w.WriteHeader(http.StatusMethodNotAllowed)
} }
} }
} }
func main() { func main() {
currentConfig = setConfig() currentConfig = getConfig()
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 {
fmt.Println(err) log.Fatal(err)
} }
} }

View file

@ -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, length) b := make([]rune, currentConfig.urlLength)
for i := range b { for i := range b {
b[i] = listChars[mathrand.Intn(len(listChars))] b[i] = listChars[mathrand.Intn(len(listChars))]
} }
@ -21,17 +21,13 @@ 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 {
// handle error here log.Printf("Failed to generate secret")
} }
secret := hex.EncodeToString(key) return 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()
if exist == 1 { return exist == 1
return true
}
return false
} }