-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
79 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,39 @@ | ||
# vercel-chi | ||
# vercel-go-chi | ||
|
||
Demo application using vercel serverless functions to create a simple API with go using chi router. | ||
This application also uses a PostgreSQL database served by vercel. | ||
|
||
Technologies used: | ||
## Technologies used | ||
|
||
- [Go](https://go.dev/) - Programming language | ||
- [Chi](https://go-chi.io/) - Router | ||
- [Vercel](https://vercel.com/) - Serverless functions | ||
- [PostgreSQL](https://www.postgresql.org/) - Database | ||
- [Dagger](https://dagger.io/) - CI | ||
- [Github Actions](https://github.com/features/actions) - CI Executor | ||
|
||
## Using this template | ||
|
||
To use this template, you can click on the "Use this template" button or clone it using git: | ||
|
||
```bash | ||
git clone https://github.com/ngoldack/vercel-go-chi.git | ||
``` | ||
|
||
## Pre-requisites | ||
|
||
To run this application locally, you need to have [Go](https://golang.org/) installed. | ||
|
||
If you want to run the CI, you also need [Docker](https://www.docker.com/) and [Dagger](https://dagger.io) installed. | ||
|
||
## Running locally | ||
|
||
### CI (build, lint, test) | ||
|
||
```bash | ||
dagger run go run ci/main.go | ||
``` | ||
|
||
### Dev server | ||
|
||
```bash | ||
go run ./cmd/server/main.go | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package handler_test | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/ngoldack/vercel-chi/api/handler" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestIndexHandle(t *testing.T) { | ||
req := httptest.NewRequest("GET", "/", nil) | ||
w := httptest.NewRecorder() | ||
|
||
rootHandler := handler.NewRootHandler() | ||
rootHandler.IndexHandler().ServeHTTP(w, req) | ||
|
||
result := w.Result() | ||
require.Equal(t, 200, result.StatusCode, "should return 200 OK") | ||
require.Equal(t, "application/json", result.Header.Get("Content-Type"), "should return JSON") | ||
|
||
m := make(map[string]string) | ||
err := json.NewDecoder(w.Result().Body).Decode(&m) | ||
require.NoError(t, err, "should decode JSON") | ||
|
||
require.True(t, len(m["message"]) > 0, "should return a message") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,7 @@ | ||
package handler | ||
|
||
import "github.com/jmoiron/sqlx" | ||
type RootHandler struct{} | ||
|
||
type RootHandler struct { | ||
db *sqlx.DB | ||
} | ||
|
||
func NewRootHandler(db *sqlx.DB) *RootHandler { | ||
return &RootHandler{db: db} | ||
func NewRootHandler() *RootHandler { | ||
return &RootHandler{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,7 @@ | ||
package app | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"os" | ||
"time" | ||
|
||
"github.com/jmoiron/sqlx" | ||
"github.com/joho/godotenv" | ||
|
||
"github.com/jackc/pgx/v5" | ||
"github.com/jackc/pgx/v5/stdlib" | ||
|
||
_ "github.com/lib/pq" | ||
) | ||
|
||
type App struct { | ||
db *sqlx.DB | ||
} | ||
type App struct{} | ||
|
||
func NewApp() *App { | ||
err := godotenv.Load() | ||
if err != nil { | ||
log.Fatal("Error loading .env file") | ||
} | ||
|
||
db, err := getDB(os.Getenv("DATABASE_URL")) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
return &App{ | ||
db: db, | ||
} | ||
} | ||
|
||
func getDB(uri string) (*sqlx.DB, error) { | ||
// before : directly using sqlx | ||
// DB, err = sqlx.Connect("postgres", uri) | ||
// after : using pgx to setup connection | ||
DB, err := createPGX(uri) | ||
if err != nil { | ||
return nil, err | ||
} | ||
DB.SetMaxIdleConns(2) | ||
DB.SetMaxOpenConns(4) | ||
DB.SetConnMaxLifetime(time.Duration(30) * time.Minute) | ||
|
||
return DB, nil | ||
} | ||
|
||
func createPGX(uri string) (*sqlx.DB, error) { | ||
connConfig, _ := pgx.ParseConfig(uri) | ||
afterConnect := stdlib.OptionAfterConnect(func(ctx context.Context, conn *pgx.Conn) error { | ||
_, err := conn.Exec(ctx, `SELECT 1`) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
}) | ||
|
||
pgxdb := stdlib.OpenDB(*connConfig, afterConnect) | ||
return sqlx.NewDb(pgxdb, "pgx"), nil | ||
return &App{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters