feat: easier setup and more transparent customization
This commit is contained in:
parent
2431f5e655
commit
04fc379479
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,3 +1,3 @@
|
||||
files/
|
||||
.key
|
||||
abyss
|
||||
.env
|
||||
|
9
abyss.go
9
abyss.go
@ -5,19 +5,24 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
const (
|
||||
filesDir = "./files"
|
||||
port = ":8080"
|
||||
port = ":8999"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := new(Application)
|
||||
|
||||
godotenv.Load()
|
||||
|
||||
app.auth.username = os.Getenv("AUTH_USERNAME")
|
||||
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 == "" {
|
||||
log.Fatal("basic auth username must be provided")
|
||||
|
30
generate_config.sh
Executable file
30
generate_config.sh
Executable 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
2
go.mod
@ -1,3 +1,5 @@
|
||||
module github.com/jabuxas/abyss
|
||||
|
||||
go 1.22.6
|
||||
|
||||
require github.com/joho/godotenv v1.5.1 // indirect
|
||||
|
2
go.sum
Normal file
2
go.sum
Normal 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=
|
11
handlers.go
11
handlers.go
@ -17,6 +17,7 @@ type Application struct {
|
||||
password string
|
||||
}
|
||||
url string
|
||||
key string
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
if !checkAuth(w, r) {
|
||||
if !app.checkAuth(r) {
|
||||
http.Error(w, "You're not authorized.", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
@ -91,12 +92,8 @@ func (app *Application) uploadHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
func checkAuth(w http.ResponseWriter, r *http.Request) bool {
|
||||
authKey, err := os.ReadFile(".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) checkAuth(r *http.Request) bool {
|
||||
return r.Header.Get("X-Auth") == string(app.key)
|
||||
}
|
||||
|
||||
func (app *Application) basicAuth(next http.HandlerFunc) http.HandlerFunc {
|
||||
|
Loading…
Reference in New Issue
Block a user