Skip to content

Commit

Permalink
Merge pull request #196 from google/gorrila/sample
Browse files Browse the repository at this point in the history
working sample application
  • Loading branch information
kapv89 authored Nov 23, 2022
2 parents 200f70c + 9a440a1 commit 9ae8338
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 71 deletions.
25 changes: 8 additions & 17 deletions go/samples/http/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
# Build Stage
# First pull Golang image
FROM golang:1.19-alpine as build-env
FROM golang:1.19-alpine

ENV APP_NAME sqlcommenter-http
ENV CMD_PATH main.go

# Copy application data into image
COPY . $GOPATH/src/$APP_NAME

# Set workdir
WORKDIR $GOPATH/src/$APP_NAME

# Copy application data into image
COPY . .

# Budild application
RUN CGO_ENABLED=0 go build -v -o /$APP_NAME $GOPATH/src/$APP_NAME/$CMD_PATH

# Run Stage
FROM alpine:3.16.3

# Set environment variable
ENV APP_NAME sqlcommenter-http

# Copy only required data into this image
COPY --from=build-env /$APP_NAME .
RUN CGO_ENABLED=0 go build -v

# Expose application port
EXPOSE 8081

# Start app
CMD ["./sqlcommenter-http", "--db_engine=mysql"]
CMD ["./sqlcommenter-http", "--db_engine=pg"]
4 changes: 0 additions & 4 deletions go/samples/http/curls/index.sh

This file was deleted.

9 changes: 5 additions & 4 deletions go/samples/http/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@ require (
)

require (
github.com/julienschmidt/httprouter v1.3.0
github.com/lib/pq v1.10.7
go.opentelemetry.io/otel v1.11.1
)

require github.com/google/sqlcommenter/go/gorrila/mux v0.0.1-beta // indirect

