diff --git a/.gitignore b/.gitignore index 83b2c94..edafdc4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ tmp -bin \ No newline at end of file +bin +config.yaml \ No newline at end of file diff --git a/config.template.yaml b/config.template.yaml new file mode 100644 index 0000000..9a652bf --- /dev/null +++ b/config.template.yaml @@ -0,0 +1,24 @@ + +secretkey: 7A25432A462D4A614E645267556B5870 +privatekey: /home/his/backend/internal/auth/keys/private.pem +hashCost: 9 +dbuser: root +dbpass: +dbhost: localhost +dbport: 3306 +dbname: his +tokenExpiration: 120 +dbsequser: root +dbseqpass: +dbseqhost: localhost +dbseqport: 3306 +dbseqname: his_seq +photo: /home/ubuntu/photo-folder-location/ +# temporary param +invoice_prefix: RSPAD +vclaimProviderCode: 0901R003 +vclaimConsID: 6057 +vclaimSecretKey: rspad211016 +vclaimUserKey: 32e99aefa0866a5880067b27d151c3e4 +vclaimBaseURL: https://apijkn-dev.bpjs-kesehatan.go.id/vclaim-rest-dev +vclaimAntreanBaseURL: https://apijkn-dev.bpjs-kesehatan.go.id/antreanrs_dev \ No newline at end of file diff --git a/package/config/config.go b/package/config/config.go new file mode 100644 index 0000000..78589df --- /dev/null +++ b/package/config/config.go @@ -0,0 +1,53 @@ +package config + +import ( + "log" + + "github.com/fsnotify/fsnotify" + "github.com/spf13/viper" + "go.uber.org/zap" +) + +type Reader interface { + Get(key string) string +} + +type viperConfigReader struct { + viper *viper.Viper +} + +var Data *viperConfigReader + +func (v viperConfigReader) Get(key string) string { + return v.viper.GetString(key) +} + +func (v viperConfigReader) GetInt(key string) int { + return v.viper.GetInt(key) +} + +func Load() { + v := viper.New() + v.AddConfigPath(".") + v.SetConfigName("config") + v.SetConfigType("yaml") + v.AutomaticEnv() + + err := v.ReadInConfig() + if err != nil { + return + } + Data = &viperConfigReader{ + viper: v, + } + v.WatchConfig() + v.OnConfigChange(func(e fsnotify.Event) { + log.Println("config file changed", e.Name) + logger, _ := zap.NewProduction() + defer logger.Sync() + logger.Info("Config file change", + zap.String("name", e.Name), + ) + }) + return +} diff --git a/package/database/database.go b/package/database/database.go new file mode 100644 index 0000000..32e08c3 --- /dev/null +++ b/package/database/database.go @@ -0,0 +1,95 @@ +package database + +import ( + "bytes" + "database/sql" + "fmt" + "log" + "strings" + + "cpone/package/config" + + _ "github.com/go-sql-driver/mysql" +) + +var Handle *sql.DB +var HandleSeq *sql.DB + +func InitDB() { + println(config.Data.Get("DBhost")) + if config.Data.Get("DBhost") == "" { + println("sqlite") + + handle, err := sql.Open("sqlite", config.Data.Get("DBname")) + if err != nil { + // log.Fatalf("🔥 failed to connect to the database: %s", err.Error()) + log.Panic(err) + } + Handle = handle + } else { + println("mysql") + dsn := config.Data.Get("DBuser") + ":" + config.Data.Get("DBpass") + "@tcp(" + config.Data.Get("DBhost") + ":" + config.Data.Get("DBport") + ")/" + config.Data.Get("DBname") + println(dsn) + handle, err := sql.Open("mysql", dsn) + + if err != nil { + log.Panic(err) + } + + if err = handle.Ping(); err != nil { + log.Panic(err) + } + Handle = handle + + } + log.Println("🚀 Connected Successfully to the Database") + +} + +func LogSQL(query string, args ...interface{}) { + var buffer bytes.Buffer + nArgs := len(args) + // Break the string by question marks, iterate over its parts and for each + // question mark - append an argument and format the argument according to + // it's type, taking into consideration NULL values and quoting strings. + for i, part := range strings.Split(query, "?") { + buffer.WriteString(part) + if i < nArgs { + switch a := args[i].(type) { + case int: + buffer.WriteString(fmt.Sprintf("%d", a)) + case int64: + buffer.WriteString(fmt.Sprintf("%d", a)) + case bool: + buffer.WriteString(fmt.Sprintf("%t", a)) + case sql.NullBool: + if a.Valid { + buffer.WriteString(fmt.Sprintf("%t", a.Bool)) + } else { + buffer.WriteString("NULL") + } + case sql.NullInt64: + if a.Valid { + buffer.WriteString(fmt.Sprintf("%d", a.Int64)) + } else { + buffer.WriteString("NULL") + } + case sql.NullString: + if a.Valid { + buffer.WriteString(fmt.Sprintf("%q", a.String)) + } else { + buffer.WriteString("NULL") + } + case sql.NullFloat64: + if a.Valid { + buffer.WriteString(fmt.Sprintf("%f", a.Float64)) + } else { + buffer.WriteString("NULL") + } + default: + buffer.WriteString(fmt.Sprintf("%q", a)) + } + } + } + log.Print(buffer.String()) +}