Skip to content

Commit

Permalink
Merge pull request #97 from mbaraa/refactor/go-env-variable
Browse files Browse the repository at this point in the history
Refactor: Add `GO_ENV` Environment Variable
  • Loading branch information
mbaraa authored Jul 6, 2024
2 parents ac70862 + e253fb2 commit 8a70faa
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 26 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
PORT=3000
GO_ENV="dev" # "beta" "prod"
HOSTNAME="http://localhost:3000"
JWT_SECRET="spoikoninochi"

Expand Down
20 changes: 0 additions & 20 deletions app/Dockerfile.beta

This file was deleted.

7 changes: 2 additions & 5 deletions app/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,15 @@ init:
go generate && \
go run main.go migrate

seed:
go run main.go seed

dev:
go run github.com/cosmtrek/[email protected]

beta:
./${BINARY_NAME} serve
GO_ENV="beta" ./${BINARY_NAME} serve

prod:
./${BINARY_NAME} migrate
./${BINARY_NAME} serve
GO_ENV="prod" ./${BINARY_NAME} serve

clean:
go clean
Expand Down
4 changes: 4 additions & 0 deletions app/cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"dankmuzikk/handlers/middlewares/auth"
"dankmuzikk/handlers/middlewares/contenttype"
"dankmuzikk/handlers/middlewares/ismobile"
"dankmuzikk/handlers/middlewares/logger"
"dankmuzikk/handlers/middlewares/theme"
"dankmuzikk/handlers/pages"
"dankmuzikk/log"
Expand Down Expand Up @@ -125,5 +126,8 @@ func StartServer(staticFS embed.FS) error {
applicationHandler.Handle("/api/", ismobile.Handler(theme.Handler(http.StripPrefix("/api", apisHandler))))

log.Info("Starting http server at port " + config.Env().Port)
if config.Env().GoEnv == "dev" || config.Env().GoEnv == "beta" {
return http.ListenAndServe(":"+config.Env().Port, logger.Handler(ismobile.Handler(theme.Handler(m.Middleware(applicationHandler)))))
}
return http.ListenAndServe(":"+config.Env().Port, ismobile.Handler(theme.Handler(m.Middleware(applicationHandler))))
}
2 changes: 2 additions & 0 deletions app/config/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var (
func initEnvVars() {
_config = config{
Port: getEnv("PORT"),
GoEnv: getEnv("GO_ENV"),
Hostname: getEnv("HOSTNAME"),
JwtSecret: getEnv("JWT_SECRET"),
YouTube: struct {
Expand Down Expand Up @@ -57,6 +58,7 @@ func initEnvVars() {

type config struct {
Port string
GoEnv string
Hostname string
JwtSecret string
YouTube struct {
Expand Down
18 changes: 18 additions & 0 deletions app/handlers/middlewares/logger/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package logger

import (
"dankmuzikk/log"
"fmt"
"net/http"
)

func Handler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Infoln(r.Method, r.URL.Path, r.URL.Query())
for key, val := range r.Header {
log.Infoln(key, val)
}
fmt.Println()
h.ServeHTTP(w, r)
})
}
2 changes: 1 addition & 1 deletion docker-compose-beta.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ services:
app-beta:
container_name: "dankmuzikk-beta"
build:
dockerfile: ./Dockerfile.beta
dockerfile: ./Dockerfile
context: ./app
image: "dankmuzikk-app-beta"
restart: "always"
Expand Down

0 comments on commit 8a70faa

Please sign in to comment.