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

Distributor Management HTTP server #166

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
a17f6df
add: init project files
sreeram77 Oct 28, 2024
30ef7cd
add: Region and Distributor models
sreeram77 Oct 28, 2024
0130b08
add: countries model to store geography
sreeram77 Oct 28, 2024
d6fc519
add: csv package to parse file
sreeram77 Oct 28, 2024
08ac768
add: Distributors store interface
sreeram77 Oct 28, 2024
9baf2b7
update: add Parent in models.Distributor
sreeram77 Oct 28, 2024
3e68fb3
add: Parse to parse Region from string
sreeram77 Oct 29, 2024
5c00377
update: add ValidateRegion in Distributor
sreeram77 Oct 29, 2024
cf7292c
fix: Region parse failing to parse only CountryCode
sreeram77 Oct 29, 2024
2258567
refactor: countries to geography
sreeram77 Oct 29, 2024
3ef6473
update: Distributors interface signature
sreeram77 Oct 29, 2024
f0991de
update: ValidateIncludedRegions and ValidateExcludedRegions in Distri…
sreeram77 Oct 29, 2024
ca093b7
add: implement Distributors store
sreeram77 Oct 29, 2024
ee55ea5
add: Validate in Geography
sreeram77 Oct 29, 2024
0ebdaf6
update: validate whether Region exists in Geography
sreeram77 Oct 29, 2024
372cd2d
fix: set parent in Distributors store Insert
sreeram77 Oct 29, 2024
ab2e32f
add: Get by name in Distributors store
sreeram77 Oct 29, 2024
881d9c0
update: add parent in Distributor Stringer
sreeram77 Oct 29, 2024
8582d1f
update: return Distributor pointer
sreeram77 Oct 29, 2024
e54655c
add: cobra cli for commands
sreeram77 Oct 29, 2024
99c94e5
remove: cli
sreeram77 Oct 29, 2024
0dc2782
fix: format of Region String
sreeram77 Oct 29, 2024
437f591
add: handler for distributor HTTP APIs
sreeram77 Oct 29, 2024
1573411
add: ModelToDTO for Distributor
sreeram77 Oct 29, 2024
16d75f3
fix: return on error in handler
sreeram77 Oct 29, 2024
5ae909f
add: OpenAPI spec
sreeram77 Oct 29, 2024
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
main

88 changes: 88 additions & 0 deletions csv/load.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package csv

import (
"encoding/csv"
"errors"
"os"

"challenge2016/models"
)

// Load reads a CSV file and returns a map of countries.
func Load(file string) (models.Geography, error) {
// Load content from file.
content, err := os.Open(file)
if err != nil {
return nil, err
}

// Parse content as CSV.
reader := csv.NewReader(content)
if reader == nil {
return nil, errors.New("could not read CSV")
}

records, err := reader.ReadAll()
if err != nil {
return nil, err
}

countries := make(models.Geography)

for i, record := range records {
// Skip the first row
if i == 0 {
continue
}

country, exists := countries[record[2]]
if !exists {
cities := make(models.Cities)
cities[record[0]] = record[3]

province := models.Province{
Name: record[4],
Code: record[1],
Cities: cities,
}

provinces := make(models.Provinces)
provinces[record[1]] = province

country = models.Country{
Name: record[5],
Code: record[2],
Provinces: provinces,
}

countries[record[2]] = country

continue
}

province, exists := country.Provinces[record[1]]
if !exists {
cities := make(models.Cities)
cities[record[0]] = record[3]

province := models.Province{
Name: record[4],
Code: record[1],
Cities: cities,
}

country.Provinces[record[1]] = province

continue
}

_, exists = province.Cities[record[0]]
if !exists {
province.Cities[record[0]] = record[3]
}

province.Cities[record[0]] = record[3]
}

return countries, nil
}
36 changes: 36 additions & 0 deletions dto/distributor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package dto

import "challenge2016/models"

type Distributor struct {
Name string `json:"name,omitempty"`
Parent string `json:"parent,omitempty"`
IncludeRegions []string `json:"include,omitempty"`
ExcludedRegions []string `json:"exclude,omitempty"`
}

func ModelToDTO(d models.Distributor) Distributor {
var (
includesStr, excludesStr []string
)

for _, region := range d.IncludeRegions {
includesStr = append(includesStr, region.String())
}

for _, region := range d.ExcludedRegions {
excludesStr = append(excludesStr, region.String())
}

resp := Distributor{
Name: d.Name,
IncludeRegions: includesStr,
ExcludedRegions: excludesStr,
}

if d.Parent != nil {
resp.Parent = d.Parent.Name
}

return resp
}
5 changes: 5 additions & 0 deletions dto/region.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package dto

