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

Commit

Permalink
Reverse proxy built in
Browse files Browse the repository at this point in the history
  • Loading branch information
tahirmurata committed Sep 22, 2024
1 parent b144fc1 commit 32157af
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 27 deletions.
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ RUN go build -v -o /app cmd/api/main.go

FROM alpine:3

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

CMD [ "app" ]
6 changes: 4 additions & 2 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ package main

import (
"backend/internal/server"
"fmt"
"log/slog"
"os"
)

func main() {
newServer := server.NewServer()

err := newServer.ListenAndServe()
if err != nil {
panic(fmt.Sprintf("cannot start newServer: %s", err))
slog.Default().Error("cannot start newServer", "erro", err)
os.Exit(1)
}
}
11 changes: 6 additions & 5 deletions fly.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
# fly.toml app configuration file generated for f3s-api on 2024-09-20T16:34:56+09:00
# fly.toml app configuration file generated for f3s-web on 2024-09-22T19:38:19+09:00
#
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
#

app = 'f3s-api'
app = 'f3s-web'
primary_region = 'nrt'

[build]

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

[http_service]
internal_port = 0
force_https = true
internal_port = 8080
auto_stop_machines = 'stop'
auto_start_machines = true
min_machines_running = 0
Expand Down
62 changes: 61 additions & 1 deletion internal/server/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type (
}
)

func (s *Server) RegisterRoutes() http.Handler {
func (s *Server) ApiRoutes() *echo.Echo {
api := echo.New()
api.IPExtractor = func(r *http.Request) string {
return r.Header.Get("Fly-Client-IP")
Expand All @@ -59,8 +59,10 @@ func (s *Server) RegisterRoutes() http.Handler {
}))
api.Use(middleware.Recover())
api.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"https://aicj.io"},
AllowMethods: []string{http.MethodGet, http.MethodPatch, http.MethodPost},
}))

// api.Use(middleware.RateLimiter(middleware.NewRateLimiterMemoryStore(rate.Limit(20))))
// limiterStore := middleware.NewRateLimiterMemoryStore(rate.Limit(10))

Expand Down Expand Up @@ -110,6 +112,64 @@ func (s *Server) RegisterRoutes() http.Handler {
return api
}

func (s *Server) RegisterRoutes() *echo.Echo { // Hosts
hosts := map[string]*Host{}

//-----
// API
//-----

api := s.ApiRoutes()

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

//------
// Public Website
//------

public := echo.New()
public.Use(middleware.Logger())
public.Use(middleware.Recover())

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

public.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, c.Request().Header.Get("Fly-Client-IP"))
})

//---------
// Node Website
//---------

node := echo.New()
node.Use(middleware.Logger())
node.Use(middleware.Recover())

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

node.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "node")
})

// Server
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
}

func (s *Server) PingHandler(c echo.Context) error {
ip := c.RealIP()
addr, err := netip.ParseAddr(ip)
Expand Down
21 changes: 7 additions & 14 deletions internal/server/server.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,29 @@
package server

import (
"fmt"
"net/http"
"os"
"strconv"
"time"

_ "github.com/joho/godotenv/autoload"

"backend/internal/database"
)

type Server struct {
Port int
DB database.Service
DB database.Service
}

func NewServer() *http.Server {
port, _ := strconv.Atoi(os.Getenv("PORT"))
NewServer := &Server{
Port: port,
DB: database.New(),
DB: database.New(),
}

// Declare Server config
server := &http.Server{
Addr: fmt.Sprintf(":%d", NewServer.Port),
Handler: NewServer.RegisterRoutes(),
IdleTimeout: time.Minute,
ReadTimeout: 10 * time.Second,
WriteTimeout: 30 * time.Second,
Addr: "0.0.0.0:8080",
Handler: NewServer.RegisterRoutes(),
// IdleTimeout: time.Minute,
// ReadTimeout: 10 * time.Second,
// WriteTimeout: 30 * time.Second,
}

return server
Expand Down
6 changes: 2 additions & 4 deletions sql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,16 @@ CREATE TABLE students (
visitor_id BIGINT UNIQUE,
grade INTEGER NOT NULL,
class INTEGER NOT NULL,
student_id INTEGER NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (visitor_id) REFERENCES visitors(id)
);
-- Talent table
-- Model table
CREATE TABLE models (
id BIGSERIAL PRIMARY KEY,
name TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (visitor_id) REFERENCES visitors(id)
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TYPE entry_logs_type AS ENUM ('ENTERED', 'LEFT');
-- EntryLog table
Expand Down

0 comments on commit 32157af

Please sign in to comment.