Skip to content

Commit c6adcb2

Browse files
Ranuga DisansaRanuga Disansa
authored andcommitted

File tree

7 files changed

+96
-4
lines changed

7 files changed

+96
-4
lines changed

Go/GoBank/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ build:
22
@go build -o bin/gobank
33

44
run:
5-
build
5+
make build
66
@./bin/gobank
77

88
test:

Go/GoBank/api.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,70 @@
11
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"log"
7+
"net/http"
8+
9+
"github.com/gorilla/mux"
10+
)
11+
12+
func WriteJSON(w http.ResponseWriter, status int, v any) error {
13+
w.WriteHeader(status)
14+
w.Header().Set("Content-Type", "application/json")
15+
return json.NewEncoder(w).Encode(v)
16+
}
17+
18+
type apiFunc func(http.ResponseWriter, *http.Request) error
19+
20+
type ApiError struct {
21+
Error string
22+
}
23+
24+
func makeHttpHandleFunc(f apiFunc) http.HandlerFunc {
25+
return func(w http.ResponseWriter, r *http.Request) {
26+
if err := f(w, r); err != nil {
27+
WriteJSON(w, http.StatusBadRequest, ApiError{Error: "Bad Error"})
28+
}
29+
}
30+
}
31+
32+
type APIServer struct {
33+
listenAddr string
34+
}
35+
36+
func NewAPIServer(listenAdd string) *APIServer {
37+
return &APIServer{
38+
listenAddr: listenAdd,
39+
}
40+
}
41+
42+
func (s *APIServer) Run() {
43+
router := mux.NewRouter()
44+
router.HandleFunc("/account", makeHttpHandleFunc(s.handleAccount))
45+
router.HandleFunc("/account/{id}", makeHttpHandleFunc(s.handleGetAccount))
46+
log.Println("JSON API Server Running on Port: ", s.listenAddr)
47+
http.ListenAndServe(s.listenAddr, router)
48+
}
49+
50+
func (s *APIServer) handleAccount(w http.ResponseWriter, r *http.Request) error {
51+
if r.Method == "GET" {
52+
return s.handleGetAccount(w, r)
53+
}
54+
if r.Method == "POST" {
55+
return s.handleCreateAccount(w, r)
56+
}
57+
if r.Method == "DELETE" {
58+
return s.handleDeleteAccount(w, r)
59+
}
60+
return fmt.Errorf("Method Not Allowed %s", r.Method)
61+
}
62+
63+
func (s *APIServer) handleGetAccount(w http.ResponseWriter, r *http.Request) error {
64+
vars := mux.Vars(r)
65+
// account := NewAccount("ranuga", "gamage")
66+
return WriteJSON(w, http.StatusOK, vars)
67+
}
68+
func (s *APIServer) handleCreateAccount(w http.ResponseWriter, r *http.Request) error { return nil }
69+
func (s *APIServer) handleDeleteAccount(w http.ResponseWriter, r *http.Request) error { return nil }
70+
func (s *APIServer) handleTransfer(w http.ResponseWriter, r *http.Request) error { return nil }

Go/GoBank/bin/gobank

8.31 MB
Binary file not shown.

Go/GoBank/go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module github.com/Programmer-RD-AI/learning
22

33
go 1.25.3
4+
5+
require github.com/gorilla/mux v1.8.1 // indirect

Go/GoBank/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
2+
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=

Go/GoBank/main.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

3-
import "fmt"
4-
53
func main() {
6-
fmt.Println("hello world")
4+
server := NewAPIServer(":3000")
5+
server.Run()
76
}

Go/GoBank/types.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import "math/rand"
4+
5+
type Account struct {
6+
ID int `json:"id"`
7+
FirstName string `json:"firstName"`
8+
LastName string `json:"lastName"`
9+
Number int64 `json:"number"`
10+
Balance int64 `json:"balance"`
11+
}
12+
13+
func NewAccount(firstName, lastName string) *Account {
14+
return &Account{
15+
ID: rand.Intn(1000),
16+
FirstName: firstName,
17+
LastName: lastName,
18+
Number: int64(rand.Intn(100000)),
19+
}
20+
}

0 commit comments

Comments
 (0)