From 15dcc2928ab67580fdaab3731b0568c024f85d9c Mon Sep 17 00:00:00 2001 From: Caleb Gardner Date: Sat, 13 Jul 2024 03:31:37 -0500 Subject: [PATCH] Starting work on the actual website --- files.go | 53 ++++++++++++++++++++++++++++++++++++++ go.mod | 4 +-- internal/backend/README.md | 2 +- internal/backend/crash.go | 12 ++++----- main.go | 9 ++++--- web.go | 30 +++++++++++++++++++++ 6 files changed, 97 insertions(+), 13 deletions(-) create mode 100644 files.go create mode 100644 web.go diff --git a/files.go b/files.go new file mode 100644 index 0000000..271e0bb --- /dev/null +++ b/files.go @@ -0,0 +1,53 @@ +package main + +import ( + "fmt" + "log" + "net/http" + "os" + "path/filepath" +) + +const fileElement = "

%v

" + +func filesRequest(w http.ResponseWriter, r *http.Request) { + partPath := filepath.Clean(r.URL.Path) + path := filepath.Join(*webRoot, partPath) + var pageContent string + fil, err := os.Open(path) + if err != nil { + if os.IsNotExist(err) { + pageContent = "

404 Not Found

" + w.WriteHeader(http.StatusNotFound) + } else { + pageContent = "

Server error!

" + w.WriteHeader(http.StatusInternalServerError) + log.Println("error serving files:", err) + } + } else { + stat, _ := fil.Stat() + if stat.IsDir() { + var dirs []os.DirEntry + dirs, err = fil.ReadDir(-1) + if err != nil { + pageContent = "

Server error!

" + w.WriteHeader(http.StatusInternalServerError) + log.Println("error serving files:", err) + } + for _, f := range dirs { + if f.IsDir() { + continue + } + pageContent += fmt.Sprintf(fileElement, partPath, f.Name()) + } + } else { + http.ServeFile(w, r, path) + return + } + } + if r.URL.Query().Get("contentOnly") == "true" { + w.Write([]byte(pageContent)) + } else { + sendIndexWithContent(w, pageContent) + } +} diff --git a/go.mod b/go.mod index 9a709ca..8eea991 100644 --- a/go.mod +++ b/go.mod @@ -1,16 +1,16 @@ module github.com/CalebQ42/darkstorm-server -go 1.22.3 +go 1.22.5 require ( github.com/golang-jwt/jwt/v5 v5.2.1 github.com/google/uuid v1.6.0 go.mongodb.org/mongo-driver v1.15.1 golang.org/x/crypto v0.24.0 + github.com/CalebQ42/bbConvert v1.0.0 ) require ( - github.com/CalebQ42/bbConvert v1.0.0 // indirect github.com/golang/snappy v0.0.1 // indirect github.com/google/go-cmp v0.6.0 // indirect github.com/klauspost/compress v1.13.6 // indirect diff --git a/internal/backend/README.md b/internal/backend/README.md index 051c8ef..f9a119a 100644 --- a/internal/backend/README.md +++ b/internal/backend/README.md @@ -61,7 +61,7 @@ Users are stored per backend and not per app. { count: 1, // We do not store duplicates. If a duplicate does occur platform: "android", - appVersion: "v1.0.0", // Application version + version: "v1.0.0", // Application version error: "error", stack: "stacktrace" } diff --git a/internal/backend/crash.go b/internal/backend/crash.go index 672abfc..e4df220 100644 --- a/internal/backend/crash.go +++ b/internal/backend/crash.go @@ -14,11 +14,11 @@ type ArchivedCrash struct { } type IndividualCrash struct { - Platform string `json:"platform" bson:"platform"` - AppVersion string `json:"appVersion" bson:"appVersion"` - Error string `json:"error" bson:"error"` - Stack string `json:"stack" bson:"stack"` - Count int `json:"count" bson:"count"` + Platform string `json:"platform" bson:"platform"` + Version string `json:"version" bson:"version"` + Error string `json:"error" bson:"error"` + Stack string `json:"stack" bson:"stack"` + Count int `json:"count" bson:"count"` } type CrashReport struct { @@ -44,7 +44,7 @@ func (b *Backend) reportCrash(w http.ResponseWriter, r *http.Request) { defer r.Body.Close() var crash IndividualCrash err = json.NewDecoder(r.Body).Decode(&crash) - if err != nil || crash.Platform == "" || crash.AppVersion == "" || crash.Error == "" || crash.Stack == "" { + if err != nil || crash.Platform == "" || crash.Version == "" || crash.Error == "" || crash.Stack == "" { ReturnError(w, http.StatusBadRequest, "invalidBody", "Bad request") return } diff --git a/main.go b/main.go index 801bbb6..c9fce61 100644 --- a/main.go +++ b/main.go @@ -19,11 +19,12 @@ var ( mongoClient *mongo.Client back *backend.Backend blogApp *blog.BlogApp + webRoot *string ) func main() { mongoURL := flag.String("mongo", "", "Enables MongoDB usage for Darkstorm backend.") - webRoot := flag.String("web-root", "", "Sets root directory of web server.") + webRoot = flag.String("web-root", "", "Sets root directory of web server.") flag.Parse() if flag.NArg() != 1 { log.Fatal("You must specify key directory. ex: darkstorm-server /etc/web-keys") @@ -37,7 +38,7 @@ func main() { mux := http.NewServeMux() setupMongo(*mongoURL) setupBackend(mux) - setupWebsite(mux, *webRoot) + setupWebsite(mux) serv := &http.Server{ Addr: ":443", Handler: mux, @@ -80,6 +81,6 @@ func setupBackend(mux *http.ServeMux) { mux.Handle("/", back) } -func setupWebsite(mux *http.ServeMux, root string) { - //TODO +func setupWebsite(mux *http.ServeMux) { + mux.HandleFunc("GET /files", filesRequest) } diff --git a/web.go b/web.go new file mode 100644 index 0000000..755b65c --- /dev/null +++ b/web.go @@ -0,0 +1,30 @@ +package main + +import ( + "bytes" + "io" + "log" + "net/http" + "os" + "path/filepath" +) + +const replacementComment = "" + +func sendIndexWithContent(w http.ResponseWriter, content string) { + indexFile, err := os.Open(filepath.Join(*webRoot, "index.html")) + if err != nil { + log.Println("error when opening main index.html:", err) + w.WriteHeader(http.StatusNotFound) + return + } + dat, err := io.ReadAll(indexFile) + if err != nil { + log.Println("error reading main index.html:", err) + w.WriteHeader(http.StatusInternalServerError) + return + } + dat = bytes.ReplaceAll(dat, []byte(replacementComment), []byte(content)) + w.Header().Set("Content-Type", "text/html; charset=utf-8") + w.Write(dat) +}