Added environment vars for easy setup

This commit is contained in:
Phil 2021-12-07 19:23:27 +00:00
parent 133ae32b3b
commit 38226336e0
4 changed files with 75 additions and 6 deletions

View file

@ -1,8 +1,13 @@
version: "3" version: "3"
services: services:
web: web:
# build: . # Only if you want to Build the image on your own Server! # build: . # Only if you want to Build the image on your own Server!
image: registry.ucode.space/phil/goshorly:latest image: registry.ucode.space/phil/goshorly:latest
environment:
- HOST=example.org # Domain or IP-Adress
- HTTPS=true # If you want to use HTTPS
- PROXY=true # If you want to use a Reverse Proxy
- PORT=3000 # The Port you want to use
depends_on: depends_on:
- redis - redis
restart: always restart: always
@ -17,7 +22,7 @@ services:
caddy: caddy:
image: caddy:2 image: caddy:2
restart: always restart: always
command: caddy reverse-proxy --from https://my-domain.com:443 --to http://web:3000 command: caddy reverse-proxy --from https://example.org:443 --to http://web:3000
ports: ports:
- 80:80 - 80:80
- 443:443 - 443:443

View file

@ -1,8 +1,13 @@
version: "3" version: "3"
services: services:
web: web:
# build: . # Only if you want to Build the image on your own Server! # build: . # Only if you want to Build the image on your own Server!
image: registry.ucode.space/phil/goshorly:latest image: registry.ucode.space/phil/goshorly:latest
environment:
- HOST=your_domain_or_ip # Domain or IP-Adress
- HTTPS=false # If you want to use HTTPS
- PROXY=false # If you want to use a Reverse Proxy
- PORT=3000 # The Port you want to use
ports: ports:
- "3000:3000" - "3000:3000"
depends_on: depends_on:

View file

@ -7,6 +7,7 @@ import (
"regexp" "regexp"
"time" "time"
"git.ucode.space/Phil/goshorly/utils"
"github.com/go-redis/redis" "github.com/go-redis/redis"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/limiter" "github.com/gofiber/fiber/v2/middleware/limiter"
@ -19,6 +20,8 @@ var viewsfs embed.FS
func main() { func main() {
utils.Init_ENV()
engine := html.NewFileSystem(http.FS(viewsfs), ".html") engine := html.NewFileSystem(http.FS(viewsfs), ".html")
app := fiber.New(fiber.Config{ app := fiber.New(fiber.Config{
@ -51,7 +54,7 @@ func main() {
val, err := client.Get(c.Params("id")).Result() val, err := client.Get(c.Params("id")).Result()
if err != nil { if err != nil {
return c.Render("views/404", fiber.Map{ return c.Render("views/404", fiber.Map{
"BASEURL": c.Protocol() + "://" + c.Hostname(), "BASEURL": utils.URL,
}) })
} }
return c.Redirect(val) return c.Redirect(val)
@ -97,12 +100,12 @@ func main() {
}) })
} }
fURL := c.Protocol() + "://" + c.Hostname() + "/" + id fURL := utils.URL + id
return c.Render("views/home", fiber.Map{ return c.Render("views/home", fiber.Map{
"URL": fURL, "URL": fURL,
}) })
}) })
log.Fatal(app.Listen(":3000")) log.Fatal(app.Listen(":" + utils.PORT))
} }

56
utils/env.go Normal file
View file

@ -0,0 +1,56 @@
package utils
import (
"fmt"
"log"
"os"
)
var HOST string
var HTTPS string
var PROXY bool
var PORT string
var URL string
func Init_ENV() {
UHOST, err := os.LookupEnv("HOST")
if !err {
log.Fatal("HOST enviroment variable not found, please set it!")
}
HOST = UHOST
UHTTPS, _ := os.LookupEnv("HTTPS")
if UHTTPS != "true" {
HTTPS = "http"
} else {
HTTPS = "https"
}
UPROXY, _ := os.LookupEnv("PROXY")
if UPROXY != "true" {
PROXY = false
} else {
PROXY = true
}
UPORT, err := os.LookupEnv("PORT")
if !err {
PORT = "3000"
} else {
PORT = UPORT
}
create_string()
}
func create_string() {
if !PROXY {
URL = HTTPS + "://" + HOST + ":" + PORT + "/"
} else {
URL = HTTPS + "://" + HOST + "/"
}
fmt.Println(URL)
}