Skip to content

Commit

Permalink
Merge pull request #5 from ghanithan/2-create-http-server
Browse files Browse the repository at this point in the history
feat: half way through with db and server
  • Loading branch information
ghanithan authored Oct 21, 2024
2 parents 5669472 + ec190ad commit f6cd15f
Show file tree
Hide file tree
Showing 15 changed files with 460 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ jobs:
run: go build -v ./...

- name: Test
run: go test -v ./...
run: go test -short -v ./...
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/settings/default.yaml
25 changes: 25 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package config

type ConifgService interface {
GetConfig(...string) *Config
}

type (
Config struct {
Server ServerConfig `yaml: server`
Db DbConfig `yaml: db`
}

ServerConfig struct {
Host string `yaml: host`
Port string `yaml: port`
}

DbConfig struct {
Host string `yaml: host`
Port string `yaml: port`
Reponame string `yaml: reponame `
Username string `yaml: username`
Password string `yaml: password`
}
)
34 changes: 34 additions & 0 deletions config/getConfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package config

import (
"os"

"gopkg.in/yaml.v2"
)

func GetConfig(args ...string) (*Config, error) {
//init config struct
config := &Config{}

// set default file path
filePath := "../settings/sample.yaml"
// collect the filepath from varidac arguments if provided
if len(args) > 0 {
filePath = args[0]
}

// read the file
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()

// unmarshal the yaml file into conifg struct
decoder := yaml.NewDecoder(file)
if err := decoder.Decode(&config); err != nil {
return nil, err
}

return config, nil
}
35 changes: 35 additions & 0 deletions config/getConfig_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package config

import (
"reflect"
"testing"
)

func TestGetConfig(t *testing.T) {
t.Run(
"Testing getConfig",
func(t *testing.T) {
want := &Config{
Server: ServerConfig{
Host: "http://localhost",
Port: "3000",
},
Db: DbConfig{
Host: "localhost",
Port: "8091",
Reponame: "vizha",
Username: "username",
Password: "password",
},
}
got, err := GetConfig()
if err != nil {
t.Fatalf("Error in fetching config: %s", err)
}
if !reflect.DeepEqual(want, got) {
t.Fatalf("expected %q got %q", want, got)

}
},
)
}
66 changes: 66 additions & 0 deletions database/cb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package database

import (
"log"
"sync"
"time"

"github.com/couchbase/gocb/v2"
"github.com/ghanithan/goBilling/config"
)

// Module for couchbase db wrapper

type couchbaseDatabase struct {
Db *gocb.Cluster
}

var (
once sync.Once
dbInstance couchbaseDatabase
)

func InitCouchbaseDb(config *config.Config) couchbaseDatabase {
once.Do(func() {
options := gocb.ClusterOptions{
Authenticator: gocb.PasswordAuthenticator{
Username: config.Db.Username,
Password: config.Db.Password,
},
}

// Sets a pre-configured profile called "wan-development" to help avoid latency issues
// when accessing Capella from a different Wide Area Network
// or Availability Zone (e.g. your laptop).
if err := options.ApplyProfile(gocb.ClusterConfigProfileWanDevelopment); err != nil {
log.Fatal(err, "Error in profile")
}

// Initialize the Connection
cluster, err := gocb.Connect("couchbase://"+config.Db.Host, options)
if err != nil {
log.Fatal(err, " connecting cluster")
}

// bucket := cluster.Bucket(config.Db.Reponame)

err = cluster.WaitUntilReady(5*time.Second, nil)
if err != nil {
log.Fatal(err)
}

dbInstance = couchbaseDatabase{
Db: cluster,
}
})
return dbInstance
}

func (cb *couchbaseDatabase) GetDb() *gocb.Cluster {
return cb.Db
}

func (cb *couchbaseDatabase) Close() {
closeOpt := gocb.ClusterCloseOptions{}
cb.Db.Close(&closeOpt)
}
72 changes: 72 additions & 0 deletions database/cb_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package database

import (
"fmt"
"testing"

"github.com/couchbase/gocb/v2"
"github.com/ghanithan/goBilling/config"
)

func TestInitCouchbaseDb(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
config, err := config.GetConfig("../settings/default.yaml")
if err != nil {
t.Fatalf("Error in reading config: %s", err)
}

db := InitCouchbaseDb(config)

cluster := db.GetDb()

// col := bucket.Scope("_default").Collection("customers")

// // Get the document back
// getResult, err := col.Get("custid:C1", nil)
// if err != nil {
// t.Errorf("Error fetching: %s", err)
// }

qOpts := gocb.QueryOptions{}

// create query
queryStr := "select * from default:vizha._default.customers"

fmt.Printf("query: %v\n", queryStr)

rows, err := cluster.Query(queryStr, &qOpts)
if err != nil {
panic(err)
}

fmt.Printf("rows: %v\n", rows)

for rows.Next() {
var intfc interface{}
err = rows.Row(&intfc)
if err != nil {
panic(err)
}
fmt.Printf("interface result: %v\n", intfc)
}

// type UserType struct {
// Id string `json:custid`
// Name string `json: name`
// Address struct {
// Street string `json: street`
// City string `json: city`
// Zipcode string `json: zipcode`
// } `json: address`
// }

// user := &UserType{}
// err = getResult.Content(&user)
// if err != nil {
// log.Fatal(err)
// }
// fmt.Printf("User: %v\n", user)

}
10 changes: 10 additions & 0 deletions database/database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package database

type DbService interface {
GetDb() any
CloseDb()
}

type DbStub interface {
FetchById(...string) any
}
24 changes: 24 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,34 @@ module github.com/ghanithan/goBilling

go 1.23

require github.com/couchbase/gocb v1.6.7

require (
github.com/couchbase/gocb/v2 v2.9.2 // indirect
github.com/couchbase/gocbcore/v10 v10.5.2 // indirect
github.com/couchbase/gocbcoreps v0.1.3 // indirect
github.com/couchbase/goprotostellar v1.0.2 // indirect
github.com/couchbaselabs/gocbconnstr/v2 v2.0.0-20240607131231-fb385523de28 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect
github.com/opentracing/opentracing-go v1.1.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 // indirect
go.opentelemetry.io/otel v1.31.0 // indirect
go.opentelemetry.io/otel/metric v1.31.0 // indirect
go.opentelemetry.io/otel/trace v1.31.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240401170217-c3f982113cda // indirect
google.golang.org/grpc v1.63.2 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/couchbase/gocbcore.v7 v7.1.18 // indirect
gopkg.in/couchbaselabs/gocbconnstr.v1 v1.0.4 // indirect
gopkg.in/couchbaselabs/jsonx.v1 v1.0.1 // indirect
gopkg.in/yaml.v2 v2.4.0
)
Loading

0 comments on commit f6cd15f

Please sign in to comment.