Skip to content

Commit

Permalink
feat(infra): deploy (#8)
Browse files Browse the repository at this point in the history
* feat(infra): deploy

* feat(infra): remove unecessary code

* feat(infra): remove comments
  • Loading branch information
vit0rr authored Oct 18, 2024
1 parent 9d23745 commit 7c1de18
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 20 deletions.
27 changes: 27 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# flyctl launch added from .gitignore
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
**/*.exe
**/*.exe~
**/*.dll
**/*.so
**/*.dylib

# Test binary, built with `go test -c`
**/*.test

# Output of the go coverage tool, specifically when used with LiteIDE
**/*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
**/go.work
**/go.work.sum

# env file
**/.env
fly.toml
18 changes: 18 additions & 0 deletions .github/workflows/fly-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# See https://fly.io/docs/app-guides/continuous-deployment-with-github-actions/

name: Fly Deploy
on:
push:
branches:
- main
jobs:
deploy:
name: Deploy app
runs-on: ubuntu-latest
concurrency: deploy-group # optional: ensure only one action runs at a time
steps:
- uses: actions/checkout@v4
- uses: superfly/flyctl-actions/setup-flyctl@master
- run: flyctl deploy --remote-only
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
19 changes: 19 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
ARG GO_VERSION=1
FROM golang:${GO_VERSION}-bookworm as builder

WORKDIR /usr/src/app

COPY go.mod go.sum ./
RUN go mod download && go mod verify

COPY . .

RUN go build -v -o /usr/local/bin/app ./cmd/api

FROM debian:bookworm

RUN apt-get update && apt-get install -y ca-certificates && update-ca-certificates

COPY --from=builder /usr/local/bin/app /usr/local/bin/app

CMD ["/usr/local/bin/app"]
2 changes: 1 addition & 1 deletion api/internal/url-short/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func NewHTTP(deps *deps.Deps, db *mongo.Database) *HTTP {
}

// POST /short-url
func (h *HTTP) ShortUrl(_ http.ResponseWriter, r *http.Request) (interface{}, error) {
func (h *HTTP) ShortUrl(w http.ResponseWriter, r *http.Request) (interface{}, error) {
url, err := h.service.ShortUrl(r.Context(), r.Body, *h.service.db.Client())
if err != nil {
log.Error(r.Context(), "Failed to create short URL", log.ErrAttr(err))
Expand Down
7 changes: 6 additions & 1 deletion api/internal/url-short/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"context"
"encoding/json"
"io"
"os"

"github.com/joho/godotenv"
gonanoid "github.com/matoous/go-nanoid"
"github.com/vit0rr/short-spot/pkg/deps"
"github.com/vit0rr/short-spot/pkg/log"
Expand All @@ -31,6 +33,9 @@ func NewService(deps *deps.Deps, db *mongo.Database) *Service {

// Get short URL
func (s *Service) ShortUrl(c context.Context, b io.ReadCloser, dbclient mongo.Client) (*Response, error) {

godotenv.Load()

var body Body
err := json.NewDecoder(b).Decode(&body)
if err != nil {
Expand All @@ -57,7 +62,7 @@ func (s *Service) ShortUrl(c context.Context, b io.ReadCloser, dbclient mongo.Cl
}, err
}

shortenedUrl := "http://localhost:8080/" + id
shortenedUrl := os.Getenv("BASE_URL") + id

coll := dbclient.Database("shortspot").Collection("shorturls")
_, err = coll.InsertOne(c, Urls{
Expand Down
19 changes: 19 additions & 0 deletions api/router/router.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package router

import (
"net/http"

"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
urlshort "github.com/vit0rr/short-spot/api/internal/url-short"
Expand All @@ -21,6 +23,21 @@ func New(deps *deps.Deps, db mongo.Database) *Router {
}
}

func corsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")

if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusOK)
return
}

next.ServeHTTP(w, r)
})
}

func (router *Router) BuildRoutes() *chi.Mux {
r := chi.NewRouter()
r.Use(middleware.Recoverer)
Expand All @@ -29,6 +46,8 @@ func (router *Router) BuildRoutes() *chi.Mux {
r.Use(SetResponseTypeToJSON)
r.Use(telemetry.TelemetryMiddleware)

r.Use(corsMiddleware)

r.Route("/", func(r chi.Router) {
r.Get("/{id}", telemetry.HandleFuncLogger(router.urlshort.Redirect))
r.Route("/short-url", func(r chi.Router) {
Expand Down
9 changes: 4 additions & 5 deletions config/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@ type Mongo struct {
}

func GetDefaultAPIConfig() API {
err := godotenv.Load()
if err != nil {
panic("Error loading .env file")
}
godotenv.Load()

return API{
Mongo: Mongo{
Dsn: fmt.Sprintf("mongodb://%s:%s@localhost:27017",
Dsn: fmt.Sprintf("mongodb+srv://%s:%s@%s/?retryWrites=true&w=majority&appName=%s",
os.Getenv("MONGODB_USER"),
os.Getenv("MONGODB_PASS"),
os.Getenv("MONGODB_HOST"),
os.Getenv("MONGODB_APPNAME"),
),
},
}
Expand Down
13 changes: 0 additions & 13 deletions config/config_local.hcl

This file was deleted.

27 changes: 27 additions & 0 deletions fly.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# fly.toml app configuration file generated for short-spot on 2024-10-18T07:25:47-03:00
#
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
#

app = 'short-spot'
primary_region = 'gig'

[build]
[build.args]
GO_VERSION = '1.23'

[env]
BASE_URL="https://short-spot.fly.dev/"

[http_service]
internal_port = 8080
force_https = true
auto_stop_machines = 'stop'
auto_start_machines = true
min_machines_running = 0
processes = ['app']

[[vm]]
memory = '1gb'
cpu_kind = 'shared'
cpus = 1

0 comments on commit 7c1de18

Please sign in to comment.