Input box, sidebar, ws connection
This commit is contained in:
@@ -21,7 +21,6 @@ var JWT_SECRET string
|
||||
var LOG_TO_FILE bool
|
||||
|
||||
func SetConfiguration() {
|
||||
fmt.Println("setting configuration for server")
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
fmt.Println("no .env file found. Needs to be added to server directory.")
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/alexedwards/argon2id"
|
||||
// "github.com/alexedwards/argon2id"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
@@ -54,14 +54,5 @@ func InitDB() error {
|
||||
|
||||
DB = result
|
||||
|
||||
// Use default recommended parameters
|
||||
hash, err := argon2id.CreateHash("hunter2", argon2id.DefaultParams)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Println("Argon2 Hash:")
|
||||
fmt.Println(hash)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -17,6 +17,7 @@ require (
|
||||
|
||||
require (
|
||||
github.com/go-chi/chi/v5 v5.2.1 // indirect
|
||||
github.com/gorilla/websocket v1.5.3 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/mailgun/errors v0.4.0 // indirect
|
||||
github.com/mattn/go-colorable v0.1.14 // indirect
|
||||
|
||||
@@ -10,6 +10,8 @@ github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5x
|
||||
github.com/golang-jwt/jwt/v5 v5.3.0 h1:pv4AsKCKKZuqlgs5sUmn4x8UlGa0kEVt/puTpKx9vvo=
|
||||
github.com/golang-jwt/jwt/v5 v5.3.0/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
|
||||
@@ -11,10 +11,17 @@ import (
|
||||
// "runtime/debug"
|
||||
"strings"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func isWebSocketRequest(r *http.Request) bool {
|
||||
connHeader := strings.ToLower(r.Header.Get("Connection"))
|
||||
upgradeHeader := strings.ToLower(r.Header.Get("Upgrade"))
|
||||
return strings.Contains(connHeader, "upgrade") && upgradeHeader == "websocket"
|
||||
}
|
||||
|
||||
func main() {
|
||||
config.SetConfiguration()
|
||||
logger.ConfigureLogger()
|
||||
@@ -39,13 +46,17 @@ func main() {
|
||||
|
||||
if(loggedIn(w, r)) {
|
||||
log.Info().Msg("logged")
|
||||
if isWebSocketRequest(r) {
|
||||
handleWebSocket(w, r)
|
||||
return
|
||||
}
|
||||
handleSite(w, r)
|
||||
} else {
|
||||
handlePublic(w, r)
|
||||
}
|
||||
})
|
||||
|
||||
log.Info().Msgf("Server starting on http://localhost: %s", config.PORT)
|
||||
log.Info().Msgf("Server starting on http://localhost:%s", config.PORT)
|
||||
err = http.ListenAndServe(":"+config.PORT, nil)
|
||||
if err != nil {
|
||||
log.Fatal().Msgf("failed to start server: %v", err)
|
||||
@@ -144,4 +155,37 @@ func loggedIn(w http.ResponseWriter, r *http.Request) bool {
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
var upgrader = websocket.Upgrader{
|
||||
CheckOrigin: func(r *http.Request) bool {
|
||||
return true // In production, validate the origin!
|
||||
},
|
||||
}
|
||||
|
||||
func handleWebSocket(w http.ResponseWriter, r *http.Request) {
|
||||
conn, err := upgrader.Upgrade(w, r, nil)
|
||||
if err != nil {
|
||||
fmt.Println("WebSocket upgrade failed:", err)
|
||||
http.Error(w, "WebSocket upgrade failed", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
fmt.Println("WebSocket connection established")
|
||||
|
||||
for {
|
||||
msgType, msg, err := conn.ReadMessage()
|
||||
if err != nil {
|
||||
fmt.Println("Read error:", err)
|
||||
break
|
||||
}
|
||||
fmt.Printf("Received: %s\n", msg)
|
||||
|
||||
if err := conn.WriteMessage(msgType, msg); err != nil {
|
||||
fmt.Println("Write error:", err)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user