diff --git a/README.md b/README.md index 7e3a8cc..1b3db76 100644 --- a/README.md +++ b/README.md @@ -14,3 +14,7 @@ This repository contains examples of how to deploy applications using Dokploy. - [x] Svelte - [x] Vite - [x] VueJS +- [x] AstroJS +- [x] Go Fiber + +--- diff --git a/go-fiber-simple/LICENSE b/go-fiber-simple/LICENSE new file mode 100644 index 0000000..d2683e6 --- /dev/null +++ b/go-fiber-simple/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 Hayden L + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/go-fiber-simple/README.md b/go-fiber-simple/README.md new file mode 100644 index 0000000..efd4c9d --- /dev/null +++ b/go-fiber-simple/README.md @@ -0,0 +1,20 @@ +# Go Fiber Example + +This repository contains an example of Go fiber application that is deployed on Dokploy. + + +1. **Use Git Provider in Your Application**: + - Repository: `https://github.com/Dokploy/examples.git` + - Branch: `main` + - Build path: `/go-fiber-simple` + +4. **Click on Deploy**: + - Deploy your application by clicking the deploy button. + +5. **Generate a Domain**: + - Click on generate domain button. + - A new domain will be generated for you. + - You can use this domain to access your application. + + +If you need further assistance, join our [Discord server](https://discord.com/invite/2tBnJ3jDJc). diff --git a/go-fiber-simple/cmd/api/healthcheck.go b/go-fiber-simple/cmd/api/healthcheck.go new file mode 100644 index 0000000..e72dddb --- /dev/null +++ b/go-fiber-simple/cmd/api/healthcheck.go @@ -0,0 +1,19 @@ +package main + +import ( + "net/http" +) + +func (app *application) healthcheckHandler(w http.ResponseWriter, r *http.Request) { + data := map[string]string{ + "status": "ok", + "version": version, + } + + // No need to add acess control origin headers. On other routes, that may be necessary + err := app.writeJSON(w, http.StatusOK, data, nil) + if err != nil { + app.logger.Error(err.Error()) + http.Error(w, "server error", http.StatusInternalServerError) + } +} diff --git a/go-fiber-simple/cmd/api/helpers.go b/go-fiber-simple/cmd/api/helpers.go new file mode 100644 index 0000000..f8d65fe --- /dev/null +++ b/go-fiber-simple/cmd/api/helpers.go @@ -0,0 +1,28 @@ +package main + +import ( + "encoding/json" + "net/http" +) + +// The writeJSON() method is a generic helper for writing JSON to a response +func (app *application) writeJSON(w http.ResponseWriter, sCode int, data any, headers http.Header) error { + marshalledJson, err := json.Marshal(data) + + if err != nil { + return err + } + + // Valid json requires newline + marshalledJson = append(marshalledJson, '\n') + + for key, value := range headers { + w.Header()[key] = value + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(sCode) + w.Write(marshalledJson) + + return nil +} diff --git a/go-fiber-simple/cmd/api/main.go b/go-fiber-simple/cmd/api/main.go new file mode 100644 index 0000000..6b347a5 --- /dev/null +++ b/go-fiber-simple/cmd/api/main.go @@ -0,0 +1,63 @@ +package main + +import ( + "fmt" + "log/slog" + "net/http" + "os" + "strconv" + "time" +) + +const version = "0.0.1" + +type config struct { + port int +} + +type application struct { + config config + logger *slog.Logger +} + +func main() { + + var cfg config + + // Try to read environment variable for port (given by railway). Otherwise use default + port := os.Getenv("PORT") + intPort, err := strconv.Atoi(port) + if err != nil { + intPort = 4000 + } + + // Set the port to run the API on + cfg.port = intPort + + // create the logger + logger := slog.New(slog.NewTextHandler(os.Stdout, nil)) + + // create the application + app := &application{ + config: cfg, + logger: logger, + } + + // create the server + srv := &http.Server{ + Addr: fmt.Sprintf(":%d", cfg.port), + Handler: app.routes(), + IdleTimeout: 45 * time.Second, + ReadTimeout: 5 * time.Second, + WriteTimeout: 10 * time.Second, + ErrorLog: slog.NewLogLogger(logger.Handler(), slog.LevelError), + } + + logger.Info("server started", "addr", srv.Addr) + + // Start the server + err = srv.ListenAndServe() + logger.Error(err.Error()) + os.Exit(1) + +} diff --git a/go-fiber-simple/cmd/api/routes.go b/go-fiber-simple/cmd/api/routes.go new file mode 100644 index 0000000..cc5186c --- /dev/null +++ b/go-fiber-simple/cmd/api/routes.go @@ -0,0 +1,16 @@ +package main + +import ( + "net/http" + + "github.com/julienschmidt/httprouter" +) + +func (app *application) routes() *httprouter.Router { + router := httprouter.New() + + // Define the available routes + router.HandlerFunc(http.MethodGet, "/v1/healthcheck", app.healthcheckHandler) + + return router +} diff --git a/go-fiber-simple/go.mod b/go-fiber-simple/go.mod new file mode 100644 index 0000000..4c542a3 --- /dev/null +++ b/go-fiber-simple/go.mod @@ -0,0 +1,5 @@ +module goapi.railway.app + +go 1.21.7 + +require github.com/julienschmidt/httprouter v1.3.0 diff --git a/go-fiber-simple/go.sum b/go-fiber-simple/go.sum new file mode 100644 index 0000000..096c54e --- /dev/null +++ b/go-fiber-simple/go.sum @@ -0,0 +1,2 @@ +github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U= +github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= diff --git a/go-fiber-simple/nixpacks.toml b/go-fiber-simple/nixpacks.toml new file mode 100644 index 0000000..b6b5d48 --- /dev/null +++ b/go-fiber-simple/nixpacks.toml @@ -0,0 +1,5 @@ +[phases.build] +cmds = ['''GOOS=linux GOARCH=amd64 go build -ldflags='-s' -o=./bin/linux_amd64/api ./cmd/api'''] + +[start] +cmd = './bin/linux_amd64/api' \ No newline at end of file