Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: add IFSC service #85

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ dig fun.dict @dns.toys

dig excuse @dns.toys

dig ABNA0000001.ifsc @dns.toys

dig A12.9352,77.6245/12.9698,77.7500.aerial @dns.toys
```

Expand Down
10 changes: 10 additions & 0 deletions cmd/dnstoys/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/knadh/dns.toys/internal/services/epoch"
"github.com/knadh/dns.toys/internal/services/excuse"
"github.com/knadh/dns.toys/internal/services/fx"
"github.com/knadh/dns.toys/internal/services/ifsc"
"github.com/knadh/dns.toys/internal/services/num2words"
"github.com/knadh/dns.toys/internal/services/random"
"github.com/knadh/dns.toys/internal/services/sudoku"
Expand Down Expand Up @@ -346,6 +347,15 @@ func main() {
help = append(help, []string{"return a developer excuse", "dig excuse @%s"})
}

if ko.Bool("ifsc.enabled") {
e, err := ifsc.New(ko.MustString("ifsc.ifsc_path"))
if err != nil {
lo.Fatalf("error initializing ifsc service: %v", err)
}
h.register("ifsc", e, mux)
help = append(help, []string{"lookup bank details for IFSC code", "dig ABNA0000001.ifsc @%s"})
}

// Prepare the static help response for the `help` query.
for _, l := range help {
r, err := dns.NewRR(fmt.Sprintf("help. %d TXT \"%s\" \"%s\"", HELP_TTL, l[0], fmt.Sprintf(l[1], h.domain)))
Expand Down
8 changes: 8 additions & 0 deletions config.sample.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,11 @@ enabled = true
[excuse]
enabled = true
file = "data/excuses.txt"

[ifsc]
enabled = true

# You will need to manually download the IFSC data
# Download from https://github.com/razorpay/ifsc/releases/download/latest/by-bank.tar.gz
# Untar the folder and save its contents within data/ifsc directory
ifsc_path = "data/ifsc"
gopuvenkat marked this conversation as resolved.
Show resolved Hide resolved
8 changes: 8 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,14 @@ <h2>Developer Excuse</h2>
<p>Return a developer excuse.</p>
</section>

<section class="box">
<h2>Bank details for an IFSC code</h2>
<code class="block">
<p>dig ABNA0000001.ifsc @dns.toys</p>
</code>
<p>Lookup bank details for a given IFSC code</p>
</section>

<section class="box">
<h2>Help</h2>
<code class="block">
Expand Down
99 changes: 99 additions & 0 deletions internal/services/ifsc/ifsc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package ifsc

import (
"encoding/json"
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"
)

const (
ifscCodeLen = 11
)

type Branch struct {
Bank string `json:"BANK"`
IFSC string `json:"IFSC"`
Branch string `json:"BRANCH"`
City string `json:"CITY"`
State string `json:"STATE"`
}
gopuvenkat marked this conversation as resolved.
Show resolved Hide resolved

type BranchDetails struct {
Bank string
Branch string
City string
State string
}

type IFSC struct {
data map[string]BranchDetails
}

func New(dir string) (*IFSC, error) {
// Load IFSCs from disk
log.Printf("loading IFSC data from %s", dir)
files, err := os.ReadDir(dir)
if err != nil {
return nil, fmt.Errorf("error accessing IFSC directory: %w", err)
}

var ifsc IFSC
ifsc.data = make(map[string]BranchDetails)

for _, file := range files {
var jsonFilePath = filepath.Join(dir, file.Name())
jsonFile, err := os.Open(jsonFilePath)
if err != nil {
return nil, fmt.Errorf("error opening IFSC JSON file: %w", err)
}
defer jsonFile.Close()

byteValue, err := io.ReadAll(jsonFile)
if err != nil {
return nil, fmt.Errorf("error reading IFSC JSON file: %w", err)
}

var branches map[string]Branch
json.Unmarshal(byteValue, &branches)

for _, v := range branches {
ifsc.data[v.IFSC] = BranchDetails{Bank: v.Bank, Branch: v.Branch, City: v.City, State: v.State}
}
}
log.Printf("loaded IFSC data")

return &ifsc, nil
}

func (i *IFSC) Query(q string) ([]string, error) {
ifscCode := strings.TrimSuffix(q, ".")
ifscCode = strings.TrimSuffix(q, ".ifsc")
ifscCode = strings.ToUpper(ifscCode)

if len(ifscCode) != ifscCodeLen {
return nil, fmt.Errorf("invalid IFSC code length: %d", len(ifscCode))
}

var output []string
value, ok := i.data[ifscCode]
if ok {
output = []string{
fmt.Sprintf(`%s.ifsc. 1 IN TXT "BANK: %s"`, ifscCode, value.Bank),
fmt.Sprintf(`%s.ifsc. 1 IN TXT "BRANCH: %s"`, ifscCode, value.Branch),
fmt.Sprintf(`%s.ifsc. 1 IN TXT "CITY: %s"`, ifscCode, value.City),
fmt.Sprintf(`%s.ifsc. 1 IN TXT "STATE: %s"`, ifscCode, value.State),
}
} else {
output = []string{fmt.Sprintf(`%s.ifsc. 1 IN TXT "IFSC code %s not found"`, ifscCode, ifscCode)}
}

return output, nil
}

func (i *IFSC) Dump() ([]byte, error) {
return nil, nil
}