Skip to content

Commit 69274a7

Browse files
author
Kapil Verma
committed
wiring done
1 parent a7117f6 commit 69274a7

5 files changed

Lines changed: 146 additions & 42 deletions

File tree

go/samples/http/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
)
1010

1111
require (
12+
github.com/julienschmidt/httprouter v1.3.0 // indirect
1213
github.com/lib/pq v1.10.7 // indirect
1314
go.opentelemetry.io/otel v1.10.0 // indirect
1415
)

go/samples/http/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ github.com/google/sqlcommenter/go/database/sql v0.0.1-beta h1:N680pEYaRwmOSrQWUd
1414
github.com/google/sqlcommenter/go/database/sql v0.0.1-beta/go.mod h1:VdswmF4SM0cbjJdD+3GyM5QuXpHhH6F5nSzcbikzCGY=
1515
github.com/google/sqlcommenter/go/net/http v0.0.1-beta h1:7XQ6poZv+ZJwwHWQHlesq9IMsRus3G6Z9n10qAkrGqE=
1616
github.com/google/sqlcommenter/go/net/http v0.0.1-beta/go.mod h1:tVUqM1YZ/K3eRTdGzeav1GSbw+BXNdTGzSAbLW9CxAc=
17+
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
18+
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
1719
github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw=
1820
github.com/lib/pq v1.10.7/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
1921
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

go/samples/http/main.go

Lines changed: 56 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,82 +9,96 @@ import (
99
"github.com/google/sqlcommenter/go/core"
1010
gosql "github.com/google/sqlcommenter/go/database/sql"
1111
httpnet "github.com/google/sqlcommenter/go/net/http"
12+
"github.com/julienschmidt/httprouter"
1213
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
1314
sdktrace "go.opentelemetry.io/otel/sdk/trace"
1415

1516
"sqlcommenter-http/mysqldb"
1617
"sqlcommenter-http/pgdb"
18+
"sqlcommenter-http/todos"
1719
)
1820

19-
var db *gosql.DB
21+
func MakeIndexRoute(db *gosql.DB) func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
22+
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
23+
exp, _ := stdouttrace.New(stdouttrace.WithPrettyPrint())
24+
bsp := sdktrace.NewSimpleSpanProcessor(exp) // You should use batch span processor in prod
25+
tp := sdktrace.NewTracerProvider(
26+
sdktrace.WithSampler(sdktrace.AlwaysSample()),
27+
sdktrace.WithSpanProcessor(bsp),
28+
)
2029

21-
func Index(w http.ResponseWriter, r *http.Request) {
22-
exp, _ := stdouttrace.New(stdouttrace.WithPrettyPrint())
23-
bsp := sdktrace.NewSimpleSpanProcessor(exp) // You should use batch span processor in prod
24-
tp := sdktrace.NewTracerProvider(
25-
sdktrace.WithSampler(sdktrace.AlwaysSample()),
26-
sdktrace.WithSpanProcessor(bsp),
27-
)
30+
ctx, span := tp.Tracer("foo").Start(r.Context(), "parent-span-name")
31+
defer span.End()
2832

29-
ctx, span := tp.Tracer("foo").Start(r.Context(), "parent-span-name")
30-
defer span.End()
33+
db.ExecContext(ctx, "Select 1")
34+
db.Exec("Select 2")
3135

32-
db.ExecContext(ctx, "Select 1")
33-
db.Exec("Select 2")
36+
stmt1, err := db.Prepare("Select 3")
37+
if err != nil {
38+
log.Fatal(err)
39+
}
40+
stmt1.QueryRow()
3441

35-
stmt1, err := db.Prepare("Select 3")
36-
if err != nil {
37-
log.Fatal(err)
42+
stmt2, err := db.PrepareContext(ctx, "Select 4")
43+
if err != nil {
44+
log.Fatal(err)
45+
}
46+
stmt2.QueryRow()
47+
48+
db.QueryContext(ctx, "Select 5")
49+
50+
fmt.Fprintf(w, "Hello World!\r\n")
3851
}
39-
stmt1.QueryRow()
52+
}
4053

41-
stmt2, err := db.PrepareContext(ctx, "Select 4")
54+
// middleware is used to intercept incoming HTTP calls and apply general functions upon them.
55+
func middleware(next httprouter.Handle) httprouter.Handle {
56+
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
57+
ctx := core.ContextInject(r.Context(), httpnet.NewHTTPRequestExtractor(r, next))
58+
log.Printf("HTTP request sent to %s from %v", r.URL.Path, next)
59+
next(w, r.WithContext(ctx), p)
60+
}
61+
}
62+
63+
func runApp(todosController *todos.TodosController) {
64+
err := todosController.CreateTodosTableIfNotExists()
4265
if err != nil {
4366
log.Fatal(err)
4467
}
45-
stmt2.QueryRow()
4668

47-
db.QueryContext(ctx, "Select 5")
69+
router := httprouter.New()
4870

49-
fmt.Fprintf(w, "Hello World!\r\n")
50-
}
71+
index := MakeIndexRoute(todosController.DB)
72+
router.GET("/", middleware(index))
5173

52-
// middleware is used to intercept incoming HTTP calls and apply general functions upon them.
53-
func middleware(next http.Handler) http.Handler {
54-
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
55-
ctx := core.ContextInject(r.Context(), httpnet.NewHTTPRequestExtractor(r, next))
56-
log.Printf("HTTP request sent to %s from %v", r.URL.Path, next)
57-
next.ServeHTTP(w, r.WithContext(ctx))
58-
})
74+
router.GET("/todos", middleware(todosController.ActionList))
75+
router.POST("/todos", middleware(todosController.ActionInsert))
76+
router.PUT("/todos/:id", middleware(todosController.ActionUpdate))
77+
router.DELETE("/todos/:id", middleware(todosController.ActionDelete))
78+
79+
http.ListenAndServe(":8080", router)
5980
}
6081

6182
func runForMysql() *gosql.DB {
6283
connection := "root:password@/sqlcommenter_db"
63-
db = mysqldb.ConnectMySQL(connection)
64-
65-
mux := http.NewServeMux()
66-
finalHandler := http.HandlerFunc(Index)
67-
mux.Handle("/", middleware((finalHandler)))
68-
log.Fatal(http.ListenAndServe(":8080", mux))
69-
return db
84+
db := mysqldb.ConnectMySQL(connection)
85+
todosController := &todos.TodosController{DB: db, SQL: todos.MySQLQueries{}}
86+
runApp(todosController)
7087
return db
7188
}
7289

7390
func runForPg() *gosql.DB {
7491
connection := "postgres://dev:dev@localhost/sqlcommenter_db?sslmode=disable"
75-
db = pgdb.ConnectPG(connection)
76-
77-
mux := http.NewServeMux()
78-
finalHandler := http.HandlerFunc(Index)
79-
mux.Handle("/", middleware((finalHandler)))
80-
log.Fatal(http.ListenAndServe(":8080", mux))
92+
db := pgdb.ConnectPG(connection)
93+
todosController := &todos.TodosController{DB: db, SQL: todos.PGQueries{}}
94+
runApp(todosController)
8195
return db
8296
}
8397

8498
func main() {
8599
var engine string
86100

87-
flag.StringVar(&engine, "db_engine", "mysql", "db-engine to run the sample on")
101+
flag.StringVar(&engine, "db_engine", "mysql", "db-engine to run the sample application on")
88102
flag.Parse()
89103

90104
if engine != "mysql" && engine != "pg" {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package todos
2+
3+
import (
4+
"net/http"
5+
6+
gosql "github.com/google/sqlcommenter/go/database/sql"
7+
"github.com/julienschmidt/httprouter"
8+
)
9+
10+
type TodosController struct {
11+
DB *gosql.DB
12+
SQL TodosQueries
13+
}
14+
15+
func (c *TodosController) CreateTodosTableIfNotExists() error {
16+
return nil
17+
}
18+
19+
func (c *TodosController) ActionList(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
20+
21+
}
22+
23+
func (c *TodosController) ActionInsert(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
24+
25+
}
26+
27+
func (c *TodosController) ActionUpdate(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
28+
29+
}
30+
31+
func (c *TodosController) ActionDelete(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
32+
33+
}

go/samples/http/todos/queries.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package todos
2+
3+
type PGQueries struct{}
4+
type MySQLQueries struct{}
5+
6+
type TodosQueries interface {
7+
CreateTodosTableIfNotExists() string
8+
ListTodos(search string) string
9+
InsertTodo(task string) string
10+
UpdateTodo(id int, task string) string
11+
DeleteTodo(id int) string
12+
}
13+
14+
// PGQueries impl
15+
func (sql PGQueries) CreateTodosTableIfNotExists() string {
16+
return ""
17+
}
18+
19+
func (sql PGQueries) ListTodos(search string) string {
20+
return ""
21+
}
22+
23+
func (sql PGQueries) InsertTodo(task string) string {
24+
return ""
25+
}
26+
27+
func (sql PGQueries) UpdateTodo(id int, task string) string {
28+
return ""
29+
}
30+
31+
func (sql PGQueries) DeleteTodo(id int) string {
32+
return ""
33+
}
34+
35+
// MySQLQueries impl
36+
func (sql MySQLQueries) CreateTodosTableIfNotExists() string {
37+
return ""
38+
}
39+
40+
func (sql MySQLQueries) ListTodos(search string) string {
41+
return ""
42+
}
43+
44+
func (sql MySQLQueries) InsertTodo(task string) string {
45+
return ""
46+
}
47+
48+
func (sql MySQLQueries) UpdateTodo(id int, task string) string {
49+
return ""
50+
}
51+
52+
func (sql MySQLQueries) DeleteTodo(id int) string {
53+
return ""
54+
}

0 commit comments

Comments
 (0)