Skip to content

Commit ac51a59

Browse files
committed
use constant instead of env variable
1 parent 40c78ce commit ac51a59

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

configs/.env

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
APP_NAME=static-server
22

3-
HTTP_PORT=8000
4-
5-
STATIC_DIR_PATH=./website
3+
HTTP_PORT=8000

main.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ import (
1212
"gofr.dev/pkg/gofr/logging"
1313
)
1414

15+
const staticFilePath = `./website`
16+
1517
func main() {
1618
app := gofr.New()
1719

18-
files := createListOfFiles(app.Config.Get("STATIC_DIR_PATH"), app.Logger())
20+
files := createListOfFiles(app.Logger())
1921

2022
app.UseMiddleware(func(handler http.Handler) http.Handler {
2123
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -36,24 +38,24 @@ func main() {
3638
})
3739
})
3840

39-
app.AddStaticFiles("/", "./website")
41+
app.AddStaticFiles("/", staticFilePath)
4042

4143
app.Run()
4244
}
4345

44-
func createListOfFiles(staticDirPath string, logger logging.Logger) map[string]bool {
46+
func createListOfFiles(logger logging.Logger) map[string]bool {
4547
files := make(map[string]bool)
4648

4749
files["/"] = true
4850

49-
_, err := os.Stat(staticDirPath)
51+
_, err := os.Stat(staticFilePath)
5052
if err != nil {
51-
logger.Errorf("Error while reading static files directory %v", err)
53+
logger.Fatalf("Error while reading static files directory %v", err)
5254

5355
return files
5456
}
5557

56-
err = filepath.Walk(staticDirPath, func(path string, info fs.FileInfo, _ error) error {
58+
err = filepath.Walk(staticFilePath, func(path string, info fs.FileInfo, _ error) error {
5759
after, _ := strings.CutPrefix(path, "website")
5860

5961
if !info.IsDir() {
@@ -64,6 +66,14 @@ func createListOfFiles(staticDirPath string, logger logging.Logger) map[string]b
6466
})
6567
if err != nil {
6668
logger.Errorf("Error while walking through static files directory %v", err)
69+
70+
return files
71+
}
72+
73+
logger.Infof("File reading successful")
74+
75+
for k, _ := range files {
76+
logger.Infof("File: %v", k)
6777
}
6878

6979
return files

0 commit comments

Comments
 (0)