begin
This commit is contained in:
3
server/.env
Normal file
3
server/.env
Normal file
@@ -0,0 +1,3 @@
|
||||
BASE_URL=localhost:3004
|
||||
JWT_SECRET=950b15c8c1c8a27dd716bba3ab51d96ce49afa85cae72884cf22e936e1bc0cb9
|
||||
ENV=development
|
||||
52
server/config/config.go
Normal file
52
server/config/config.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
var ENV string
|
||||
|
||||
// URLs
|
||||
var BASE_URL string
|
||||
const PORT = "3004"
|
||||
|
||||
// Auth
|
||||
var JWT_SECRET string
|
||||
|
||||
// Logging
|
||||
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.")
|
||||
}
|
||||
|
||||
ENV = os.Getenv("ENV")
|
||||
if ENV != "production" && ENV != "development" {
|
||||
fmt.Println("invalid value for ENV, must be 'development' or 'production'")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
BASE_URL = os.Getenv("BASE_URL")
|
||||
if BASE_URL == "" {
|
||||
fmt.Println("BASE_URL not provided, aborting")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
JWT_SECRET = os.Getenv("JWT_SECRET")
|
||||
if JWT_SECRET == "" {
|
||||
fmt.Println("JWT_SECRET not provided, aborting")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
LOG_TO_FILE, err = strconv.ParseBool(os.Getenv("LOG_TO_FILE"))
|
||||
if err != nil {
|
||||
LOG_TO_FILE = false
|
||||
}
|
||||
}
|
||||
7
server/env.example
Normal file
7
server/env.example
Normal file
@@ -0,0 +1,7 @@
|
||||
# ENV must be development or production
|
||||
# Required
|
||||
ENV=development
|
||||
|
||||
# Auth
|
||||
# Required
|
||||
JWT_SECRET=950b15c8c1c8a27dd716bba3ab51d96ce49afa85cae72884cf22e936e1bc0cb9
|
||||
28
server/go.mod
Normal file
28
server/go.mod
Normal file
@@ -0,0 +1,28 @@
|
||||
module hyperia
|
||||
|
||||
go 1.23.0
|
||||
|
||||
toolchain go1.24.2
|
||||
|
||||
require (
|
||||
github.com/alexedwards/argon2id v1.0.0
|
||||
github.com/golang-jwt/jwt/v5 v5.3.0
|
||||
github.com/joho/godotenv v1.5.1
|
||||
github.com/lib/pq v1.10.9
|
||||
github.com/mailgun/mailgun-go/v4 v4.23.0
|
||||
github.com/mssola/user_agent v0.6.0
|
||||
github.com/rs/zerolog v1.34.0
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/go-chi/chi/v5 v5.2.1 // 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
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
golang.org/x/crypto v0.14.0 // indirect
|
||||
golang.org/x/sys v0.35.0 // indirect
|
||||
)
|
||||
95
server/go.sum
Normal file
95
server/go.sum
Normal file
@@ -0,0 +1,95 @@
|
||||
github.com/alexedwards/argon2id v1.0.0 h1:wJzDx66hqWX7siL/SRUmgz3F8YMrd/nfX/xHHcQQP0w=
|
||||
github.com/alexedwards/argon2id v1.0.0/go.mod h1:tYKkqIjzXvZdzPvADMWOEZ+l6+BD6CtBXMj5fnJppiw=
|
||||
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/go-chi/chi/v5 v5.2.1 h1:KOIHODQj58PmL80G2Eak4WdvUzjSJSm0vG72crDCqb8=
|
||||
github.com/go-chi/chi/v5 v5.2.1/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops=
|
||||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||
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/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=
|
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
||||
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||
github.com/mailgun/errors v0.4.0 h1:6LFBvod6VIW83CMIOT9sYNp28TCX0NejFPP4dSX++i8=
|
||||
github.com/mailgun/errors v0.4.0/go.mod h1:xGBaaKdEdQT0/FhwvoXv4oBaqqmVZz9P1XEnvD/onc0=
|
||||
github.com/mailgun/mailgun-go/v4 v4.23.0 h1:jPEMJzzin2s7lvehcfv/0UkyBu18GvcURPr2+xtZRbk=
|
||||
github.com/mailgun/mailgun-go/v4 v4.23.0/go.mod h1:imTtizoFtpfZqPqGP8vltVBB6q9yWcv6llBhfFeElZU=
|
||||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
|
||||
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
|
||||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||
github.com/mssola/user_agent v0.6.0 h1:uwPR4rtWlCHRFyyP9u2KOV0u8iQXmS7Z7feTrstQwk4=
|
||||
github.com/mssola/user_agent v0.6.0/go.mod h1:TTPno8LPY3wAIEKRpAtkdMT0f8SE24pLRGPahjCH4uw=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
|
||||
github.com/rs/zerolog v1.34.0 h1:k43nTLIwcTVQAncfCw4KZ2VY6ukYoZaBPNOE8txlOeY=
|
||||
github.com/rs/zerolog v1.34.0/go.mod h1:bJsvje4Z08ROH4Nhs5iH600c3IkWhwp44iRc54W6wYQ=
|
||||
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
|
||||
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
|
||||
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI=
|
||||
golang.org/x/sys v0.35.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
|
||||
golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
||||
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
148
server/handlers/join.go
Normal file
148
server/handlers/join.go
Normal file
@@ -0,0 +1,148 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"github.com/rs/zerolog/log"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"regexp"
|
||||
"time"
|
||||
"context"
|
||||
|
||||
"hyperia/config"
|
||||
"github.com/mailgun/mailgun-go/v4"
|
||||
)
|
||||
|
||||
type joinRequest struct {
|
||||
Email string `json:"email"`
|
||||
}
|
||||
|
||||
var emailRegex = regexp.MustCompile(`^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$`)
|
||||
|
||||
func isValidEmail(email string) bool {
|
||||
return emailRegex.MatchString(email)
|
||||
}
|
||||
|
||||
func HandleJoin(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "Only POST allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
var creds joinRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&creds); err != nil {
|
||||
http.Error(w, "Invalid JSON", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if !isValidEmail(creds.Email) {
|
||||
http.Error(w, "Invalid email address", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// exists, err := EmailExists(creds.Email)
|
||||
// if err != nil {
|
||||
// log.Printf("Error checking email: %v", err)
|
||||
// http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
// return
|
||||
// }
|
||||
// if exists {
|
||||
// http.Error(w, "Email already exists.", http.StatusConflict)
|
||||
// return
|
||||
// }
|
||||
|
||||
// err = CreateApplicant(creds.Email)
|
||||
// if err != nil {
|
||||
// log.Printf("Error creating applicant: %v", err)
|
||||
// http.Error(w, "Failed to create applicant", http.StatusInternalServerError)
|
||||
// return
|
||||
// }
|
||||
|
||||
// token, err := generateVerificationToken(creds.Email)
|
||||
// if err != nil {
|
||||
// log.Printf("Error generating verification token: %v", err)
|
||||
// http.Error(w, "Error, please try again later.", http.StatusInternalServerError)
|
||||
// return
|
||||
// }
|
||||
|
||||
// err = sendWelcomeEmail(creds.Email, token)
|
||||
// if err != nil {
|
||||
// log.Printf("Error sending welcome email: %v", err)
|
||||
// http.Error(w, "Failed to send email", http.StatusInternalServerError)
|
||||
// return
|
||||
// }
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("OK"))
|
||||
}
|
||||
|
||||
func generateVerificationToken(email string) (string, error) {
|
||||
// Create 32 random bytes → 64-char hex string
|
||||
b := make([]byte, 32)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
return "", err
|
||||
}
|
||||
token := hex.EncodeToString(b)
|
||||
|
||||
// err := CreateApplicantVerification(email, token)
|
||||
// if err != nil {
|
||||
// return "", err
|
||||
// }
|
||||
|
||||
return token, nil
|
||||
}
|
||||
|
||||
func mailgunEmail(to string, token string) error {
|
||||
// link format: https://hyperia.so/verify?token=7a1a7cb986437cf8868b18cf43d73ce2e947d65aef30b42419bab957f5e51a09
|
||||
domain := "mg.hyperia.so"
|
||||
apiKey := "aeb90a0c75ef782eab6fc3d48fdf4435-812b35f5-fe818055"
|
||||
|
||||
mg := mailgun.NewMailgun(domain, apiKey)
|
||||
|
||||
sender := "welcome@" + domain
|
||||
subject := "Verify Your Email"
|
||||
verifyLink := config.BASE_URL + "/verify?token=" + token
|
||||
body := "Thanks for signing up! Please verify your email by clicking this link: " + verifyLink
|
||||
|
||||
message := mg.NewMessage(sender, subject, body, to)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
||||
defer cancel()
|
||||
|
||||
_, _, err := mg.Send(ctx, message)
|
||||
return err
|
||||
}
|
||||
|
||||
func sendWelcomeEmail(to string, token string) error {
|
||||
if config.ENV == "development" {
|
||||
verifyLink := config.BASE_URL + "/verify?token=" + token
|
||||
log.Debug().Msgf("email Verify Link: %s", verifyLink)
|
||||
return nil
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
// query := `
|
||||
// INSERT INTO emails ("to", "from", subject, body, createdon, createdby, status)
|
||||
// VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
// `
|
||||
|
||||
// sender := "noreply@mail.hyperia.so"
|
||||
// subject := "Verify Your Email"
|
||||
// verifyLink := config.BASE_URL + "/verify?token=" + token
|
||||
// body := "Thanks for signing up! Please verify your email by clicking this link: " + verifyLink
|
||||
|
||||
// _, err := DB.Exec(
|
||||
// query,
|
||||
// to,
|
||||
// sender,
|
||||
// subject,
|
||||
// body,
|
||||
// time.Now(), // createdon
|
||||
// "go-backend", // createdby
|
||||
// "pending", // status
|
||||
// )
|
||||
|
||||
// return err
|
||||
}
|
||||
107
server/handlers/login.go
Normal file
107
server/handlers/login.go
Normal file
@@ -0,0 +1,107 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
// "errors"
|
||||
"net/http"
|
||||
// "strings"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
// "github.com/alexedwards/argon2id"
|
||||
)
|
||||
|
||||
type loginRequest struct {
|
||||
Name string `json:"name"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
type user struct {
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
func HandleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "Only POST allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
var creds loginRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&creds); err != nil {
|
||||
http.Error(w, "Invalid JSON", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// user, err := getUserByCredentials(creds.Name, creds.Password)
|
||||
// if err != nil {
|
||||
// http.Error(w, "Unauthorized: "+err.Error(), http.StatusUnauthorized)
|
||||
// return
|
||||
// }
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
http.Error(w, "Not implemented", http.StatusMethodNotAllowed)
|
||||
// json.NewEncoder(w).Encode(user)
|
||||
}
|
||||
|
||||
// func getUserByCredentials(name string, password string) (*user, error) {
|
||||
// var id int
|
||||
// var dbName, dbHash string
|
||||
|
||||
// name = strings.TrimSpace(strings.ToLower(name))
|
||||
// err := DB.QueryRow("SELECT id, name, password FROM users WHERE LOWER(name) = LOWER($1)", name).Scan(&id, &dbName, &dbHash)
|
||||
// if err != nil {
|
||||
// return nil, errors.New("user not found")
|
||||
// }
|
||||
|
||||
// match, err := argon2id.ComparePasswordAndHash(password, dbHash)
|
||||
// if err != nil || !match {
|
||||
// return nil, errors.New("invalid password")
|
||||
// }
|
||||
|
||||
// return &user{
|
||||
// ID: id,
|
||||
// Name: dbName,
|
||||
// }, nil
|
||||
// }
|
||||
|
||||
func HandleApplicantLogin(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "Only POST allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
var creds loginRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&creds); err != nil {
|
||||
http.Error(w, "Invalid JSON", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// exists, err := EmailExists(creds.Name)
|
||||
// if err != nil {
|
||||
// log.Err(err).Msg("error checking email")
|
||||
// http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
// return
|
||||
// }
|
||||
// if !exists {
|
||||
// http.Error(w, "Email does not exist.", http.StatusConflict)
|
||||
// return
|
||||
// }
|
||||
|
||||
token, err := generateVerificationToken(creds.Name)
|
||||
if err != nil {
|
||||
log.Err(err).Msg("error generating verification token")
|
||||
http.Error(w, "Error, please try again later.", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
err = sendWelcomeEmail(creds.Name, token)
|
||||
if err != nil {
|
||||
log.Err(err).Msg("error sending welcome email")
|
||||
http.Error(w, "Failed to send email", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("OK"))
|
||||
}
|
||||
33
server/handlers/logout.go
Normal file
33
server/handlers/logout.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
"os"
|
||||
|
||||
"hyperia/config"
|
||||
)
|
||||
|
||||
func HandleLogout(w http.ResponseWriter, r *http.Request) {
|
||||
// Create a cookie with the same name and domain, but expired
|
||||
cookie := &http.Cookie{
|
||||
Name: "auth_token",
|
||||
Value: "",
|
||||
Path: "/",
|
||||
Domain: "." + os.Getenv("BASE_URL"), // must match what you set when logging in
|
||||
HttpOnly: true,
|
||||
Secure: true,
|
||||
Expires: time.Unix(0, 0), // way in the past
|
||||
MaxAge: -1, // tells browser to delete immediately
|
||||
SameSite: http.SameSiteLaxMode,
|
||||
}
|
||||
|
||||
if config.ENV == "development" {
|
||||
cookie.Secure = false
|
||||
cookie.Domain = ".hyperia.local"
|
||||
}
|
||||
|
||||
http.SetCookie(w, cookie)
|
||||
|
||||
http.Redirect(w, r, config.BASE_URL, http.StatusSeeOther)
|
||||
}
|
||||
82
server/handlers/verify.go
Normal file
82
server/handlers/verify.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
// "os"
|
||||
"time"
|
||||
|
||||
"hyperia/config"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
)
|
||||
|
||||
func GenerateJWT(applicantId int) (string, error) {
|
||||
claims := jwt.MapClaims{
|
||||
"applicant_id": applicantId,
|
||||
"exp": time.Now().Add(2 * time.Hour).Unix(), // expires in 2 hours
|
||||
"iat": time.Now().Unix(),
|
||||
}
|
||||
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||
jwtSecret := []byte(config.JWT_SECRET)
|
||||
signedToken, err := token.SignedString(jwtSecret)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return signedToken, nil
|
||||
}
|
||||
|
||||
func HandleVerify(w http.ResponseWriter, r *http.Request) {
|
||||
// token := r.URL.Query().Get("token")
|
||||
// if token == "" {
|
||||
// http.Error(w, "Missing token", http.StatusBadRequest)
|
||||
// return
|
||||
// }
|
||||
|
||||
// v, err := GetApplicantVerificationByToken(token)
|
||||
// if err != nil {
|
||||
// log.Println("Invalid token: ", token)
|
||||
// http.Error(w, "Invalid token", http.StatusUnauthorized)
|
||||
// return
|
||||
// }
|
||||
|
||||
// if time.Since(v.CreatedOn) > 30*time.Minute || v.Expired {
|
||||
// log.Println("Token expired: ", token)
|
||||
// http.Error(w, "Token expired", http.StatusUnauthorized)
|
||||
// return
|
||||
// }
|
||||
|
||||
// _, err = DB.Exec(`
|
||||
// UPDATE ApplicantVerifications SET Expired = 1 WHERE ApplicantId = $1
|
||||
// `, v.ApplicantId)
|
||||
// if err != nil {
|
||||
// http.Error(w, "Failed to update verification", http.StatusInternalServerError)
|
||||
// return
|
||||
// }
|
||||
|
||||
// jwtToken, err := GenerateJWT(v.ApplicantId)
|
||||
// if err != nil {
|
||||
// log.Println("JWT generation error:", err)
|
||||
// http.Error(w, "Failed to generate auth token", http.StatusInternalServerError)
|
||||
// return
|
||||
// }
|
||||
|
||||
// cookie := &http.Cookie{
|
||||
// Name: "auth_token",
|
||||
// Value: jwtToken,
|
||||
// Path: "/",
|
||||
// HttpOnly: true,
|
||||
// Domain: "." + os.Getenv("BASE_URL"), // or ".localhost" — this allows subdomains
|
||||
// Secure: true, // default to true (production)
|
||||
// MaxAge: 2 * 60 * 60,
|
||||
// SameSite: http.SameSiteLaxMode,
|
||||
// }
|
||||
// if config.ENV == "development" {
|
||||
// cookie.Secure = false
|
||||
// cookie.Domain = ".hyperia.local"
|
||||
// }
|
||||
|
||||
// http.SetCookie(w, cookie)
|
||||
log.Println("Verification success.")
|
||||
http.Redirect(w, r, config.BASE_URL, http.StatusSeeOther)
|
||||
}
|
||||
28
server/logger/logger.go
Normal file
28
server/logger/logger.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"hyperia/config"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
"gopkg.in/natefinch/lumberjack.v2"
|
||||
)
|
||||
|
||||
// Very basic setup for starters,
|
||||
func ConfigureLogger() {
|
||||
if !config.LOG_TO_FILE {
|
||||
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
|
||||
} else {
|
||||
fmt.Println("logging to file /var/log/hyperia-server.log")
|
||||
logFile := &lumberjack.Logger{
|
||||
Filename: "/var/log/hyperia-server.log", // Path to your log file
|
||||
MaxSize: 100, // Max size in MB before rotation
|
||||
MaxBackups: 3, // Max number of old log files to keep
|
||||
MaxAge: 28, // Max number of days to retain old log files
|
||||
Compress: true, // Whether to compress old log files
|
||||
}
|
||||
log.Logger = zerolog.New(logFile).With().Timestamp().Logger()
|
||||
}
|
||||
}
|
||||
180
server/main.go
Normal file
180
server/main.go
Normal file
@@ -0,0 +1,180 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"hyperia/config"
|
||||
"hyperia/handlers"
|
||||
"hyperia/logger"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func main() {
|
||||
config.SetConfiguration()
|
||||
|
||||
logger.ConfigureLogger()
|
||||
|
||||
// err := handlers.InitDB()
|
||||
// if err != nil {
|
||||
// log.Fatal().Msgf("failed to connect to database: %v", err)
|
||||
// } else {
|
||||
// log.Info().Msg("successfully connected to PostgreSQL")
|
||||
// }
|
||||
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
log.Error().
|
||||
Interface("panic_reason", r).
|
||||
Bytes("stack_trace", debug.Stack()).
|
||||
Msg("panic in http goroutine")
|
||||
}
|
||||
}()
|
||||
|
||||
subdomain := ""
|
||||
host := strings.Split(r.Host, ":")[0] // remove port
|
||||
parts := strings.Split(host, ".")
|
||||
if len(parts) > 2 || (len(parts) > 1 && parts[1] == "localhost") {
|
||||
subdomain = parts[0]
|
||||
}
|
||||
|
||||
if strings.HasPrefix(r.URL.Path, "/_") {
|
||||
handleAsset(w, r)
|
||||
} else if subdomain == "apply" {
|
||||
authMiddleware(handleApply)(w, r)
|
||||
} else if subdomain == "pma" {
|
||||
authMiddleware(handlePMA)(w, r)
|
||||
} else {
|
||||
handlePublic(w, r)
|
||||
}
|
||||
})
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func handlePublic(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/api/login" {
|
||||
handlers.HandleLogin(w, r)
|
||||
return
|
||||
}
|
||||
if r.URL.Path == "/api/applicantlogin" {
|
||||
handlers.HandleApplicantLogin(w, r)
|
||||
return
|
||||
}
|
||||
if r.URL.Path == "/api/join" {
|
||||
handlers.HandleJoin(w, r)
|
||||
return
|
||||
}
|
||||
if r.URL.Path == "/verify" {
|
||||
handlers.HandleVerify(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
servePublicFile(w, r)
|
||||
}
|
||||
|
||||
func handleAsset(w http.ResponseWriter, r *http.Request) {
|
||||
path := r.URL.Path
|
||||
filePath := filepath.Join("../ui", path)
|
||||
log.Debug().Msgf("serving asset: %s", filePath)
|
||||
http.ServeFile(w, r, filePath)
|
||||
}
|
||||
|
||||
func servePublicFile(w http.ResponseWriter, r *http.Request) {
|
||||
path := r.URL.Path
|
||||
|
||||
if path == "/" {
|
||||
path = "/index.html"
|
||||
} else if !strings.Contains(path, ".") {
|
||||
path = filepath.Join("/pages", path) + ".html"
|
||||
}
|
||||
|
||||
filePath := filepath.Join("../ui/public", path)
|
||||
log.Debug().Msgf("serving: %s", filePath)
|
||||
http.ServeFile(w, r, filePath)
|
||||
}
|
||||
|
||||
func authMiddleware(next http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
cookie, err := r.Cookie("auth_token")
|
||||
if err != nil {
|
||||
log.Warn().Msg("Unauthorized - missing auth token")
|
||||
http.Error(w, "Unauthorized - missing auth token", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
jwtToken := cookie.Value
|
||||
|
||||
token, err := jwt.Parse(jwtToken, func(token *jwt.Token) (interface{}, error) {
|
||||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
|
||||
}
|
||||
return []byte(config.JWT_SECRET), nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
log.Err(err).Msg("error authenticating jwt")
|
||||
}
|
||||
if err != nil || !token.Valid {
|
||||
http.Error(w, "Unauthorized - invalid auth token", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
next(w, r)
|
||||
}
|
||||
}
|
||||
|
||||
func handleApply(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// if r.URL.Path == "/api/application-save" {
|
||||
// handlers.HandleApplicationSubmit(w, r)
|
||||
// return
|
||||
// }
|
||||
// if r.URL.Path == "/api/get-application" {
|
||||
// handlers.HandleGetApplication(w, r)
|
||||
// return
|
||||
// }
|
||||
// if r.URL.Path == "/logout" {
|
||||
// handlers.HandleLogout(w, r)
|
||||
// return
|
||||
// }
|
||||
// if r.URL.Path == "/" {
|
||||
// handlers.CheckApplicationCompleteMiddleware(w, r)
|
||||
// }
|
||||
// if r.URL.Path == "/complete" {
|
||||
// handlers.ApplicationSubmitMiddleware(w, r)
|
||||
// }
|
||||
|
||||
path := r.URL.Path
|
||||
if path == "/" {
|
||||
path = "/index.html"
|
||||
} else if !strings.Contains(path, ".") {
|
||||
path = filepath.Join("/pages", path) + ".html"
|
||||
}
|
||||
|
||||
filePath := filepath.Join("../ui/apply", path)
|
||||
log.Debug().Msgf("Serving apply subdomain: %s", filePath)
|
||||
http.ServeFile(w, r, filePath)
|
||||
}
|
||||
|
||||
func handlePMA(w http.ResponseWriter, r *http.Request) {
|
||||
path := r.URL.Path
|
||||
if path == "/" {
|
||||
path = "/index.html"
|
||||
} else if !strings.Contains(path, ".") {
|
||||
path = filepath.Join("/pages", path) + ".html"
|
||||
}
|
||||
|
||||
filePath := filepath.Join("../ui/pma", path)
|
||||
log.Debug().Msgf("serving pma subdomain: %s", filePath)
|
||||
http.ServeFile(w, r, filePath)
|
||||
}
|
||||
Reference in New Issue
Block a user