mirror of
https://github.com/NotAShelf/Echo.git
synced 2024-11-22 13:20:47 +00:00
initial commit
This commit is contained in:
parent
b8f6e62d1b
commit
ac95f3b5db
5 changed files with 75 additions and 0 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -19,3 +19,6 @@
|
|||
|
||||
# Go workspace file
|
||||
go.work
|
||||
|
||||
# Ignore dotenv files
|
||||
.env*
|
||||
|
|
22
README.md
Normal file
22
README.md
Normal file
|
@ -0,0 +1,22 @@
|
|||
# Echo
|
||||
|
||||
Janus allows you to setup a simple file server for local testing.
|
||||
|
||||
### Requirements
|
||||
|
||||
- Go (any recent version should work, native to Go 1.21)
|
||||
|
||||
## Usage
|
||||
|
||||
1. Clone the project
|
||||
2. Create a `.env` file
|
||||
|
||||
- Add `SERVER_PORT={PORT}`
|
||||
- Add `BASE_PATH={PATH}`
|
||||
|
||||
3. Run the program with `go run .`
|
||||
|
||||
## Notes
|
||||
|
||||
Echo is intended for localhost testing ONLY and designed as a convenient alternative to spinning up a nginx webserver for serving files quickly.
|
||||
Do not use in a production environment, as no security features are implemented.
|
5
go.mod
Normal file
5
go.mod
Normal file
|
@ -0,0 +1,5 @@
|
|||
module notashelf.dev/echo
|
||||
|
||||
go 1.21.4
|
||||
|
||||
require github.com/joho/godotenv v1.5.1
|
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=
|
43
main.go
Normal file
43
main.go
Normal file
|
@ -0,0 +1,43 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
fmt.Println("Error loading .env file")
|
||||
}
|
||||
|
||||
serverPort := os.Getenv("SERVER_PORT")
|
||||
if serverPort == "" {
|
||||
serverPort = "8080"
|
||||
}
|
||||
|
||||
basePath := os.Getenv("BASE_PATH")
|
||||
if basePath == "" {
|
||||
basePath = "."
|
||||
}
|
||||
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
if regexp.MustCompile(`\.\.\/?`).MatchString(r.URL.Path) {
|
||||
http.Error(w, "Forbidden", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
filePath := filepath.Join(basePath, r.URL.Path)
|
||||
http.ServeFile(w, r, filePath)
|
||||
})
|
||||
|
||||
port, _ := strconv.Atoi(serverPort)
|
||||
fmt.Printf("Echo started with base path %s on port %d\n", basePath, port)
|
||||
http.ListenAndServe(":"+serverPort, nil)
|
||||
}
|
Loading…
Reference in a new issue