Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some fixes, added simple frontend framework #21

Merged
merged 6 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 0 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ DEBUG_MODE=true
# External Services

## PSQL
### To enable PSQL connection, set PSQL_ENABLED true
PSQL_ENABLED=false

POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_USER=postgres
Expand Down
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,25 @@ cp .env.example .env
nano .env
```

4. Run the application
4. Setup the database

```bash
make run || go run main.go
docker-compose up -d db
```

5. Visit the application in your browser
5. Run the migrations

```bash
make migrate-up
```

6. Run the application

```bash
make run
```

7. Visit the application in your browser

Feel free to visit the application at `localhost:8000` and move around available paths

Expand Down
1 change: 1 addition & 0 deletions controller/api/get_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"net/http"

sql "web/repository/db"

"github.com/gin-gonic/gin"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ctrlerror
package httperr

import (
"io"
Expand All @@ -17,7 +17,9 @@ func StatusNotFound(c *gin.Context) {
log.Printf("error opening file: %v", err)
return
}

defer file.Close()

if _, err := io.Copy(c.Writer, file); err != nil {
if err := c.Error(err); err != nil {
log.Printf("error adding error to Gin context: %v", err)
Expand Down
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ go 1.22.1
require (
github.com/jarcoal/httpmock v1.3.1
github.com/stretchr/testify v1.9.0
gorm.io/driver/postgres v1.5.7
gorm.io/gorm v1.25.10
)

require (
Expand All @@ -13,6 +15,11 @@ require (
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/go-cmp v0.6.0 // 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/kr/pretty v0.3.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
Expand Down
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,18 @@ github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MG
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
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/jarcoal/httpmock v1.3.1 h1:iUx3whfZWVf3jT01hQTO/Eo5sAYtB2/rqaUuOtpInww=
github.com/jarcoal/httpmock v1.3.1/go.mod h1:3yb8rc4BI7TCBhFY8ng0gjuLKJNquuDNiPaZjnENuYg=
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/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
Expand Down Expand Up @@ -103,5 +113,9 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EV
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/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=
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
13 changes: 13 additions & 0 deletions helpers/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package helper

import "os"

func CheckFileExists(path string) bool {
_, err := os.Stat(path)
return !os.IsNotExist(err)
}

func CheckFileNotExists(path string) bool {
_, err := os.Stat(path)
return os.IsNotExist(err)
}
KostLinux marked this conversation as resolved.
Show resolved Hide resolved
32 changes: 32 additions & 0 deletions helpers/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package helper

import (
"log"
"os"
"strings"

"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
)

func ServePageAssets(router *gin.Engine) {
if CheckFileExists("./public/index.html") {
router.Use(static.Serve("/assets", static.LocalFile("./public/assets", true)))
} else {
router.Use(static.Serve("/assets", static.LocalFile("./views/assets", true)))
assetsPerPage(router)
}
}

func assetsPerPage(router *gin.Engine) {
assetsDir, err := os.ReadDir("./views")
if err != nil {
log.Fatal(err)
}

for _, directory := range assetsDir {
if directory.IsDir() && strings.Contains(directory.Name(), "_page") {
router.Use(static.Serve("/"+directory.Name()+"/assets", static.LocalFile("./views/"+directory.Name()+"/assets", true)))
}
}
}
26 changes: 13 additions & 13 deletions middleware/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"web/controller/api"
errorController "web/controller/error"
helper "web/helpers"
httpTemplates "web/views/templates"

"github.com/gin-contrib/static"
Expand Down Expand Up @@ -35,19 +36,23 @@ func NewRouter(router *gin.Engine) *gin.Engine {
c.Header("Permissions-Policy", "geolocation=(),midi=(),sync-xhr=(),microphone=(),camera=(),magnetometer=(),gyroscope=(),fullscreen=(self),payment=()")
})

if _, err := os.Stat("./public/index.html"); os.IsNotExist(err) {
// Load welcome page from html template
// Frontend & Static Files Handling
router.LoadHTMLGlob("./views/**/*.html")

router.Use(static.Serve("/assets", static.LocalFile("./public/assets", true)))
if helper.CheckFileNotExists("./public/index.html") {
router.GET("/", WelcomePageMiddleware())
router.GET("/favicon.ico", func(c *gin.Context) {
c.String(http.StatusNoContent, "")
})
} else {
// Handle static files from the public folder
router.Use(static.Serve("/", static.LocalFile("./public/", true)))
}

// Serve static assets
router.Use(static.Serve("/assets", static.LocalFile("./public/assets", true)))
router.GET("/favicon.ico", func(c *gin.Context) {
c.String(http.StatusNoContent, "")
})
helper.ServePageAssets(router)

// HTML Template generated pages
router.GET("/status", httpTemplates.StatusPageResponse(), StatusPageMiddleware())

// API Handling
apiGroup := router.Group(os.Getenv("API_PATH"))
Expand All @@ -56,11 +61,6 @@ func NewRouter(router *gin.Engine) *gin.Engine {
apiGroup.GET("/users", api.GetUsers)
}

// HTML Templates (e.g Status page)
router.LoadHTMLGlob("./views/**/*")
router.GET("/status", httpTemplates.StatusPageResponse(), StatusPageMiddleware())
router.GET("/docs", DocumentationPageMiddleware())

KostLinux marked this conversation as resolved.
Show resolved Hide resolved
// Error handling
router.NoRoute(errorController.StatusNotFound)

Expand Down
10 changes: 2 additions & 8 deletions middleware/sites.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
// StatusPageMiddleware handles the status page
func StatusPageMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
statuses := c.MustGet("statuses").([]map[string]string)
status := c.MustGet("statuses").([]map[string]string)
c.HTML(http.StatusOK, "status.html", gin.H{
"services": statuses,
"services": status,
})
}
}
Expand All @@ -22,9 +22,3 @@ func WelcomePageMiddleware() gin.HandlerFunc {
c.HTML(http.StatusOK, "welcome.html", gin.H{})
}
}

func DocumentationPageMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.HTML(http.StatusOK, "docs.html", gin.H{})
}
}
23 changes: 6 additions & 17 deletions repository/db/db_connection.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,30 @@
package sql

import (
"database/sql"
"fmt"
"log"
"os"

// Import pq to register the Postgres driver.
_ "github.com/lib/pq"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)

var DB *sql.DB
var DB *gorm.DB

func NewDBConnection() error {
if os.Getenv("PSQL_ENABLED") != "true" && os.Getenv("APP_ENV") == "development" {
log.Println("Warning: PSQL is not enabled. Database queries will fail.")
return nil
}

connStr := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
dbConnAttrs := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
os.Getenv("POSTGRES_HOST"),
os.Getenv("POSTGRES_PORT"),
os.Getenv("POSTGRES_USER"),
os.Getenv("POSTGRES_PASSWORD"),
os.Getenv("POSTGRES_DB"),
)

db, err := sql.Open("postgres", connStr)
db, err := gorm.Open(postgres.Open(dbConnAttrs), &gorm.Config{})
if err != nil {
return err
}

if err = db.Ping(); err != nil {
return err
}

// assign the *sql.DB instance to DB
// assign the *gorm.DB instance to DB
DB = db
return nil
}
16 changes: 3 additions & 13 deletions repository/db/get_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,10 @@ import (
)

func GetUsers() ([]model.User, error) {
rows, err := DB.Query("SELECT id, name FROM users")
if err != nil {
return nil, err
}
defer rows.Close()

var users []model.User
for rows.Next() {
var user model.User
if err := rows.Scan(&user.UserID, &user.Name); err != nil {
return nil, err
}
users = append(users, user)
result := DB.Find(&users)
if result.Error != nil {
return nil, result.Error
}

return users, nil
}
1 change: 1 addition & 0 deletions tests/statuspage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"
"os"
"testing"

model "web/model"
httpTemplates "web/views/templates"

Expand Down
File renamed without changes
File renamed without changes.
2 changes: 1 addition & 1 deletion views/components/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<ul class="flex">
<li><a href="/" class="text-white">Home</a></li>
<li><a href="/status" class="text-white ml-8">Status</a></li>
<li><a href="/docs" class="text-white ml-8">Documentation</a></li>
<li><a href="https://docs.goblitz.org/" class="text-white ml-8">Documentation</a></li>
</ul>
<a href="https://github.com/KostLinux/Goblitz" class="ml-8 text-white text-3xl">
<i class="fab fa-github"></i>
Expand Down
17 changes: 0 additions & 17 deletions views/docs_page/docs.html

This file was deleted.

1 change: 1 addition & 0 deletions views/templates/statuspage.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"log"
"net/http"
"os"

model "web/model"

"github.com/gin-gonic/gin"
Expand Down