feat: easier setup and more transparent customization

This commit is contained in:
jabuxas 2024-09-03 21:51:36 -03:00
parent 2431f5e655
commit 04fc379479
6 changed files with 46 additions and 10 deletions

2
.gitignore vendored
View File

@ -1,3 +1,3 @@
files/ files/
.key
abyss abyss
.env

View File

@ -5,19 +5,24 @@ import (
"net/http" "net/http"
"os" "os"
"time" "time"
"github.com/joho/godotenv"
) )
const ( const (
filesDir = "./files" filesDir = "./files"
port = ":8080" port = ":8999"
) )
func main() { func main() {
app := new(Application) app := new(Application)
godotenv.Load()
app.auth.username = os.Getenv("AUTH_USERNAME") app.auth.username = os.Getenv("AUTH_USERNAME")
app.auth.password = os.Getenv("AUTH_PASSWORD") app.auth.password = os.Getenv("AUTH_PASSWORD")
app.url = os.Getenv("URL") app.url = os.Getenv("ABYSS_URL")
app.key = os.Getenv("UPLOAD_KEY")
if app.auth.username == "" { if app.auth.username == "" {
log.Fatal("basic auth username must be provided") log.Fatal("basic auth username must be provided")

30
generate_config.sh Executable file
View File

@ -0,0 +1,30 @@
#!/bin/sh
echo "Press enter to use the default value '[value]' or use a custom value"
read -p "Server domain name - this is the end url of where abyss will be hosted []: " -e ABYSS_URL
read -p "Upload key - this is the key you will need to have to be able to make uploads to the server []: " -e UPLOAD_KEY
read -p "Auth username - this is the username to access /tree (show all uploaded files) [admin]: " -e AUTH_USERNAME
if [ -z $AUTH_USERNAME ]; then
AUTH_USERNAME="admin"
fi
read -p "Auth password - this is the password to access /tree (show all uploaded files) [admin]: " -e AUTH_PASSWORD
if [ -z $AUTH_PASSWORD ]; then
AUTH_PASSWORD="admin"
fi
cat << EOF > .env
# This is the full name of the final domain for the server. Example: paste.abyss.dev
ABYSS_URL=$ABYSS_URL
# This is the username of the user for accessing /tree
AUTH_USERNAME=$AUTH_USERNAME
# This is the password of the user for accessing /tree
AUTH_PASSWORD=$AUTH_PASSWORD
# This is the key needed to make uploads. Include it as X-Auth in curl.
UPLOAD_KEY=$UPLOAD_KEY
EOF

2
go.mod
View File

@ -1,3 +1,5 @@
module github.com/jabuxas/abyss module github.com/jabuxas/abyss
go 1.22.6 go 1.22.6
require github.com/joho/godotenv v1.5.1 // indirect

2
go.sum Normal file
View File

@ -0,0 +1,2 @@
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=

View File

@ -17,6 +17,7 @@ type Application struct {
password string password string
} }
url string url string
key string
} }
func (app *Application) fileHandler(w http.ResponseWriter, r *http.Request) { func (app *Application) fileHandler(w http.ResponseWriter, r *http.Request) {
@ -50,7 +51,7 @@ func (app *Application) uploadHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
if !checkAuth(w, r) { if !app.checkAuth(r) {
http.Error(w, "You're not authorized.", http.StatusBadRequest) http.Error(w, "You're not authorized.", http.StatusBadRequest)
return return
} }
@ -91,12 +92,8 @@ func (app *Application) uploadHandler(w http.ResponseWriter, r *http.Request) {
} }
} }
func checkAuth(w http.ResponseWriter, r *http.Request) bool { func (app *Application) checkAuth(r *http.Request) bool {
authKey, err := os.ReadFile(".key") return r.Header.Get("X-Auth") == string(app.key)
if err != nil {
http.Error(w, "Couldn't find your .key", http.StatusNotFound)
}
return r.Header.Get("X-Auth")+"\n" == string(authKey)
} }
func (app *Application) basicAuth(next http.HandlerFunc) http.HandlerFunc { func (app *Application) basicAuth(next http.HandlerFunc) http.HandlerFunc {