diff --git a/.gitignore b/.gitignore index 022d1e6..9d2be16 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ __debug_bin* templates/*_templ.go +bin/ +config.toml +tmp/ bin/ \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..c0a107c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,16 @@ +FROM golang:alpine + +RUN apk add just + +RUN go install github.com/a-h/templ/cmd/templ@latest && \ + go install github.com/air-verse/air@latest + +COPY . /src + +RUN cd /src && go get -u && just build + +RUN mkdir /app && mv /src/bin/gpx-downloader /app/gpx-downloader + +WORKDIR /app + +ENTRYPOINT [ "/app/gpx-downloader" ] \ No newline at end of file diff --git a/compose.yml b/compose.yml new file mode 100644 index 0000000..0a0a7b6 --- /dev/null +++ b/compose.yml @@ -0,0 +1,8 @@ +services: + gpx-download: + build: . + restart: unless-stopped + ports: + - "127.0.0.1:3000:3000" + volumes: + - ./config.toml:/app/config.toml \ No newline at end of file diff --git a/config.example.toml b/config.example.toml new file mode 100644 index 0000000..61b7461 --- /dev/null +++ b/config.example.toml @@ -0,0 +1,3 @@ +[server] +address = "127.0.0.1" +port = 3000 \ No newline at end of file diff --git a/go.mod b/go.mod index 632e163..39f36eb 100644 --- a/go.mod +++ b/go.mod @@ -24,6 +24,7 @@ require ( github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-runewidth v0.0.19 // indirect github.com/natefinch/atomic v1.0.1 // indirect + github.com/pelletier/go-toml v1.9.5 // indirect github.com/rivo/uniseg v0.4.7 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasthttp v1.69.0 // indirect diff --git a/go.sum b/go.sum index 0d01cb0..e41cdec 100644 --- a/go.sum +++ b/go.sum @@ -33,6 +33,8 @@ github.com/mattn/go-runewidth v0.0.19 h1:v++JhqYnZuu5jSKrk9RbgF5v4CGUjqRfBm05byF github.com/mattn/go-runewidth v0.0.19/go.mod h1:XBkDxAl56ILZc9knddidhrOlY5R/pDhgLpndooCuJAs= github.com/natefinch/atomic v1.0.1 h1:ZPYKxkqQOx3KZ+RsbnP/YsgvxWQPGxjC0oBt2AhwV0A= github.com/natefinch/atomic v1.0.1/go.mod h1:N/D/ELrljoqDyT3rZrsUmtsuzvHkeB/wWjHV22AZRbM= +github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8= +github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= diff --git a/helpers/config.go b/helpers/config.go new file mode 100644 index 0000000..0910534 --- /dev/null +++ b/helpers/config.go @@ -0,0 +1,29 @@ +package helpers + +import ( + "fmt" + + "github.com/pelletier/go-toml" +) + +type Configuration struct { + Server struct { + Address string `toml:"address"` + Port int `toml:"port"` + } `toml:"server"` +} + +func ParseConfig(filePath string) (*Configuration, error) { + var config Configuration + data, err := toml.LoadFile(filePath) + if err != nil { + return nil, fmt.Errorf("error parsing config file: %v", err) + } + + err = data.Unmarshal(&config) + if err != nil { + return nil, fmt.Errorf("error parsing config file: %v", err) + } + + return &config, nil +} diff --git a/justfile b/justfile index 8fad0a8..9150926 100644 --- a/justfile +++ b/justfile @@ -11,12 +11,13 @@ run: prebuild go run main.go build: prebuild - go build -o bin/gpx-downloader main.go + mkdir bin + go build -o ./bin/gpx-downloader main.go watch: CI=1 CLICOLOR_FORCE=1 air \ --build.cmd "just build" \ --build.bin "./bin/gpx-downloader" \ -build.include_ext "go,templ" \ - --build.exclude_dir "data" \ + --build.exclude_dir "data,bin" \ --build.exclude_regex "_templ.go" diff --git a/main.go b/main.go index 57cadec..62ee0bb 100644 --- a/main.go +++ b/main.go @@ -1,8 +1,10 @@ package main import ( + "fmt" "log" + "git.readonly.ch/bouzoure/gpx-downloader/helpers" "git.readonly.ch/bouzoure/gpx-downloader/routes" "github.com/gofiber/fiber/v2" "github.com/gofiber/helmet/v2" @@ -22,7 +24,15 @@ func main() { app.Get("/", routes.Index) app.Post("/fetch", routes.Fetch) - err := app.Listen("127.0.0.1:3000") + config, err := helpers.ParseConfig("config.toml") + if err != nil { + log.Fatal(err) + } + + err = app.Listen(fmt.Sprintf( + "%s:%d", + config.Server.Address, config.Server.Port, + )) if err != nil { log.Fatal(err) } diff --git a/routes/fetch.go b/routes/fetch.go index dedc5a3..b5229c1 100644 --- a/routes/fetch.go +++ b/routes/fetch.go @@ -16,12 +16,12 @@ func Fetch(c *fiber.Ctx) error { baseParams := templates.BaseTemplateParams{ Title: "Error - GPX downloader", } - var indexParams templates.IndexParams + var errorParams templates.ErrorParams sourceUrl := c.FormValue("url") if len(sourceUrl) <= 0 { - indexParams.Error = "Error: URL is empty" - return helpers.TemplRender(c, templates.Index(baseParams, indexParams)) + errorParams.Error = "Error: URL is empty" + return helpers.TemplRender(c, templates.Error(baseParams, errorParams)) } var gpxFile gpx.GPX @@ -39,21 +39,21 @@ func Fetch(c *fiber.Ctx) error { gpxFile, err = providers.SuisseMobileFetch(suisseMobileId) if err != nil { log.Error(err) - indexParams.Error = "An error occured" - return helpers.TemplRender(c, templates.Index(baseParams, indexParams)) + errorParams.Error = "An error occured" + return helpers.TemplRender(c, templates.Error(baseParams, errorParams)) } } if len(filename) <= 0 { - indexParams.Error = "Error: URL is not supported" - return helpers.TemplRender(c, templates.Index(baseParams, indexParams)) + errorParams.Error = "Error: URL is not supported" + return helpers.TemplRender(c, templates.Error(baseParams, errorParams)) } xml, err := gpxFile.ToXml(gpx.ToXmlParams{}) if err != nil { log.Error(err) - indexParams.Error = "An error occured" - return helpers.TemplRender(c, templates.Index(baseParams, indexParams)) + errorParams.Error = "An error occured" + return helpers.TemplRender(c, templates.Error(baseParams, errorParams)) } c.Set("Content-Type", "octet-stream") diff --git a/routes/index.go b/routes/index.go index 08fab1e..0121618 100644 --- a/routes/index.go +++ b/routes/index.go @@ -10,7 +10,6 @@ func Index(c *fiber.Ctx) error { baseParams := templates.BaseTemplateParams{ Title: "GPX downloader", } - var indexParams templates.IndexParams - return helpers.TemplRender(c, templates.Index(baseParams, indexParams)) + return helpers.TemplRender(c, templates.Index(baseParams)) } diff --git a/templates/errors.templ b/templates/errors.templ new file mode 100644 index 0000000..f4702c4 --- /dev/null +++ b/templates/errors.templ @@ -0,0 +1,19 @@ +package templates + +type ErrorParams struct { + Error string +} + +templ Error(baseParams BaseTemplateParams, errorParams ErrorParams) { + @BaseTemplate(baseParams) { +
+

GPX downloader

+ +
+ { errorParams.Error } +
+ + Retour +
+ } +} \ No newline at end of file diff --git a/templates/index.templ b/templates/index.templ index 2fae97a..1fd4e0d 100644 --- a/templates/index.templ +++ b/templates/index.templ @@ -1,26 +1,14 @@ package templates -type IndexParams struct { - Error string -} - -templ Index(baseParams BaseTemplateParams, indexParams IndexParams) { +templ Index(baseParams BaseTemplateParams) { @BaseTemplate(baseParams) {

GPX downloader

- if len(indexParams.Error) <= 0 { -
- - -
- } else { -
- { indexParams.Error } -
- - Retour - } +
+ + +
} } \ No newline at end of file