This is my link shortener server using mux framework, postgres database, and prometheus + grafana bundle to display metrics
func (i *Implementation) RedirectToUrl(w http.ResponseWriter, r *http.Request)You can access this handler by using localhost:8080/{shorturl} and you will be provided to your short url address
func (i *Implementation) AddNewUrl(w http.ResponseWriter, r *http.Request)This handler allow you to add new link to postgres database, you can use it by routing localhost:8080/add?url={your_long_url}
When you starting up server for the first time it will create a new database with this scheme:
CREATE TABLE IF NOT EXISTS url(
id SERIAL PRIMARY KEY not null,
longurl VARCHAR(255) not null,
shorturl VARCHAR(255),
status VARCHAR(255)
);Next function will create first start link which is http://yandex.ru
func (r *repository) AddStartLink(ctx context.Context) error This is an example of a ready-made Postgresql database schema
This is a gorutine system which checking status of url's every 10 minutes. It starts by sending an empty struct to special channel.
func startStatus(chStart chan<- struct{}, chDone <-chan struct{}) {
go func() {
for {
select {
case <-chDone:
chStart <- struct{}{}
default:
time.Sleep(10 * time.Minute)
}
}
}()
}The status system takes 300 links, which are ordered by url.id.
Prometheus is scraping every 10 minutes (btw you can change it in prometheus -> prometheus.yml which is config) every handlers using, and also it scrap count of redirects. In docker-compose there is also grafana for visualization metrics, but you can use localhost:8080/metrics for checking it out.
You can also use pgadmin for administrating your db. It starts on 5050 port.
docker-compose starts a few images (postgres, server, pgadmin, grafana and prometheus), all dependenses you can find at docker-compose file. Keep on mind that while docker creating new images and volume for db it may take a while and this can take up a lot of space on your hard drive.
I used to connect to db by pgx.pool, so it creates a pool of connections.
func NewClient(ctx context.Context, cfg config.Storage) (pool *pgxpool.Pool, err error)this function makes 5 attempts to create a pool every 5 seconds. There is a problem that db could start after server, but if function will be out of attempts it will shut down because of panic using nil pointer in querys, but it will restart, because of docker-compose restart: on-failure.
- Clone repository
- run
docker-compose up -din terminal.