Starting work on the actual website
This commit is contained in:
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,16 +1,16 @@
|
|||||||
module github.com/CalebQ42/darkstorm-server
|
module github.com/CalebQ42/darkstorm-server
|
||||||
|
|
||||||
go 1.22.3
|
go 1.22.5
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/golang-jwt/jwt/v5 v5.2.1
|
github.com/golang-jwt/jwt/v5 v5.2.1
|
||||||
github.com/google/uuid v1.6.0
|
github.com/google/uuid v1.6.0
|
||||||
go.mongodb.org/mongo-driver v1.15.1
|
go.mongodb.org/mongo-driver v1.15.1
|
||||||
golang.org/x/crypto v0.24.0
|
golang.org/x/crypto v0.24.0
|
||||||
|
github.com/CalebQ42/bbConvert v1.0.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/CalebQ42/bbConvert v1.0.0 // indirect
|
|
||||||
github.com/golang/snappy v0.0.1 // indirect
|
github.com/golang/snappy v0.0.1 // indirect
|
||||||
github.com/google/go-cmp v0.6.0 // indirect
|
github.com/google/go-cmp v0.6.0 // indirect
|
||||||
github.com/klauspost/compress v1.13.6 // indirect
|
github.com/klauspost/compress v1.13.6 // indirect
|
||||||
|
|||||||
@@ -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
|
count: 1, // We do not store duplicates. If a duplicate does occur
|
||||||
platform: "android",
|
platform: "android",
|
||||||
appVersion: "v1.0.0", // Application version
|
version: "v1.0.0", // Application version
|
||||||
error: "error",
|
error: "error",
|
||||||
stack: "stacktrace"
|
stack: "stacktrace"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,11 +14,11 @@ type ArchivedCrash struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type IndividualCrash struct {
|
type IndividualCrash struct {
|
||||||
Platform string `json:"platform" bson:"platform"`
|
Platform string `json:"platform" bson:"platform"`
|
||||||
AppVersion string `json:"appVersion" bson:"appVersion"`
|
Version string `json:"version" bson:"version"`
|
||||||
Error string `json:"error" bson:"error"`
|
Error string `json:"error" bson:"error"`
|
||||||
Stack string `json:"stack" bson:"stack"`
|
Stack string `json:"stack" bson:"stack"`
|
||||||
Count int `json:"count" bson:"count"`
|
Count int `json:"count" bson:"count"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type CrashReport struct {
|
type CrashReport struct {
|
||||||
@@ -44,7 +44,7 @@ func (b *Backend) reportCrash(w http.ResponseWriter, r *http.Request) {
|
|||||||
defer r.Body.Close()
|
defer r.Body.Close()
|
||||||
var crash IndividualCrash
|
var crash IndividualCrash
|
||||||
err = json.NewDecoder(r.Body).Decode(&crash)
|
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")
|
ReturnError(w, http.StatusBadRequest, "invalidBody", "Bad request")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,11 +19,12 @@ var (
|
|||||||
mongoClient *mongo.Client
|
mongoClient *mongo.Client
|
||||||
back *backend.Backend
|
back *backend.Backend
|
||||||
blogApp *blog.BlogApp
|
blogApp *blog.BlogApp
|
||||||
|
webRoot *string
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
mongoURL := flag.String("mongo", "", "Enables MongoDB usage for Darkstorm backend.")
|
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()
|
flag.Parse()
|
||||||
if flag.NArg() != 1 {
|
if flag.NArg() != 1 {
|
||||||
log.Fatal("You must specify key directory. ex: darkstorm-server /etc/web-keys")
|
log.Fatal("You must specify key directory. ex: darkstorm-server /etc/web-keys")
|
||||||
@@ -37,7 +38,7 @@ func main() {
|
|||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
setupMongo(*mongoURL)
|
setupMongo(*mongoURL)
|
||||||
setupBackend(mux)
|
setupBackend(mux)
|
||||||
setupWebsite(mux, *webRoot)
|
setupWebsite(mux)
|
||||||
serv := &http.Server{
|
serv := &http.Server{
|
||||||
Addr: ":443",
|
Addr: ":443",
|
||||||
Handler: mux,
|
Handler: mux,
|
||||||
@@ -80,6 +81,6 @@ func setupBackend(mux *http.ServeMux) {
|
|||||||
mux.Handle("/", back)
|
mux.Handle("/", back)
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupWebsite(mux *http.ServeMux, root string) {
|
func setupWebsite(mux *http.ServeMux) {
|
||||||
//TODO
|
mux.HandleFunc("GET /files", filesRequest)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user