require (
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/google/sqlcommenter/go/core v0.0.1-beta
github.com/google/sqlcommenter/go/database/sql v0.0.1-beta
github.com/google/sqlcommenter/go/net/http v0.0.1-beta
github.com/google/sqlcommenter/go/core v0.0.5-beta
github.com/google/sqlcommenter/go/database/sql v0.0.3-beta
github.com/google/sqlcommenter/go/net/http v0.0.3-beta // indirect
go.opentelemetry.io/otel/trace v1.11.1 // indirect
golang.org/x/sys v0.0.0-20220927170352-d9d178bc13c6 // indirect
)
37 changes: 17 additions & 20 deletions go/samples/http/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/felixge/httpsnoop v1.0.3 h1:s/nj+GCswXYzN5v2DpNMuMQYe+0DDwt5WVCU6CWBdXk=
github.com/felixge/httpsnoop v1.0.3/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
Expand All @@ -9,40 +9,37 @@ github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/sqlcommenter/go/core v0.0.1-beta h1:IVszEHanWVeS7UcmP8C3SHa57CmfeqMBj0QUcJ8VZ9Q=
github.com/google/sqlcommenter/go/core v0.0.1-beta/go.mod h1:CZfcqmbIxngExnZ7Se6AsKNVubZhKyi54aeDJZiqTMQ=
github.com/google/sqlcommenter/go/database/sql v0.0.1-beta h1:N680pEYaRwmOSrQWUd4A4aD4kj4WYxcJTQB9WLe69vY=
github.com/google/sqlcommenter/go/database/sql v0.0.1-beta/go.mod h1:VdswmF4SM0cbjJdD+3GyM5QuXpHhH6F5nSzcbikzCGY=
github.com/google/sqlcommenter/go/net/http v0.0.1-beta h1:7XQ6poZv+ZJwwHWQHlesq9IMsRus3G6Z9n10qAkrGqE=
github.com/google/sqlcommenter/go/net/http v0.0.1-beta/go.mod h1:tVUqM1YZ/K3eRTdGzeav1GSbw+BXNdTGzSAbLW9CxAc=
github.com/google/sqlcommenter/go/core v0.0.2-beta h1:VnX58Jvf1mkI5KveBddZhCm4YtzG9IQErCNdmfXBU1I=
github.com/google/sqlcommenter/go/core v0.0.2-beta/go.mod h1:CZfcqmbIxngExnZ7Se6AsKNVubZhKyi54aeDJZiqTMQ=
github.com/google/sqlcommenter/go/core v0.0.3-beta h1:gWUfq/UyMPEmHpAyhjcsGGAXRCV2fjB6e6F8CPedlnU=
github.com/google/sqlcommenter/go/core v0.0.3-beta/go.mod h1:GORu2htXRC4xtejBzOa4ct1L20pohP81DFNYKdCJI70=
github.com/google/sqlcommenter/go/core v0.0.5-beta h1:axqYR1zQCCdRBLnwr/j+ckllBSBJ7uaVdsnANuGzCUI=
github.com/google/sqlcommenter/go/core v0.0.5-beta/go.mod h1:GORu2htXRC4xtejBzOa4ct1L20pohP81DFNYKdCJI70=
github.com/google/sqlcommenter/go/database/sql v0.0.2-beta h1:0Caf+turrQA18W35lP3BwYpaV15vty+7eHAWtE73mQs=
github.com/google/sqlcommenter/go/database/sql v0.0.2-beta/go.mod h1:d4SXOYrVnfW0X38FR+MUa1pbYNgqHlPv2rgYK0xr+rg=
github.com/google/sqlcommenter/go/database/sql v0.0.3-beta h1:1f989k6obpu99gl/wX7cS6zHQwa77p5NrQABCg0X80Y=
github.com/google/sqlcommenter/go/database/sql v0.0.3-beta/go.mod h1:Bmr95q93SPegEZ04UpJTVSr+okVqZTR3tM4Tom4f0b4=
github.com/google/sqlcommenter/go/gorrila/mux v0.0.1-beta h1:kCtKVtbDtv42Vh7jvwaHC7MCuVOFjKFUMcdjN2gvXjo=
github.com/google/sqlcommenter/go/gorrila/mux v0.0.1-beta/go.mod h1:xhQX/UtJVH6+dzvU+ULSVyepXvnqMqBPZay4l8m1WfM=
github.com/google/sqlcommenter/go/net/http v0.0.3-beta h1:IE/vO3xKddn/2Bq3k+hSy4CxcEuvE1lUiIDYTXjApzA=
github.com/google/sqlcommenter/go/net/http v0.0.3-beta/go.mod h1:duXQQvXZYCX8eQ+XOrlojWF512ltEp1eSKXc/KiS9lg=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw=
github.com/lib/pq v1.10.7/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux v0.36.4 h1:gn5Cf5XpnENThRBjAHq6vuENFo+l9qwnEMqwmanIYuY=
go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux v0.36.4/go.mod h1:f26RulijcxdgrGSYep0AykXM9ZkWoKVtInstDYUR8EU=
go.opentelemetry.io/otel v1.10.0 h1:Y7DTJMR6zs1xkS/upamJYk0SxxN4C9AqRd77jmZnyY4=
go.opentelemetry.io/otel v1.10.0/go.mod h1:NbvWjCthWHKBEUMpf0/v8ZRZlni86PpGFEMA9pnQSnQ=
go.opentelemetry.io/otel v1.11.1 h1:4WLLAmcfkmDk2ukNXJyq3/kiz/3UzCaYq6PskJsaou4=
go.opentelemetry.io/otel v1.11.1/go.mod h1:1nNhXBbWSD0nsL38H6btgnFN2k4i0sNLHNNMZMSbUGE=
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.10.0 h1:c9UtMu/qnbLlVwTwt+ABrURrioEruapIslTDYZHJe2w=
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.10.0/go.mod h1:h3Lrh9t3Dnqp3NPwAZx7i37UFX7xrfnO1D+fuClREOA=
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.11.1 h1:3Yvzs7lgOw8MmbxmLRsQGwYdCubFmUHSooKaEhQunFQ=
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.11.1/go.mod h1:pyHDt0YlyuENkD2VwHsiRDf+5DfI3EH7pfhUYW6sQUE=
go.opentelemetry.io/otel/sdk v1.10.0 h1:jZ6K7sVn04kk/3DNUdJ4mqRlGDiXAVuIG+MMENpTNdY=
go.opentelemetry.io/otel/sdk v1.10.0/go.mod h1:vO06iKzD5baltJz1zarxMCNHFpUlUiOy4s65ECtn6kE=
go.opentelemetry.io/otel/sdk v1.11.1 h1:F7KmQgoHljhUuJyA+9BiU+EkJfyX5nVVF4wyzWZpKxs=
go.opentelemetry.io/otel/sdk v1.11.1/go.mod h1:/l3FE4SupHJ12TduVjUkZtlfFqDCQJlOlithYrdktys=
go.opentelemetry.io/otel/trace v1.10.0 h1:npQMbR8o7mum8uF95yFbOEJffhs1sbCOfDh8zAJiH5E=
go.opentelemetry.io/otel/trace v1.10.0/go.mod h1:Sij3YYczqAdz+EhmGhE6TpTxUO5/F/AzrK+kxfGqySM=
go.opentelemetry.io/otel/trace v1.11.1 h1:ofxdnzsNrGBYXbP7t7zpUK281+go5rF7dvdIZXF8gdQ=
go.opentelemetry.io/otel/trace v1.11.1/go.mod h1:f/Q9G7vzk5u91PhbmKbg1Qn0rzH1LJ4vbPHFGkTPtOk=
golang.org/x/sys v0.0.0-20220927170352-d9d178bc13c6 h1:cy1ko5847T/lJ45eyg/7uLprIE/amW5IXxGtEnQdYMI=
golang.org/x/sys v0.0.0-20220927170352-d9d178bc13c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
28 changes: 8 additions & 20 deletions go/samples/http/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package main

