Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"net/http"
"sync"

"github.com/lucas-clemente/quic-go/http3"
Expand All @@ -14,6 +15,8 @@ import (
"server/src"
)

var loading = true

func main() {
config.LoadConfig()
log.Log("Loaded config:", fmt.Sprintf("%+v", config.GetConfig()))
Expand All @@ -23,20 +26,19 @@ func main() {
log.Log("Starting server")
src.DBInit()

srv.LoadSites()

serv := srv.CreateServe()
go func() {
loading = true
srv.LoadSites()
loading = false
}()

webServer3 := &http3.Server{
Addr: "localhost:" + fmt.Sprintf("%d", config.GetConfig().Port),
Handler: serv,
}
serv := srv.CreateServe(&loading)

wg := sync.WaitGroup{}

wg.Add(1)
go func() {
startWebServer(webServer3)
startWebServer("localhost:"+fmt.Sprintf("%d", config.GetConfig().Port), serv)
wg.Done()
}()

Expand All @@ -50,11 +52,10 @@ func main() {
wg.Wait()
}

func startWebServer(webServer *http3.Server) {
func startWebServer(addr string, handler http.Handler) {
// blocks if success
log.Log(fmt.Sprintf("ListenAndServe Webserver HTTP/3 with TLS started on https://%s", webServer.Addr))
// err := webServer.ListenAndServeTLS(config.CertsFile, config.KeyFile)
err := http3.ListenAndServe(webServer.Addr, config.CertsFile, config.KeyFile, webServer.Handler)
log.Log(fmt.Sprintf("ListenAndServe Webserver HTTP/3 with TLS started on https://%s", addr))
err := http3.ListenAndServe(addr, config.CertsFile, config.KeyFile, handler)

if err != nil {
log.Err(err, "Error starting webServer")
Expand Down
1 change: 1 addition & 0 deletions src/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
var cluster *gocql.ClusterConfig

// var GQLSession gocqlx.Session

var Session *gocql.Session

// DBInit Create and open DB Connection
Expand Down
48 changes: 25 additions & 23 deletions src/srv/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,36 @@ func GetErrorSite(error Errors, host string, path string, additional string) (*[
site = "Error not found"
}

site = fmt.Sprintf(`
site = fmt.Sprintf(errorSite, error, http.StatusText(int(error)), http.StatusText(int(error)), path, site, additional, int(error), host, runtime.Version(), getOS())

ret := []byte(site)

return &ret, int(error)
}

func getOS() string {
f, err := os.Open("/etc/os-release")
if err != nil {
return "Not UNIX"
}
defer f.Close()
s := bufio.NewScanner(f)
for s.Scan() {
if strings.HasPrefix(s.Text(), "NAME=") {
return strings.TrimSuffix(strings.TrimPrefix(s.Text(), "NAME=\""), "\"")
}
}
return ""
}

const errorSite = `
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width"/>
<title>%d | %s</title>
</head>
<body style="background:black;background:linear-gradient(140deg, rgb(7 10 15) 0%%, rgb(0,0,0) 50%%, rgb(7 9 10) 100%%);;color:white">
<body style="background:black;background:linear-gradient(140deg, rgb(7 10 15) 0%%, rgb(0,0,0) 50%%, rgb(7 9 10) 100%%);color:white">
<div style="margin:auto;width:50%%;padding:10px;position:absolute;bottom:50%%;right:50%%;transform:translate(50%%,50%%);overflow:hidden;display:flex;flex-direction:column">
<div style="display:flex;align-items:center;justify-content:space-between;gap:2em">
<h1 style="margin-block:0.2em;flex-shrink:0">%s</h1>
Expand All @@ -53,24 +75,4 @@ func GetErrorSite(error Errors, host string, path string, additional string) (*[
</div>
</body>
</html>
`, error, http.StatusText(int(error)), http.StatusText(int(error)), path, site, additional, int(error), host, runtime.Version(), getOS())

ret := []byte(site)

return &ret, int(error)
}

func getOS() string {
f, err := os.Open("/etc/os-release")
if err != nil {
return "Not UNIX"
}
defer f.Close()
s := bufio.NewScanner(f)
for s.Scan() {
if strings.HasPrefix(s.Text(), "NAME=") {
return strings.TrimSuffix(strings.TrimPrefix(s.Text(), "NAME=\""), "\"")
}
}
return ""
}
`
39 changes: 39 additions & 0 deletions src/srv/loading.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package srv

import (
"fmt"
"runtime"
)

func GetLoadingSite(host string, path string) *[]byte {
site := fmt.Sprintf(loadingSite, path, host, runtime.Version(), getOS())

ret := []byte(site)

return &ret
}

const loadingSite = `
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width"/>
<title>Server Loading</title>
</head>
<body style="background:black;background:linear-gradient(140deg, rgb(7 10 15) 0%%, rgb(0,0,0) 50%%, rgb(7 9 10) 100%%);color:white">
<div style="margin:auto;width:50%%;padding:10px;position:absolute;bottom:50%%;right:50%%;transform:translate(50%%,50%%);overflow:hidden;display:flex;flex-direction:column">
<div style="display:flex;align-items:center;justify-content:space-between;gap:2em">
<h1 style="margin-block:0.2em;flex-shrink:0">Server is still starting</h1>
<p>Error accessing %s</p>
</div>
<div style="display:flex;align-items:center;justify-content:space-between;gap:2em">
<p>-- Progress --</p>
<button style="padding:8px 16px;color:white;border:white 1px solid;background:transparent;cursor:pointer;border-radius:1em" onclick="location.reload()">Reload</button>
</div>
<img src="https://media.tenor.com/RVvnVPK-6dcAAAAC/reload-cat.gif" style="align-self:center;max-height:80vh">
<hr style="width:100%%">
<address>GoWebserver at %s running %s on %s</address>
</div>
</body>
</html>
`
23 changes: 17 additions & 6 deletions src/srv/serveSite.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,24 @@ func getSite(request *http.Request, availableEncodings *map[Encoding]bool) (*[]b
return data, encoding, 200, file.mimetype, nil
}

// CreateServe
//
// Registers a handle for '/' to serve the DefaultSite
func CreateServe() http.HandlerFunc {
func CreateServe(loading *bool) http.HandlerFunc {
fun := func(w http.ResponseWriter, r *http.Request) {
if *loading {
data := GetLoadingSite(r.Host, r.URL.Path)

w.WriteHeader(http.StatusTeapot)
w.Header().Set("Content-Type", "text/html")
_, er := w.Write(*data)
if er != nil {
log.Err(er, "Error writing response:")
}

return
}

if settings.GetSettings().ServerOff.Get() {
w.WriteHeader(http.StatusGone)
return
}
start := time.Now()
if r.URL.Path == "/" {
Expand All @@ -96,9 +107,9 @@ func CreateServe() http.HandlerFunc {
availableEncodings[Encoding(strings.TrimSpace(encoding))] = true
}

msg, encoding, code, mime, err := getSite(r, &availableEncodings)

// get actual site
searchTime := time.Now()
msg, encoding, code, mime, err := getSite(r, &availableEncodings)

if err != nil {
log.Err(err, fmt.Sprintf("Error getting site %s", r.URL.Path))
Expand Down