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

docs: add code documentation #54

Merged
merged 6 commits into from
Dec 22, 2023
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
2 changes: 2 additions & 0 deletions database/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package database

import "github.com/kevinanielsen/go-fast-cdn/models"

// Migrate runs database migrations for all model structs using
// the global DB instance. This would typically be called on app startup.
func Migrate() {
DB.AutoMigrate(&models.Image{}, &models.Doc{})
}
3 changes: 3 additions & 0 deletions initializers/loadEnvVariables.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"github.com/joho/godotenv"
)

// LoadEnvVariables loads environment variables from .env file or sets
// hardcoded values based on prod boolean. In prod it sets PORT and DB_SECRET
// to hardcoded values. In dev it loads .env file from current directory.
func LoadEnvVariables(prod bool) {
if prod {
os.Setenv("PORT", "8080")
Expand Down
2 changes: 2 additions & 0 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/kevinanielsen/go-fast-cdn/ui"
)

// Router initializes the router and sets up middleware, routes, etc.
// It returns a *gin.Engine instance configured with the routes, middleware, etc.
func Router() {
r := gin.Default()

Expand Down
5 changes: 4 additions & 1 deletion ui/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import (
//go:embed build
var staticFS embed.FS

// AddRoutes serves the static file system for the UI React App.
// AddRoutes configures the router with middleware to serve the embedded
// React frontend. It serves the frontend as static files from the
// embedded build folder. It also configures a fallback filesystem to
// always serve index.html for unknown routes.
func AddRoutes(router gin.IRouter) {
embeddedBuildFolder := newStaticFileSystem()
fallbackFileSystem := newFallbackFileSystem(embeddedBuildFolder)
Expand Down
9 changes: 9 additions & 0 deletions util/filterFilename.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
// Package util contains utility functions used throughout the application.
package util

import (
"errors"
"strings"
)

// countVal counts the number of occurrences of val in str.
// It splits str into a slice of strings, iterates over the slice
// checking each string for equality with val, and increments a counter
// each time a match is found. The final count is returned.
func countVal(str string, val string) int {
var count int
arr := strings.Split(str, "")
Expand All @@ -17,6 +22,10 @@ func countVal(str string, val string) int {
return count
}

// FilterFilename removes illegal characters from a filename string.
// It ensures there is at most one period in the filename,
// replaces any '/' and '\' characters,
// and returns the filtered string.
func FilterFilename(filename string) (string, error) {
if countVal(filename, ".") > 1 {
return filename, errors.New("filename cannot contain more than one period character")
Expand Down
4 changes: 4 additions & 0 deletions util/getPath.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import (

var ExPath string

// LoadExPath loads the executable path and stores it in the ExPath variable.
// It uses os.Executable to get the path of the current executable.
// The filepath.Dir function is used to get the directory containing the executable.
// If there is an error getting the executable path, it will panic.
func LoadExPath() {
ex, err := os.Executable()
if err != nil {
Expand Down
Loading