import (
"context"
"database/sql"
"flag"
"log"
"net/http"

"github.com/google/sqlcommenter/go/core"
gosql "github.com/google/sqlcommenter/go/database/sql"
httpnet "github.com/google/sqlcommenter/go/net/http"
gosqlmux "github.com/google/sqlcommenter/go/gorrila/mux"
"github.com/gorilla/mux"
"go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux"
"go.opentelemetry.io/otel"
Expand All @@ -21,15 +20,6 @@ import (
"sqlcommenter-http/todos"
)

// middleware is used to intercept incoming HTTP calls and apply general functions upon them.
func middleware(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := core.ContextInject(r.Context(), httpnet.NewHTTPRequestExtractor(r, h))
log.Printf("HTTP request sent to %s", r.URL.Path)
h.ServeHTTP(w, r.WithContext(ctx))
})
}

func runApp(todosController *todos.TodosController) {
err := todosController.CreateTodosTableIfNotExists()
if err != nil {
Expand All @@ -47,27 +37,25 @@ func runApp(todosController *todos.TodosController) {
}()

r := mux.NewRouter()
r.Use(otelmux.Middleware("sqlcommenter sample-server"))
r.Use(otelmux.Middleware("sqlcommenter sample-server"), gosqlmux.SQLCommenterMiddleware)

r.HandleFunc("/todos", todosController.ActionList).Methods("GET")
r.HandleFunc("/todos", todosController.ActionInsert).Methods("POST")
r.HandleFunc("/todos/{id}", todosController.ActionUpdate).Methods("PUT")
r.HandleFunc("/todos/{id}", todosController.ActionDelete).Methods("DELETE")

http.ListenAndServe(":8081", middleware(r))
http.ListenAndServe(":8081", r)
}

// host = “host.docker.internal”

func runForMysql() *gosql.DB {
func runForMysql() *sql.DB {
connection := "root:password@tcp(mysql:3306)/sqlcommenter_db"
db := mysqldb.ConnectMySQL(connection)
todosController := &todos.TodosController{Engine: "mysql", DB: db, SQL: todos.MySQLQueries{}}
runApp(todosController)
return db
}

func runForPg() *gosql.DB {
func runForPg() *sql.DB {
connection := "host=postgres user=postgres password=postgres dbname=postgres port=5432 sslmode=disable"
db := pgdb.ConnectPG(connection)
todosController := &todos.TodosController{Engine: "pg", DB: db, SQL: todos.PGQueries{}}
Expand All @@ -92,14 +80,14 @@ func initTracer() (*sdktrace.TracerProvider, error) {
func main() {
var engine string

flag.StringVar(&engine, "db_engine", "mysql", "db-engine to run the sample application on")
flag.StringVar(&engine, "db_engine", "pg", "db-engine to run the sample application on")
flag.Parse()

if engine != "mysql" && engine != "pg" {
log.Fatalf("invalid engine: %s", engine)
}

var db *gosql.DB
var db *sql.DB

switch engine {
case "mysql":
Expand Down
7 changes: 5 additions & 2 deletions go/samples/http/mysqldb/mysql_driver.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package mysqldb

import (
"database/sql"
"log"

_ "github.com/go-sql-driver/mysql"
"github.com/google/sqlcommenter/go/core"
gosql "github.com/google/sqlcommenter/go/database/sql"
)

func ConnectMySQL(connection string) *gosql.DB {
db, err := gosql.Open("mysql", connection, core.CommenterOptions{EnableDBDriver: true, EnableRoute: true, EnableAction: true, EnableFramework: true, EnableTraceparent: true})
func ConnectMySQL(connection string) *sql.DB {
db, err := gosql.Open("mysql", connection, core.CommenterOptions{
Config: core.CommenterConfig{EnableDBDriver: true, EnableRoute: true, EnableAction: true, EnableFramework: true, EnableTraceparent: true, EnableApplication: true},
})
if err != nil {
log.Fatalf("Failed to connect to MySQL(%q), error: %v", connection, err)
}
Expand Down
7 changes: 5 additions & 2 deletions go/samples/http/pgdb/pg_driver.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package pgdb

import (
"database/sql"
"log"

"github.com/google/sqlcommenter/go/core"
gosql "github.com/google/sqlcommenter/go/database/sql"
_ "github.com/lib/pq"
)

func ConnectPG(connection string) *gosql.DB {
db, err := gosql.Open("postgres", connection, core.CommenterOptions{EnableDBDriver: true, EnableRoute: true, EnableAction: true, EnableFramework: true, EnableTraceparent: true})
func ConnectPG(connection string) *sql.DB {
db, err := gosql.Open("postgres", connection, core.CommenterOptions{
Config: core.CommenterConfig{EnableDBDriver: true, EnableRoute: true, EnableAction: true, EnableFramework: true, EnableTraceparent: true, EnableApplication: true},
})
if err != nil {
log.Fatalf("Failed to connect to PG(%q), error: %v", connection, err)
}
Expand Down
4 changes: 2 additions & 2 deletions go/samples/http/todos/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package todos

import (
"context"
"database/sql"
"encoding/json"
"fmt"
"log"
"net/http"

gosql "github.com/google/sqlcommenter/go/database/sql"
"github.com/gorilla/mux"
)

Expand All @@ -22,7 +22,7 @@ type TodoDTO struct {

type TodosController struct {
Engine string
DB *gosql.DB
DB *sql.DB
SQL TodosQueries
}

Expand Down

0 comments on commit 9ae8338

Please sign in to comment.