Skip to content
This repository has been archived by the owner on Jan 25, 2025. It is now read-only.

Commit

Permalink
Using caddy
Browse files Browse the repository at this point in the history
  • Loading branch information
tahirmurata committed Sep 20, 2024
1 parent e54da13 commit 3e1fda1
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 52 deletions.
1 change: 0 additions & 1 deletion .env.example

This file was deleted.

7 changes: 7 additions & 0 deletions Caddyfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
auto_https off
}

api.aicj.io {
reverse_proxy :8081
}
17 changes: 10 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
ARG GO_VERSION=1
FROM golang:${GO_VERSION}-alpine as builder

WORKDIR /usr/src/app
COPY go.mod go.sum ./
WORKDIR /usr/src/backend
COPY /backend/go.mod /backend/go.sum ./
RUN go mod download && go mod verify
COPY . .
RUN go build -v -o /app cmd/api/main.go
COPY /backend/ .
RUN go build -v -o /backend cmd/api/main.go

FROM alpine:3
RUN backend

COPY --from=builder /app /usr/local/bin/
FROM caddy:2-alpine

CMD [ "app" ]
COPY Caddyfile /etc/caddy/Caddyfile

RUN caddy run
# COPY --from=builder /backend /usr/share/caddy/
14 changes: 0 additions & 14 deletions compose.yml

This file was deleted.

6 changes: 1 addition & 5 deletions fly.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@
app = 'f3s-api'
primary_region = 'nrt'

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

[env]
PORT = '8080'
PORT = '8081'

[http_service]
internal_port = 8080
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ require (
github.com/labstack/echo/v4 v4.12.0
github.com/sqids/sqids-go v0.4.1
github.com/tursodatabase/libsql-client-go v0.0.0-20240902231107-85af5b9d094d
golang.org/x/time v0.6.0
)

require (
Expand All @@ -30,4 +29,5 @@ require (
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/time v0.6.0 // indirect
)
29 changes: 5 additions & 24 deletions internal/server/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/sqids/sqids-go"
"golang.org/x/time/rate"
)

func Sqids() (*sqids.Sqids, error) {
Expand All @@ -36,8 +35,6 @@ type (
)

func (s *Server) RegisterRoutes() http.Handler {
hosts := map[string]*Host{}

api := echo.New()
api.IPExtractor = func(r *http.Request) string {
return r.Header.Get("Fly-Client-IP")
Expand All @@ -64,15 +61,16 @@ func (s *Server) RegisterRoutes() http.Handler {
api.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowMethods: []string{http.MethodGet, http.MethodPatch, http.MethodPost},
}))
api.Use(middleware.RateLimiter(middleware.NewRateLimiterMemoryStore(rate.Limit(20))))
// api.Use(middleware.RateLimiter(middleware.NewRateLimiterMemoryStore(rate.Limit(20))))
// limiterStore := middleware.NewRateLimiterMemoryStore(rate.Limit(10))

api.GET("/ip", s.PingHandler)

api.GET("/visitor", s.VisitorHandler)
api.GET("/visitor", s.VisitorHandler) // middleware.RateLimiter(limiterStore)

api.POST("/node", s.NodeIpHandler)

api.POST("/vote", s.VoteHandler)
api.POST("/vote", s.VoteHandler) // middleware.RateLimiter(limiterStore)

protected := api.Group("/protected", middleware.KeyAuth(func(key string, c echo.Context) (bool, error) {
node, ok, err := s.DB.Password(key)
Expand Down Expand Up @@ -109,24 +107,7 @@ func (s *Server) RegisterRoutes() http.Handler {

protected.PATCH("/status", s.NodeStatusHandler)

hosts["api.aicj.io"] = &Host{api}

e := echo.New()
e.Any("/*", func(c echo.Context) (err error) {
req := c.Request()
res := c.Response()
host := hosts[req.Host]

if host == nil {
err = echo.ErrNotFound
} else {
host.Echo.ServeHTTP(res, req)
}

return
})

return e
return api
}

func (s *Server) PingHandler(c echo.Context) error {
Expand Down

0 comments on commit 3e1fda1

Please sign in to comment.