Starting work on the actual website

This commit is contained in:
Caleb Gardner
2024-07-13 03:31:37 -05:00
parent e43409923c
commit 15dcc2928a
6 changed files with 97 additions and 13 deletions
+53
View File
@@ -0,0 +1,53 @@
package main
import (
"fmt"
"log"
"net/http"
"os"
"path/filepath"
)
const fileElement = "<p><a href='https://darkstorm.tech/%v/'>%v</a></p>"
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 = "<p>404 Not Found</p>"
w.WriteHeader(http.StatusNotFound)
} else {
pageContent = "<p>Server error!</p>"
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 = "<p>Server error!</p>"
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)
}
}
+2 -2
View File
@@ -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
+1 -1
View File
@@ -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"
}
+2 -2
View File
@@ -15,7 +15,7 @@ type ArchivedCrash struct {
type IndividualCrash struct {
Platform string `json:"platform" bson:"platform"`
AppVersion string `json:"appVersion" bson:"appVersion"`
Version string `json:"version" bson:"version"`
Error string `json:"error" bson:"error"`
Stack string `json:"stack" bson:"stack"`
Count int `json:"count" bson:"count"`
@@ -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
}
+5 -4
View File
@@ -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)
}
+30
View File
@@ -0,0 +1,30 @@
package main
import (
"bytes"
"io"
"log"
"net/http"
"os"
"path/filepath"
)
const replacementComment = "<!--Content-->"
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)
}