Skip to content

Commit

Permalink
Add postgres storage
Browse files Browse the repository at this point in the history
  • Loading branch information
heppu committed Mar 1, 2024
1 parent 5e2ac90 commit 2ef8639
Show file tree
Hide file tree
Showing 11 changed files with 1,429 additions and 190 deletions.
22 changes: 17 additions & 5 deletions api/api.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package api

import (
"fmt"
"encoding/json"
"log/slog"
"net/http"

"github.com/gevulotnetwork/devnet-explorer/model"
"github.com/julienschmidt/httprouter"
)

type Store interface {
Hello() string
Stats() (model.Stats, error)
}

type API struct {
Expand All @@ -30,9 +32,19 @@ func (a *API) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

func (a *API) bind() {
a.r.GET("/", a.helloWorld)
a.r.GET("/api/v1/stat", a.stats)
}

func (a *API) helloWorld(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprint(w, a.s.Hello())
func (a *API) stats(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
stats, err := a.s.Stats()
if err != nil {
slog.Error("failed to get stats", slog.Any("error", err))
http.Error(w, "failed to get stats", http.StatusInternalServerError)
return
}

if err := json.NewEncoder(w).Encode(stats); err != nil {
slog.Error("failed encode stats", slog.Any("error", err))
return
}
}
25 changes: 20 additions & 5 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package app
import (
"context"
"errors"
"fmt"
"log/slog"
"net/http"
"os"
Expand All @@ -14,12 +15,18 @@ import (
)

// Run starts the application and listens for OS signals to gracefully shutdown.
func Run() error {
conf := ParseConfig()
func Run(args ...string) error {
conf := ParseConfig(args...)

s, err := store.New(conf.DSN)
if err != nil {
return fmt.Errorf("failed to create store: %w", err)
}

sigInt := make(chan os.Signal, 1)
srv := &http.Server{
Addr: conf.ServerListenAddr,
Handler: api.New(store.New()),
Handler: api.New(s),
}

r := NewRunner()
Expand Down Expand Up @@ -53,13 +60,21 @@ func Run() error {

type Config struct {
ServerListenAddr string
DSN string
}

func ParseConfig() Config {
func ParseConfig(args ...string) Config {
addr := os.Getenv("SERVER_LISTEN_ADDR")
if addr == "" {
addr = "127.0.0.1:8383"
}
dsn := os.Getenv("DSN")
if dsn == "" {
dsn = "postgres://gevulot:gevulot@localhost:5432/gevulot"
}

return Config{ServerListenAddr: addr}
return Config{
ServerListenAddr: addr,
DSN: dsn,
}
}
16 changes: 16 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: '3'

services:
db:
image: 'docker.io/library/postgres:16-alpine'
ports:
- 5432:5432
environment:
- POSTGRES_USER=gevulot
- POSTGRES_PASSWORD=gevulot
- POSTGRES_DB=gevulot
healthcheck:
test: ["CMD-SHELL", "pg_isready -U gevulot -d gevulot"]
interval: 1s
timeout: 2s
retries: 30
Loading

0 comments on commit 2ef8639

Please sign in to comment.