Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
vladyslavpavlenko committed May 16, 2024
0 parents commit 562845a
Show file tree
Hide file tree
Showing 9 changed files with 262 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work

# Other
.idea
.DS_Store
.env
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# genesis-api-project
32 changes: 32 additions & 0 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"flag"
"github.com/vladyslavpavlenko/genesis-api-project/internal/config"
"log"
"net/http"
)

var app config.AppConfig

func main() {
err := setup(&app)
if err != nil {
log.Fatal()
}

addr := flag.String("addr", ":8080", "the api address")
flag.Parse()

log.Printf("Running on port %s", *addr)

srv := &http.Server{
Addr: *addr,
Handler: routes(&app),
}

err = srv.ListenAndServe()
if err != nil {
log.Panic(err)
}
}
30 changes: 30 additions & 0 deletions cmd/api/routes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"github.com/go-chi/chi"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/cors"
"github.com/vladyslavpavlenko/genesis-api-project/internal/config"
"github.com/vladyslavpavlenko/genesis-api-project/internal/handlers"
"net/http"
)

func routes(app *config.AppConfig) http.Handler {
mux := chi.NewRouter()
mux.Use(middleware.Logger)

mux.Use(cors.Handler(cors.Options{
AllowedOrigins: []string{"https://*", "http://*"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
ExposedHeaders: []string{"Link"},
AllowCredentials: true,
MaxAge: 300,
}))

mux.Get("/rate", handlers.Repo.GetRate)
mux.Post("/subscribe", handlers.Repo.Subscribe)
mux.Post("/sendEmails", handlers.Repo.SendEmails)

return mux
}
69 changes: 69 additions & 0 deletions cmd/api/setup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package main

import (
"fmt"
"github.com/joho/godotenv"
"github.com/vladyslavpavlenko/genesis-api-project/internal/config"
"github.com/vladyslavpavlenko/genesis-api-project/internal/handlers"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"log"
"os"
)

func setup(app *config.AppConfig) error {
// Get environment variables
env, err := loadEvnVariables()
if err != nil {
return err
}

app.Env = env

// Connect to the database and run migrations
db, err := connectToPostgresAndMigrate(env)
if err != nil {
return err
}

app.DB = db

repo := handlers.NewRepo(app)
handlers.NewHandlers(repo)
render.NewRenderer(app)

return nil
}

// loadEvnVariables loads variables from the .env file.
func loadEvnVariables() (*config.EnvVariables, error) {
err := godotenv.Load()
if err != nil {
return nil, fmt.Errorf("error getting environment variables: %v", err)
}

postgresHost := os.Getenv("POSTGRES_HOST")
postgresUser := os.Getenv("POSTGRES_USER")
postgresPass := os.Getenv("POSTGRES_PASS")
postgresDBName := os.Getenv("POSTGRES_DBNAME")
jwtSecret := os.Getenv("JWT_SECRET")

return &config.EnvVariables{
PostgresHost: postgresHost,
PostgresUser: postgresUser,
PostgresPass: postgresPass,
PostgresDBName: postgresDBName,
}, nil
}

// connectToPostgresAndMigrate initializes a PostgreSQL db session and runs GORM migrations.
func connectToPostgresAndMigrate(env *config.EnvVariables) (*gorm.DB, error) {
dsn := fmt.Sprintf("host=%s user=%s dbname=%s password=%s sslmode=disable",
env.PostgresHost, env.PostgresUser, env.PostgresDBName, env.PostgresPass)
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
log.Fatal("could not connect: ", err)
}

return db, nil
}
19 changes: 19 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module github.com/vladyslavpavlenko/genesis-api-project

go 1.22.2

require (
github.com/go-chi/chi v1.5.5 // indirect
github.com/go-chi/chi/v5 v5.0.12 // indirect
github.com/go-chi/cors v1.2.1 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.4.3 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/joho/godotenv v1.5.1 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/text v0.13.0 // indirect
gorm.io/driver/postgres v1.5.7 // indirect
gorm.io/gorm v1.25.10 // indirect
)
33 changes: 33 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-chi/chi v1.5.5 h1:vOB/HbEMt9QqBqErz07QehcOKHaWFtuj87tTDVz2qXE=
github.com/go-chi/chi v1.5.5/go.mod h1:C9JqLr3tIYjDOZpzn+BCuxY8z8vmca43EeMgyZt7irw=
github.com/go-chi/chi/v5 v5.0.12 h1:9euLV5sTrTNTRUU9POmDUvfxyj6LAABLUcEWO+JJb4s=
github.com/go-chi/chi/v5 v5.0.12/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-chi/cors v1.2.1 h1:xEC8UT3Rlp2QuWNEr4Fs/c2EAGVKBwy/1vHx3bppil4=
github.com/go-chi/cors v1.2.1/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.4.3 h1:cxFyXhxlvAifxnkKKdlxv8XqUf59tDlYjnV5YYfsJJY=
github.com/jackc/pgx/v5 v5.4.3/go.mod h1:Ig06C2Vu0t5qXC60W8sqIthScaEnFvojjj9dSljmHRA=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
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.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/postgres v1.5.7 h1:8ptbNJTDbEmhdr62uReG5BGkdQyeasu/FZHxI0IMGnM=
gorm.io/driver/postgres v1.5.7/go.mod h1:3e019WlBaYI5o5LIdNV+LyxCMNtLOQETBXL2h4chKpA=
gorm.io/gorm v1.25.10 h1:dQpO+33KalOA+aFYGlK+EfxcI5MbO7EP2yYygwh9h+s=
gorm.io/gorm v1.25.10/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
19 changes: 19 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package config

import (
"gorm.io/gorm"
)

// AppConfig holds the application config.
type AppConfig struct {
DB *gorm.DB
Env *EnvVariables
}

// EnvVariables holds environment variables used in the application.
type EnvVariables struct {
PostgresHost string
PostgresUser string
PostgresPass string
PostgresDBName string
}
36 changes: 36 additions & 0 deletions internal/handlers/handlers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package handlers

import "github.com/vladyslavpavlenko/genesis-api-project/internal/config"

type jsonResponse struct {
Error bool `json:"error"`
Message string `json:"message,omitempty"`
Data any `json:"data,omitempty"`
}

// Repo the repository used by the handlers
var Repo *Repository

// Repository is the repository type
type Repository struct {
App *config.AppConfig
}

// NewRepo creates a new repository
func NewRepo(a *config.AppConfig) *Repository {
return &Repository{
App: a,
}
}

// NewTestRepo creates a new test repository
func NewTestRepo(a *config.AppConfig) *Repository {
return &Repository{
App: a,
}
}

// NewHandlers sets the repository for handlers
func NewHandlers(r *Repository) {
Repo = r
}

0 comments on commit 562845a

Please sign in to comment.