Load env variables
This commit is contained in:
parent
57ff2eb725
commit
d854809313
8 changed files with 78 additions and 37 deletions
4
.env.example
Normal file
4
.env.example
Normal file
|
@ -0,0 +1,4 @@
|
|||
STATIC_HOSTER_HOME=/home/dave/st/
|
||||
STATIC_HOSTER_HOST_DIR=/home/dave/host-this-dir/
|
||||
STATIC_HOSTER_API_KEY=e621
|
||||
STATIC_HOSTER_PORT=4200
|
|
@ -1,29 +1,36 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/c4milo/unpackit"
|
||||
"github.com/gin-gonic/gin"
|
||||
"nikurasu.gay/static-hoster/envloader"
|
||||
)
|
||||
|
||||
func PostUpdate(ctx *gin.Context) {
|
||||
siteUpdate, err := ctx.FormFile("Hello")
|
||||
if err != nil {
|
||||
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
|
||||
"message": "File not recieved",
|
||||
})
|
||||
return
|
||||
func PostUpdate(env *envloader.Environment) gin.HandlerFunc {
|
||||
return func(ctx *gin.Context) {
|
||||
fmt.Println(env.ApiKey)
|
||||
siteUpdate, err := ctx.FormFile("STATIC_PAGE")
|
||||
if err != nil {
|
||||
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
|
||||
"message": "File not recieved",
|
||||
})
|
||||
return
|
||||
}
|
||||
if err := ctx.SaveUploadedFile(siteUpdate, fmt.Sprintf("%s%s", env.RootDir, siteUpdate.Filename)); err != nil {
|
||||
ctx.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
|
||||
"message": "Unable to save file",
|
||||
})
|
||||
return
|
||||
}
|
||||
file, err := os.Open(fmt.Sprintf("%s%s", env.RootDir, siteUpdate.Filename))
|
||||
os.RemoveAll(env.StaticDir)
|
||||
os.Mkdir(env.StaticDir, os.ModePerm)
|
||||
unpackit.Unpack(file, env.StaticDir)
|
||||
os.RemoveAll(fmt.Sprintf("%s%s", env.RootDir, siteUpdate.Filename))
|
||||
ctx.JSON(http.StatusOK, gin.H{"data": siteUpdate.Filename, "err": err})
|
||||
}
|
||||
if err := ctx.SaveUploadedFile(siteUpdate, "./"+siteUpdate.Filename); err != nil {
|
||||
ctx.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
|
||||
"message": "Unable to save file",
|
||||
})
|
||||
return
|
||||
}
|
||||
file, err := os.Open("./" + siteUpdate.Filename)
|
||||
unpackit.Unpack(file, "./")
|
||||
os.RemoveAll("./" + siteUpdate.Filename)
|
||||
ctx.JSON(http.StatusOK, gin.H{"data": siteUpdate.Filename, "err": err})
|
||||
}
|
||||
|
|
|
@ -3,25 +3,39 @@ package envloader
|
|||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
type Environment struct {
|
||||
rootDir string
|
||||
staticDir string
|
||||
apiKey string
|
||||
RootDir string
|
||||
StaticDir string
|
||||
ApiKey string
|
||||
Port string
|
||||
}
|
||||
|
||||
func Load() *Environment {
|
||||
env := new(Environment)
|
||||
env.rootDir = envReader("STATIC_HOSTER_HOME", fmt.Sprintf("%s/static-hoster", os.Getenv("HOME")))
|
||||
if _, err := os.Stat(fmt.Sprintf("%s/config.yml", env.rootDir)); errors.Is(err, os.ErrNotExist) {
|
||||
env.staticDir = envReader("STATIC_HOSTER_HOSTED_DIR", fmt.Sprintf("%s/static-hoster/hosted", os.Getenv("HOME")))
|
||||
env.apiKey = os.Getenv("STATIC_HOSTER_API_KEY")
|
||||
} else if err == nil {
|
||||
//TODO: Read from config.yml
|
||||
func Load() (env *Environment) {
|
||||
err := godotenv.Load(".static-hoster.env")
|
||||
if err != nil {
|
||||
log.Println("Error loading .env file")
|
||||
} else {
|
||||
godotenv.Overload()
|
||||
}
|
||||
return env
|
||||
defaultApiKey := "test123"
|
||||
env = new(Environment)
|
||||
env.RootDir = envReader("STATIC_HOSTER_HOME", fmt.Sprintf("%s/static-hoster/", os.Getenv("HOME")))
|
||||
env.StaticDir = envReader("STATIC_HOSTER_HOST_DIR", fmt.Sprintf("%s/hosted/", env.RootDir))
|
||||
env.Port = fmt.Sprintf(":%s", envReader("STATIC_HOSTER_PORT", "8080"))
|
||||
env.ApiKey = envReader("STATIC_HOSTER_API_KEY", defaultApiKey)
|
||||
if env.ApiKey == defaultApiKey {
|
||||
fmt.Printf("[STATIC-HOSTER-Warning]\t Environment Variable \"STATIC_HOSTER_API_KEY\" not set. Use default key \"%s\". DONT USE THIS FOR PRODUCTION!\n", defaultApiKey)
|
||||
}
|
||||
mkdirIfNotExist(env.RootDir, os.ModePerm)
|
||||
mkdirIfNotExist(env.StaticDir, os.ModePerm)
|
||||
return
|
||||
}
|
||||
|
||||
func envReader(envVar string, defaultVal string) string {
|
||||
|
@ -31,3 +45,13 @@ func envReader(envVar string, defaultVal string) string {
|
|||
return os.Getenv(envVar)
|
||||
}
|
||||
}
|
||||
|
||||
func mkdirIfNotExist(dir string, perm fs.FileMode) {
|
||||
if _, err := os.Stat(dir); errors.Is(err, os.ErrNotExist) {
|
||||
fmt.Printf("[STATIC_HOSTER-Info]\tFolder %s does not exit, trying to create it\n", dir)
|
||||
err := os.Mkdir(dir, perm)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ go 1.19
|
|||
require (
|
||||
github.com/c4milo/unpackit v1.0.0
|
||||
github.com/gin-gonic/gin v1.8.2
|
||||
github.com/joho/godotenv v1.4.0
|
||||
)
|
||||
|
||||
require (
|
||||
|
@ -32,4 +33,5 @@ require (
|
|||
golang.org/x/text v0.6.0 // indirect
|
||||
google.golang.org/protobuf v1.28.1 // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
|
|
@ -28,6 +28,8 @@ github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
|||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/hooklift/assert v0.1.0 h1:UZzFxx5dSb9aBtvMHTtnPuvFnBvcEhHTPb9+0+jpEjs=
|
||||
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
|
||||
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||
github.com/klauspost/compress v1.4.1 h1:8VMb5+0wMgdBykOV96DwNwKFQ+WTI4pzYURP99CcB9E=
|
||||
|
|
|
@ -6,8 +6,7 @@ import (
|
|||
)
|
||||
|
||||
func main() {
|
||||
env := envloader.Load()
|
||||
var env = envloader.Load()
|
||||
r := router.Create(env)
|
||||
// Listen and Server in 0.0.0.0:8080
|
||||
r.Run(":8080")
|
||||
r.Run(env.Port)
|
||||
}
|
||||
|
|
|
@ -4,11 +4,12 @@ import (
|
|||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"nikurasu.gay/static-hoster/envloader"
|
||||
)
|
||||
|
||||
func AuthMiddleware() gin.HandlerFunc {
|
||||
func AuthMiddleware(env *envloader.Environment) gin.HandlerFunc {
|
||||
return func(ctx *gin.Context) {
|
||||
if ctx.Request.Header["Static-Hoster-Key"][0] != "007" {
|
||||
if ctx.Request.Header["Static-Hoster-Key"][0] != env.ApiKey {
|
||||
ctx.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||
return
|
||||
}
|
||||
|
|
|
@ -12,16 +12,18 @@ import (
|
|||
func Create(env *envloader.Environment) *gin.Engine {
|
||||
router := gin.Default()
|
||||
|
||||
apiRoutes := router.Group("/api", auth.AuthMiddleware())
|
||||
apiRoutes := router.Group("/api", auth.AuthMiddleware(env))
|
||||
{
|
||||
apiRoutes.POST("/update", api.PostUpdate)
|
||||
apiRoutes.POST("/update", api.PostUpdate(env))
|
||||
}
|
||||
// Ping test
|
||||
router.GET("/ping", func(c *gin.Context) {
|
||||
c.String(http.StatusOK, "pong")
|
||||
})
|
||||
|
||||
router.Static("/home", "./hostdir")
|
||||
router.Static("/home", env.StaticDir)
|
||||
|
||||
// TODO: Load 404 error Page
|
||||
|
||||
return router
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue