Skip to content
Merged
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
1 change: 0 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ ENV_HTTP_HOST=localhost
ENV_HTTP_PORT=8080

# --- App super admin credentials
ENV_APP_TOKEN_USERNAME=""
ENV_APP_TOKEN_PUBLIC=""
ENV_APP_TOKEN_PRIVATE=""

Expand Down
64 changes: 37 additions & 27 deletions boost/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,53 @@ package boost
import (
"github.com/oullin/database"
"github.com/oullin/env"
"github.com/oullin/handler/user"
"github.com/oullin/pkg"
"github.com/oullin/pkg/http/middleware"
"github.com/oullin/pkg/llogs"
"github.com/oullin/pkg/middleware"
"net/http"
baseHttp "net/http"
)

type App struct {
Validator *pkg.Validator `validate:"required"`
Logs *llogs.Driver `validate:"required"`
DbConnection *database.Connection `validate:"required"`
AdminUser *user.AdminUser `validate:"required"`
Env *env.Environment `validate:"required"`
Mux *http.ServeMux `validate:"required"`
Sentry *pkg.Sentry `validate:"required"`
router *Router
sentry *pkg.Sentry
logs *llogs.Driver
validator *pkg.Validator
env *env.Environment
db *database.Connection
}

func MakeApp(mux *http.ServeMux, app *App) *App {
app.Mux = mux
func MakeApp(env *env.Environment, validator *pkg.Validator) *App {
app := App{
env: env,
validator: validator,
logs: MakeLogs(env),
sentry: MakeSentry(env),
db: MakeDbConnection(env),
}

return app
}
router := Router{
Env: env,
Mux: baseHttp.NewServeMux(),
Pipeline: middleware.Pipeline{
Env: env,
},
}

func (app App) RegisterUsers() {
stack := middleware.MakeMiddlewareStack(app.Env, func(seed string) bool {
return app.AdminUser.IsAllowed(seed)
})
app.SetRouter(router)

handler := user.RequestHandler{
Repository: user.MakeRepository(app.DbConnection, app.AdminUser),
Validator: app.Validator,
return &app
}

func (a *App) Boot() {
if a.router == nil {
panic("Router is not set")
}

app.Mux.HandleFunc("POST /users", pkg.CreateHandle(
stack.Push(
handler.Create,
stack.AdminUser,
),
))
router := *a.router

router.Profile()
router.Experience()
router.Projects()
router.Social()
router.Talks()
}
9 changes: 4 additions & 5 deletions boost/boost.go → boost/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func MakeLogs(env *env.Environment) *llogs.Driver {
lDriver, err := llogs.MakeFilesLogs(env)

if err != nil {
panic("Logs: error opening logs file: " + err.Error())
panic("logs: error opening logs file: " + err.Error())
}

return &lDriver
Expand All @@ -62,9 +62,8 @@ func MakeEnv(values map[string]string, validate *pkg.Validator) *env.Environment
port, _ := strconv.Atoi(values["ENV_DB_PORT"])

token := auth.Token{
Username: strings.TrimSpace(values["ENV_APP_TOKEN_USERNAME"]),
Public: strings.TrimSpace(values["ENV_APP_TOKEN_PUBLIC"]),
Private: strings.TrimSpace(values["ENV_APP_TOKEN_PRIVATE"]),
Public: strings.TrimSpace(values["ENV_APP_TOKEN_PUBLIC"]),
Private: strings.TrimSpace(values["ENV_APP_TOKEN_PRIVATE"]),
}

app := env.AppEnvironment{
Expand Down Expand Up @@ -115,7 +114,7 @@ func MakeEnv(values map[string]string, validate *pkg.Validator) *env.Environment
}

if _, err := validate.Rejects(logsCreds); err != nil {
panic(errorSufix + "invalid [Logs Creds] model: " + validate.GetErrorsAsJason())
panic(errorSufix + "invalid [logs Creds] model: " + validate.GetErrorsAsJason())
}

if _, err := validate.Rejects(net); err != nil {
Expand Down
45 changes: 45 additions & 0 deletions boost/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package boost

import (
"github.com/oullin/database"
"github.com/oullin/env"
baseHttp "net/http"
)

func (a *App) SetRouter(router Router) {
a.router = &router
}

func (a *App) CloseLogs() {
if a.logs == nil {
return
}

driver := *a.logs
driver.Close()
}

func (a *App) CloseDB() {
if a.db == nil {
return
}

driver := *a.db
driver.Close()
}

func (a *App) GetEnv() *env.Environment {
return a.env
}

func (a *App) GetDB() *database.Connection {
return a.db
}

func (a *App) GetMux() *baseHttp.ServeMux {
if a.router == nil {
return nil
}

return a.router.Mux
}
2 changes: 1 addition & 1 deletion boost/spark.go → boost/ignite.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/oullin/pkg"
)

func Spark(envPath string) (*env.Environment, *pkg.Validator) {
func Ignite(envPath string) (*env.Environment, *pkg.Validator) {
validate := pkg.GetDefaultValidator()

envMap, err := godotenv.Read(envPath)
Expand Down
79 changes: 79 additions & 0 deletions boost/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package boost

import (
"github.com/oullin/env"
"github.com/oullin/handler"
"github.com/oullin/pkg/http"
"github.com/oullin/pkg/http/middleware"
baseHttp "net/http"
)

type Router struct {
Env *env.Environment
Mux *baseHttp.ServeMux
Pipeline middleware.Pipeline
}

func (r *Router) PipelineFor(apiHandler http.ApiHandler) baseHttp.HandlerFunc {
tokenMiddleware := middleware.MakeTokenMiddleware(
r.Env.App.Credentials,
)

return http.MakeApiHandler(
r.Pipeline.Chain(
apiHandler,
middleware.UsernameCheck,
tokenMiddleware.Handle,
),
)
}

func (r *Router) Profile() {
abstract := handler.MakeProfileHandler()

resolver := r.PipelineFor(
abstract.Handle,
)

r.Mux.HandleFunc("GET /profile", resolver)
}

func (r *Router) Experience() {
abstract := handler.MakeExperienceHandler()

resolver := r.PipelineFor(
abstract.Handle,
)

r.Mux.HandleFunc("GET /experience", resolver)
}

func (r *Router) Projects() {
abstract := handler.MakeProjectsHandler()

resolver := r.PipelineFor(
abstract.Handle,
)

r.Mux.HandleFunc("GET /projects", resolver)
}

func (r *Router) Social() {
abstract := handler.MakeSocialHandler()

resolver := r.PipelineFor(
abstract.Handle,
)

r.Mux.HandleFunc("GET /social", resolver)
}

func (r *Router) Talks() {
abstract := handler.MakeTalks()

resolver := r.PipelineFor(
abstract.Handle,
)

r.Mux.HandleFunc("GET /talks", resolver)
}
2 changes: 1 addition & 1 deletion cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var guard gate.Guard
var environment *env.Environment

func init() {
secrets, _ := boost.Spark("./../.env")
secrets, _ := boost.Ignite("./../.env")

environment = secrets
guard = gate.MakeGuard(environment.App.Credentials)
Expand Down
8 changes: 4 additions & 4 deletions database/seeder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
var environment *env.Environment

func init() {
secrets, _ := boost.Spark("./.env")
secrets, _ := boost.Ignite("./.env")

environment = secrets
}
Expand All @@ -30,11 +30,11 @@ func main() {
// [1] --- Create the Seeder Runner.
seeder := seeds.MakeSeeder(dbConnection, environment)

// [2] --- Truncate the DB.
// [2] --- Truncate the db.
if err := seeder.TruncateDB(); err != nil {
panic(err)
} else {
cli.Successln("DB Truncated successfully ...")
cli.Successln("db Truncated successfully ...")
time.Sleep(2 * time.Second)
}

Expand Down Expand Up @@ -114,5 +114,5 @@ func main() {

wg.Wait()

cli.Magentaln("DB seeded as expected ....")
cli.Magentaln("db seeded as expected ....")
}
2 changes: 1 addition & 1 deletion database/seeder/seeds/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func MakeSeeder(dbConnection *database.Connection, environment *env.Environment)

func (s *Seeder) TruncateDB() error {
if s.environment.App.IsProduction() {
return fmt.Errorf("cannot truncate DB at the seeder level")
return fmt.Errorf("cannot truncate db at the seeder level")
}

truncate := database.MakeTruncate(s.dbConn, s.environment)
Expand Down
1 change: 0 additions & 1 deletion env/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import "github.com/oullin/pkg/auth"
const local = "local"
const staging = "staging"
const production = "production"
const ApiKeyHeader = "X-API-Key"

type AppEnvironment struct {
Name string `validate:"required,min=4"`
Expand Down
34 changes: 34 additions & 0 deletions handler/experience.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package handler

import (
"github.com/oullin/pkg/http"
"log/slog"
baseHttp "net/http"
"os"
)

type ExperienceHandler struct {
content string
}

func MakeExperienceHandler() ExperienceHandler {
return ExperienceHandler{
content: "./storage/fixture/experience.json",
}
}

func (h ExperienceHandler) Handle(w baseHttp.ResponseWriter, r *baseHttp.Request) *http.ApiError {
fixture, err := os.ReadFile(h.content)

if err != nil {
slog.Error("Error reading projects file", "error", err)

return http.InternalError("could not read experience data")
}

if err := writeJSON(fixture, w); err != nil {
return http.InternalError(err.Error())
}

return nil // A nil return indicates success.
}
34 changes: 34 additions & 0 deletions handler/profile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package handler

import (
"github.com/oullin/pkg/http"
"log/slog"
baseHttp "net/http"
"os"
)

type ProfileHandler struct {
content string
}

func MakeProfileHandler() ProfileHandler {
return ProfileHandler{
content: "./storage/fixture/profile.json",
}
}

func (h ProfileHandler) Handle(w baseHttp.ResponseWriter, r *baseHttp.Request) *http.ApiError {
fixture, err := os.ReadFile(h.content)

if err != nil {
slog.Error("Error reading projects file", "error", err)

return http.InternalError("could not read profile data")
}

if err := writeJSON(fixture, w); err != nil {
return http.InternalError(err.Error())
}

return nil // A nil return indicates success.
}
Loading
Loading