Added documentation to functions and basic JWT generation
This commit is contained in:
63
server/config.go
Normal file
63
server/config.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Host string // Host to listen on
|
||||
Port string // Port to listen on
|
||||
TrustedHost string // Domain/Hostname to accept as trusted
|
||||
TursoDBName string // DB Name for Turso DB/Branch
|
||||
TursoToken string // Bearer token for Turso DB/Branch
|
||||
SecretKey string // Secret key for signing tokens
|
||||
AccessTokenExpiry int64 // Access token expiry in minutes
|
||||
RefreshTokenExpiry int64 // Refresh token expiry in minutes
|
||||
TokenFreshTime int64 // Time for tokens to stay fresh in minutes
|
||||
}
|
||||
|
||||
// Load the application configuration and get a pointer to the Config object
|
||||
func GetConfig(args []string) (*Config, error) {
|
||||
err := godotenv.Load(".env")
|
||||
if err != nil {
|
||||
fmt.Println(".env file not found.")
|
||||
}
|
||||
var port string
|
||||
|
||||
if args[0] != "" {
|
||||
port = args[0]
|
||||
} else {
|
||||
port = GetEnvDefault("PORT", "3333")
|
||||
}
|
||||
|
||||
config := &Config{
|
||||
Host: GetEnvDefault("HOST", "127.0.0.1"),
|
||||
Port: port,
|
||||
TrustedHost: os.Getenv("TRUSTED_HOST"),
|
||||
TursoDBName: os.Getenv("TURSO_DB_NAME"),
|
||||
TursoToken: os.Getenv("TURSO_AUTH_TOKEN"),
|
||||
SecretKey: os.Getenv("SECRET_KEY"),
|
||||
AccessTokenExpiry: GetEnvInt64("ACCESS_TOKEN_EXPIRY", 5),
|
||||
RefreshTokenExpiry: GetEnvInt64("REFRESH_TOKEN_EXPIRY", 1440), // defaults to 1 day
|
||||
TokenFreshTime: GetEnvInt64("TOKEN_FRESH_TIME", 5),
|
||||
}
|
||||
|
||||
if config.TrustedHost == "" {
|
||||
return nil, errors.New("Envar not set: TRUSTED_HOST")
|
||||
}
|
||||
if config.TursoDBName == "" {
|
||||
return nil, errors.New("Envar not set: TURSO_DB_NAME")
|
||||
}
|
||||
if config.TursoToken == "" {
|
||||
return nil, errors.New("Envar not set: TURSO_AUTH_TOKEN")
|
||||
}
|
||||
if config.SecretKey == "" {
|
||||
return nil, errors.New("Envar not set: SECRET_KEY")
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
31
server/environment.go
Normal file
31
server/environment.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// Get an environment variable, specifying a default value if its not set
|
||||
func GetEnvDefault(key string, defaultValue string) string {
|
||||
val, exists := os.LookupEnv(key)
|
||||
if !exists {
|
||||
return defaultValue
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
// Get an environment variable as an int64, specifying a default value if its
|
||||
// not set or can't be parsed properly into an int64
|
||||
func GetEnvInt64(key string, defaultValue int64) int64 {
|
||||
val, exists := os.LookupEnv(key)
|
||||
if !exists {
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
intVal, err := strconv.ParseInt(val, 10, 64)
|
||||
if err != nil {
|
||||
return defaultValue
|
||||
}
|
||||
return intVal
|
||||
|
||||
}
|
||||
@@ -3,10 +3,12 @@ package server
|
||||
import (
|
||||
"database/sql"
|
||||
"net/http"
|
||||
|
||||
"projectreshoot/handlers"
|
||||
"projectreshoot/view/page"
|
||||
)
|
||||
|
||||
// Add all the handled routes to the mux
|
||||
func addRoutes(
|
||||
mux *http.ServeMux,
|
||||
config *Config,
|
||||
@@ -23,5 +25,5 @@ func addRoutes(
|
||||
|
||||
// Login page and handlers
|
||||
mux.Handle("GET /login", handlers.HandleLoginPage(config.TrustedHost))
|
||||
mux.Handle("POST /login", handlers.HandleLoginRequest(conn))
|
||||
mux.Handle("POST /login", handlers.HandleLoginRequest(conn, config.SecretKey))
|
||||
}
|
||||
|
||||
@@ -2,56 +2,12 @@ package server
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"projectreshoot/middleware"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Host string
|
||||
Port string
|
||||
TrustedHost string
|
||||
TursoURL string
|
||||
TursoToken string
|
||||
}
|
||||
|
||||
func GetConfig() (*Config, error) {
|
||||
err := godotenv.Load(".env")
|
||||
if err != nil {
|
||||
fmt.Println(".env file not found.")
|
||||
}
|
||||
|
||||
config := &Config{
|
||||
Host: os.Getenv("HOST"),
|
||||
Port: os.Getenv("PORT"),
|
||||
TrustedHost: os.Getenv("TRUSTED_HOST"),
|
||||
TursoURL: os.Getenv("TURSO_DATABASE_URL"),
|
||||
TursoToken: os.Getenv("TURSO_AUTH_TOKEN"),
|
||||
}
|
||||
if config.Host == "" {
|
||||
return nil, errors.New("Envar not set: HOST")
|
||||
}
|
||||
if config.Port == "" {
|
||||
return nil, errors.New("Envar not set: PORT")
|
||||
}
|
||||
if config.TrustedHost == "" {
|
||||
return nil, errors.New("Envar not set: TRUSTED_HOST")
|
||||
}
|
||||
if config.TursoURL == "" {
|
||||
return nil, errors.New("Envar not set: TURSO_DATABASE_URL")
|
||||
}
|
||||
if config.TursoToken == "" {
|
||||
return nil, errors.New("Envar not set: TURSO_AUTH_TOKEN")
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
|
||||
// Returns a new http.Handler with all the routes and middleware added
|
||||
func NewServer(config *Config, conn *sql.DB) http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
addRoutes(
|
||||
|
||||
Reference in New Issue
Block a user