type Region struct {
Region string `json:"region,omitempty"`
}
34 changes: 34 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module challenge2016

go 1.23.1

require github.com/gin-gonic/gin v1.10.0

require (
github.com/bytedance/sonic v1.12.3 // indirect
github.com/bytedance/sonic/loader v0.2.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.6 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.22.1 // indirect
github.com/goccy/go-json v0.10.3 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
golang.org/x/arch v0.11.0 // indirect
golang.org/x/crypto v0.28.0 // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.19.0 // indirect
google.golang.org/protobuf v1.35.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
86 changes: 86 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
github.com/bytedance/sonic v1.12.3 h1:W2MGa7RCU1QTeYRTPE3+88mVC0yXmsRQRChiyVocVjU=
github.com/bytedance/sonic v1.12.3/go.mod h1:B8Gt/XvtZ3Fqj+iSKMypzymZxw/FVwgIGKzMzT9r/rk=
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
github.com/bytedance/sonic/loader v0.2.1 h1:1GgorWTqf12TA8mma4DDSbaQigE2wOgQo7iCjjJv3+E=
github.com/bytedance/sonic/loader v0.2.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gabriel-vasile/mimetype v1.4.6 h1:3+PzJTKLkvgjeTbts6msPJt4DixhT4YtFNf1gtGe3zc=
github.com/gabriel-vasile/mimetype v1.4.6/go.mod h1:JX1qVKqZd40hUPpAfiNTe0Sne7hdfKSbOqqmkq8GCXc=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU=
github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.22.1 h1:40JcKH+bBNGFczGuoBYgX4I6m/i27HYW8P9FDk5PbgA=
github.com/go-playground/validator/v10 v10.22.1/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA=
github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.2.8 h1:+StwCXwm9PdpiEkPyzBXIy+M9KUb4ODm0Zarf1kS5BM=
github.com/klauspost/cpuid/v2 v2.2.8/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M=
github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
golang.org/x/arch v0.11.0 h1:KXV8WWKCXm6tRpLirl2szsO5j/oOODwZf4hATmGVNs4=
golang.org/x/arch v0.11.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw=
golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U=
golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA=
google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
105 changes: 105 additions & 0 deletions handler/distributor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package handler

import (
"net/http"

"challenge2016/dto"
"challenge2016/models"
"challenge2016/store"

"github.com/gin-gonic/gin"
)

type Distributor struct {
store store.Distributors
}

// NewDistributor returns a new instance of Distributor.
func NewDistributor(store store.Distributors) *Distributor {
return &Distributor{
store: store,
}
}

// Get returns the distributor.
func (d *Distributor) Get(c *gin.Context) {
name := c.Param("name")
dist, err := d.store.Get(name)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
return
}

if dist == nil {
c.JSON(http.StatusNotFound, gin.H{"error": "distributor not found"})
return
}

c.JSON(http.StatusOK, dto.ModelToDTO(*dist))
}

// Insert inserts a new distributor.
func (d *Distributor) Insert(c *gin.Context) {
var distributor dto.Distributor

err := c.BindJSON(&distributor)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
}

var includes, excludes []models.Region

for _, region := range distributor.IncludeRegions {
includes = append(includes, models.Parse(region))
}

for _, region := range distributor.ExcludedRegions {
excludes = append(excludes, models.Parse(region))
}

model := models.Distributor{
Name: distributor.Name,
IncludeRegions: includes,
ExcludedRegions: excludes,
}

err = d.store.Insert(model, distributor.Parent)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

created, err := d.store.Get(distributor.Name)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

if created == nil {
c.JSON(http.StatusNotFound, gin.H{"error": "distributor not found"})
return
}

c.JSON(http.StatusCreated, dto.ModelToDTO(*created))
}

// Authorization checks whether a distributor is allowed to access a region.
func (d *Distributor) Authorization(c *gin.Context) {
distributor := c.Param("name")

var region dto.Region

err := c.BindJSON(&region)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

allowed, err := d.store.CheckAuthorization(distributor, models.Parse(region.Region))
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

c.JSON(http.StatusOK, gin.H{"allowed": allowed})
}
34 changes: 34 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"log/slog"

"challenge2016/csv"
"challenge2016/handler"
"challenge2016/store"

"github.com/gin-gonic/gin"
)

func main() {
geography, err := csv.Load("./cities.csv")
if err != nil {
panic(err)
}

distStore := store.NewDistributors(geography)

distributorHandler := handler.NewDistributor(distStore)

router := gin.Default()

router.GET("/distributors/:name", distributorHandler.Get)
router.POST("/distributors", distributorHandler.Insert)
router.POST("/distributors/:name/authorization", distributorHandler.Authorization)

err = router.Run(":8080")
if err != nil {
slog.Error(err.Error())
return
}
}
Loading