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
|
||||
|
||||
import (
|
||||
"github.com/joho/godotenv"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
@ -17,40 +15,38 @@ type config struct {
|
|||
urlLength int
|
||||
}
|
||||
|
||||
func getConfig() config {
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
log.Fatalf("Error loading .env file: %v", err)
|
||||
}
|
||||
func setConfig() config {
|
||||
host := os.Getenv("PLAKKEN_HOST")
|
||||
|
||||
port := os.Getenv("PLAKKEN_PORT")
|
||||
if port == "" {
|
||||
port = "3000"
|
||||
}
|
||||
redisAddr := os.Getenv("PLAKKEN_REDIS_ADDR")
|
||||
db := os.Getenv("PLAKKEN_REDIS_DB")
|
||||
uLen := os.Getenv("PLAKKEN_REDIS_URL_LEN")
|
||||
|
||||
if port == "" || redisAddr == "" {
|
||||
log.Fatal("Missing or invalid PLAKKEN_PORT or PLAKKEN_REDIS_ADDR")
|
||||
if redisAddr == "" {
|
||||
redisAddr = "localhost:6379"
|
||||
}
|
||||
|
||||
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 {
|
||||
log.Fatal("Invalid PLAKKEN_REDIS_DB")
|
||||
redisDB = 0
|
||||
}
|
||||
|
||||
urlLen, err := strconv.Atoi(uLen)
|
||||
urlLength, err := strconv.Atoi("PLAKKEN_URL_LENGTH")
|
||||
if err != nil {
|
||||
log.Fatal("Invalid PLAKKEN_REDIS_URL_LEN")
|
||||
urlLength = 3
|
||||
}
|
||||
|
||||
conf := config{
|
||||
host: os.Getenv("PLAKKEN_INTERFACE"),
|
||||
s := config{
|
||||
host: host,
|
||||
port: port,
|
||||
redisAddr: redisAddr,
|
||||
redisUser: os.Getenv("PLAKKEN_REDIS_USER"),
|
||||
redisPassword: os.Getenv("PLAKKEN_REDIS_PASSWORD"),
|
||||
redisUser: redisUser,
|
||||
redisPassword: redisPassword,
|
||||
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
|
||||
|
||||
require (
|
||||
github.com/joho/godotenv v1.5.1
|
||||
github.com/redis/go-redis/v9 v9.2.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/cespare/xxhash/v2 v2.2.0 // 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 (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
@ -16,14 +15,18 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
|
|||
case "GET":
|
||||
if path == "/" {
|
||||
http.ServeFile(w, r, "./static/index.html")
|
||||
|
||||
} else if strings.HasPrefix(path, "/static/") {
|
||||
fs := http.FileServer(http.Dir("./static"))
|
||||
http.Handle("/static/", http.StripPrefix("/static/", fs))
|
||||
} else {
|
||||
if urlExist(path) {
|
||||
_, err := io.WriteString(w, "This plak exists")
|
||||
if err != nil {
|
||||
return
|
||||
pasteContent := getContent(path)
|
||||
fmt.Println(pasteContent)
|
||||
if strings.HasSuffix("/raw", path) {
|
||||
io.WriteString(w, pasteContent)
|
||||
} else {
|
||||
io.WriteString(w, pasteContent)
|
||||
}
|
||||
} else {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
|
@ -40,21 +43,16 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
|
|||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func main() {
|
||||
currentConfig = getConfig()
|
||||
currentConfig = setConfig()
|
||||
listen := currentConfig.host + ":" + currentConfig.port
|
||||
http.HandleFunc("/", handleRequest)
|
||||
|
||||
if currentConfig.host == "" {
|
||||
fmt.Println("Listening on port " + listen)
|
||||
} else {
|
||||
fmt.Println("Listening on " + listen)
|
||||
}
|
||||
|
||||
err := http.ListenAndServe(listen, nil)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
|
14
utils.go
14
utils.go
|
@ -3,13 +3,13 @@ package main
|
|||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"log"
|
||||
mathrand "math/rand"
|
||||
)
|
||||
|
||||
func generateUrl() string {
|
||||
length := currentConfig.urlLength
|
||||
listChars := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
|
||||
b := make([]rune, currentConfig.urlLength)
|
||||
b := make([]rune, length)
|
||||
for i := range b {
|
||||
b[i] = listChars[mathrand.Intn(len(listChars))]
|
||||
}
|
||||
|
@ -21,13 +21,17 @@ func generateSecret() string {
|
|||
key := make([]byte, 32)
|
||||
_, err := rand.Read(key)
|
||||
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 {
|
||||
exist := connectDB().Exists(ctx, url).Val()
|
||||
return exist == 1
|
||||
if exist == 1 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue