-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from n30w/dev
- Loading branch information
Showing
114 changed files
with
13,814 additions
and
1,412 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,3 +52,7 @@ frontend/next-env.d.ts | |
|
||
# Jetbrains IDE | ||
/.idea | ||
backend/.env | ||
|
||
# testing | ||
backend/resources/test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,73 @@ | ||
{ | ||
"cSpell.words": [ | ||
"cmds", | ||
"coursepage", | ||
"corepack", | ||
"Darkspace", | ||
"healthcheck", | ||
"idnum", | ||
"Lihua", | ||
"netid", | ||
"Taskfile", | ||
"taskfiles", | ||
"Vercel" | ||
] | ||
"[javascript]": { | ||
"editor.defaultFormatter": "esbenp.prettier-vscode", | ||
"editor.formatOnSave": true, | ||
}, | ||
"[javascriptreact]": { | ||
"editor.defaultFormatter": "esbenp.prettier-vscode", | ||
"editor.formatOnSave": true, | ||
}, | ||
"[typescriptreact]": { | ||
"editor.defaultFormatter": "esbenp.prettier-vscode", | ||
"editor.formatOnSave": true, | ||
}, | ||
"[typescript]": { | ||
"editor.defaultFormatter": "esbenp.prettier-vscode", | ||
"editor.formatOnSave": true, | ||
}, | ||
"[css]": { | ||
"editor.defaultFormatter": "vscode.css-language-features", | ||
"editor.formatOnSave": true, | ||
"editor.suggest.insertMode": "replace", | ||
"cSpell.fixSpellingWithRenameProvider": false | ||
}, | ||
"[json]": { | ||
"editor.defaultFormatter": "esbenp.prettier-vscode", | ||
"editor.formatOnSave": true | ||
}, | ||
"[jsonc]": { | ||
"editor.defaultFormatter": "esbenp.prettier-vscode", | ||
"editor.formatOnSave": false, | ||
}, | ||
"go.toolsManagement.autoUpdate": true, | ||
"[go]": { | ||
"editor.insertSpaces": false, | ||
"editor.formatOnSave": true, | ||
"editor.codeActionsOnSave": { | ||
"source.organizeImports": "explicit" | ||
}, | ||
"editor.defaultFormatter": "golang.go" | ||
}, | ||
"[markdown]": { | ||
"editor.unicodeHighlight.ambiguousCharacters": false, | ||
"editor.unicodeHighlight.invisibleCharacters": false, | ||
"editor.wordWrap": "on", | ||
"editor.quickSuggestions": { | ||
"comments": "off", | ||
"strings": "off", | ||
"other": "off" | ||
} | ||
}, | ||
"git.autofetch": true, | ||
"editor.formatOnSave": true, | ||
"cSpell.words": [ | ||
"cmds", | ||
"corepack", | ||
"courseid", | ||
"coursepage", | ||
"Darkspace", | ||
"dbname", | ||
"excelize", | ||
"godotenv", | ||
"healthcheck", | ||
"idnum", | ||
"Lihua", | ||
"netid", | ||
"realip", | ||
"sslmode", | ||
"Taskfile", | ||
"taskfiles", | ||
"userid", | ||
"Vercel" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# ======================================================================= # | ||
# Storage Environment Variables # | ||
# ======================================================================= # | ||
|
||
# LOCAL_STORAGE_DIRECTORY is the directory where the server will store | ||
# local files, such as media, data, or templates. | ||
LOCAL_STORAGE_DIRECTORY= | ||
|
||
# S3_TOKEN and S3_URL are both used to access a remote S3 object storage | ||
# hosted by Amazon. | ||
S3_TOKEN= | ||
S3_URL= | ||
|
||
# EXCEL_TEMPLATE_PATH is the path of the excel template | ||
EXCEL_TEMPLATE_PATH= | ||
|
||
# ======================================================================= # | ||
# Postgresql Environment Variables # | ||
# ======================================================================= # | ||
|
||
# DB_NAME is the name of the database in the postgresql instance. | ||
# This will be used to connect to a database named "development". | ||
# In a production environment, this would be different, according to | ||
# the deployment name, whatever that may be. | ||
DB_NAME=development | ||
|
||
# The postgresql image requires a set username and password to setup. | ||
# Therefore, these are required if running development locally. They can | ||
# be anything one wishes. In production environments though, its necessary | ||
# to use the actual credentials that will be used to connect to the | ||
# database. | ||
DB_USERNAME= | ||
DB_PASSWORD= | ||
|
||
# DB_HOST will usually be "localhost" in a development environment. | ||
# The port may also be arbitrary. The port is the one on the host | ||
# that docker will bind the container's port to. Sometimes, your | ||
# machine might already have an instance of postgresql running without | ||
# docker. Postgresql's default port is 5432. The example here sets the | ||
# DB_PORT to 6543 to avoid this port collision. | ||
DB_HOST=localhost | ||
DB_PORT=6543 | ||
|
||
# This is a field that disables SSL mode to remove the error: | ||
# "SSL is not enabled on this server/database" during development. | ||
# In a production environment, this would be enabled. | ||
DB_SSL_MODE=disable | ||
|
||
# URL Example | ||
DB_DSN="postgres://postgres:password@localhost:6543/development" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"github.com/n30w/Darkspace/internal/dal" | ||
"log" | ||
"time" | ||
|
||
"github.com/n30w/Darkspace/internal/domain" | ||
|
||
// This import fixes the error: "unknown driver "postgres" (forgotten import?)" | ||
_ "github.com/lib/pq" | ||
) | ||
|
||
type config struct { | ||
// Port the server will run on. | ||
port int | ||
|
||
// Runtime environment, either "development", "staging", or "production". | ||
env string | ||
|
||
// Database configurations | ||
db dal.DBConfig | ||
|
||
// limiter is limiter information for rate limiting. | ||
limiter struct { | ||
// rps is requests per second. | ||
rps float64 | ||
burst int | ||
|
||
// enabled either disables or enables rate limiting altogether. | ||
enabled bool | ||
} | ||
} | ||
|
||
// openDB opens a connection to the database using a certain config. | ||
func openDB(cfg config) (*sql.DB, error) { | ||
db, err := sql.Open(cfg.db.Driver, cfg.createDataSourceName()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Passing a value less than or equal to 0 means no limit. | ||
db.SetMaxOpenConns(cfg.db.MaxOpenConns) | ||
|
||
// Passing a value less than or equal to 0 means no limit. | ||
db.SetMaxIdleConns(cfg.db.MaxIdleConns) | ||
|
||
duration, err := time.ParseDuration(cfg.db.MaxIdleTime) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
db.SetConnMaxIdleTime(duration) | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel() | ||
|
||
err = db.PingContext(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return db, nil | ||
} | ||
|
||
// createDataSourceName creates the dataSourceName parameter of the | ||
// sql.Open function. | ||
func (cfg config) createDataSourceName() string { | ||
return cfg.db.CreateDataSourceName() | ||
} | ||
|
||
func (cfg config) SetFromEnv() { | ||
cfg.db.SetFromEnv() | ||
} | ||
|
||
type application struct { | ||
config config | ||
logger *log.Logger | ||
services *domain.Service | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"github.com/n30w/Darkspace/internal/models" | ||
"net/http" | ||
) | ||
|
||
type contextKey string | ||
|
||
const userContextKey = contextKey("user") | ||
|
||
func (app *application) contextSetUser( | ||
r *http.Request, | ||
user *models.User, | ||
) *http.Request { | ||
ctx := context.WithValue(r.Context(), userContextKey, user) | ||
return r.WithContext(ctx) | ||
} | ||
|
||
func (app *application) contextGetUser(r *http.Request) *models.User { | ||
user, ok := r.Context().Value(userContextKey).(*models.User) | ||
if !ok { | ||
panic("missing user value in request context") | ||
} | ||
|
||
return user | ||
} |
Oops, something went wrong.