Skip to content

Commit f2a39d6

Browse files
committed
Added main.go for the api, added config example for api and added more data for the env example, fixed versioning
1 parent d875be2 commit f2a39d6

14 files changed

Lines changed: 335 additions & 19 deletions

File tree

.env.example

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
11
# Environment example use your own values
2+
# just do not use these values unless you are testing or developing
23
DB_HOST=127.0.0.1
34
DB_PORT=5432
4-
DB_USER=postgres
5+
DB_USER=writer
56
DB_SSLMODE=disable
67
DB_PASSWORD=12345678
7-
DB_NAME=gnoland
8+
DB_NAME=gnoland
9+
10+
# Envs for the API
11+
# If you plan to use the API than use this values
12+
API_DB_HOST=127.0.0.1
13+
API_DB_PORT=5432
14+
API_DB_USER=reader
15+
API_DB_SSLMODE=disable
16+
API_DB_PASSWORD=12345678
17+
API_DB_NAME=gnoland
18+
19+
API_DB_POOL_MAX_CONNS=50
20+
API_DB_POOL_MIN_CONNS=10
21+
API_DB_POOL_MAX_CONN_LIFETIME=10s
22+
API_DB_POOL_MAX_CONN_IDLE_TIME=5m
23+
API_DB_POOL_HEALTH_CHECK_PERIOD=1m
24+
API_DB_POOL_MAX_CONN_LIFETIME_JITTER=1m

.github/workflows/release.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ jobs:
4040
run: |
4141
mkdir -p build
4242
43+
# Get git commit hash
44+
GIT_COMMIT=$(git rev-parse --short HEAD)
45+
4346
# Linux AMD64
44-
GOOS=linux GOARCH=amd64 go build -ldflags="-X main.Version=${{ steps.version.outputs.version }}" -o build/indexer indexer/main.go
47+
GOOS=linux GOARCH=amd64 go build -ldflags="-X main.Commit=${GIT_COMMIT} -X main.Version=${{ steps.version.outputs.version }}" -o build/indexer-linux-amd64 indexer/main.go
4548
4649
- name: Create GitHub Release
4750
uses: softprops/action-gh-release@v1

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@ go.work.sum
3737
config.yml
3838
test_config.yml
3939
.env
40+
config-api.yml
4041

4142
# State dumps and diagnostics directories
4243
state_dumps/
4344
diagnostics/
45+
46+
# Cert files
47+
server.crt
48+
server.key

Dockerfile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ WORKDIR /app
44

55
COPY . .
66

7-
RUN go build -o indexer indexer/main.go
8-
9-
RUN chmod +x indexer
7+
ARG VERSION=unknown
8+
RUN GIT_COMMIT=$(git rev-parse --short HEAD 2>/dev/null || echo "unknown") && \
9+
go build -ldflags="-X main.Commit=${GIT_COMMIT} -X main.Version=${VERSION}" -o indexer indexer/main.go && \
10+
chmod +x indexer
1011

1112
FROM debian:trixie-slim
1213

Makefile

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
1-
.PHONY: build install clean build-experimental install-experimental
1+
.PHONY: build install clean build-experimental install-experimental build-api
2+
3+
# Get git information
4+
GIT_COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
5+
GIT_TAG := $(shell git describe --tags --exact-match 2>/dev/null || echo "")
6+
VERSION := $(if $(GIT_TAG),$(GIT_TAG),dev-$(GIT_COMMIT))
27

38
build:
49
mkdir -p build
5-
go build -o build/indexer indexer/main.go
10+
go build -ldflags="-X main.Commit=$(GIT_COMMIT) -X main.Version=$(VERSION)" -o build/indexer indexer/main.go
611

712
install:
8-
cd indexer && go install ./...
13+
cd indexer && go install ./... -ldflags="-X main.Commit=$(GIT_COMMIT) -X main.Version=$(VERSION)"
14+
15+
build-api:
16+
mkdir -p build
17+
go build -ldflags="-X main.Commit=$(GIT_COMMIT) -X main.Version=$(VERSION)" -o build/api api/main.go
918

1019
clean:
1120
rm -rf build
1221

1322
# experimental build with greentea garbage collection
1423
# use at your own risk
1524
build-experimental:
16-
GOEXPERIMENT=greenteagc go build -o build/indexer-tea indexer/main.go
25+
GOEXPERIMENT=greenteagc go build -ldflags="-X main.Commit=$(GIT_COMMIT) -X main.Version=$(VERSION)" -o build/indexer-tea indexer/main.go
1726

1827
# experimental install with greentea garbage collection
1928
# use at your own risk
2029
install-experimental:
21-
cd indexer && GOEXPERIMENT=greenteagc go install ./...
30+
cd indexer && GOEXPERIMENT=greenteagc go install ./... -ldflags="-X main.Commit=$(GIT_COMMIT) -X main.Version=$(VERSION)"

api/config/loader.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package config
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
8+
"github.com/caarlos0/env/v6"
9+
"github.com/joho/godotenv"
10+
"go.yaml.in/yaml/v4"
11+
)
12+
13+
func LoadConfig(path string) (*ApiConfig, error) {
14+
yamlFile, err := os.ReadFile(path)
15+
if err != nil {
16+
return nil, err
17+
}
18+
var config ApiConfig
19+
err = yaml.Unmarshal(yamlFile, &config)
20+
if err != nil {
21+
return nil, err
22+
}
23+
// check if any of the fields are empty
24+
if config.Host == "" {
25+
config.Host = "localhost"
26+
}
27+
if config.Port == 0 {
28+
config.Port = 8080
29+
}
30+
// any cors method should be auto filled by the cors middleware
31+
// TODO: maybe add more options later
32+
return &config, nil
33+
}
34+
35+
func LoadEnvironment(path string) (*ApiEnv, error) {
36+
possibleEnvFiles := []string{
37+
".env", // accept only .env for now
38+
}
39+
// check if any of the files exist within the current path
40+
existingFiles := []string{}
41+
for _, envFile := range possibleEnvFiles {
42+
formPath := filepath.Join(path, envFile)
43+
if _, err := os.Stat(formPath); err == nil {
44+
existingFiles = append(existingFiles, formPath)
45+
}
46+
}
47+
// if there are multiple files, decide which has highest priority
48+
// 1. production , 2. development, 3. local, 4. default
49+
// only use the regular .env for now return to this laster
50+
if len(existingFiles) == 0 {
51+
fmt.Println("No environment file found. Searching for os environment variables.")
52+
} else if len(existingFiles) == 1 {
53+
absPath, err := filepath.Abs(existingFiles[0])
54+
if err != nil {
55+
return nil, err
56+
}
57+
err = godotenv.Load(absPath)
58+
if err != nil {
59+
return nil, fmt.Errorf("error loading .env file: %w", err)
60+
}
61+
fmt.Printf("Loaded environment variables from %s\n", absPath)
62+
}
63+
64+
environment := ApiEnv{}
65+
if err := env.Parse(&environment); err != nil {
66+
return nil, fmt.Errorf("failed to parse environment variables: %w", err)
67+
}
68+
69+
return &environment, nil
70+
}

api/config/types.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package config
2+
3+
import "time"
4+
5+
type ApiConfig struct {
6+
// Basic connection info
7+
Host string `yaml:"host"`
8+
Port int `yaml:"port"`
9+
// CORS config
10+
CorsAllowedOrigins []string `yaml:"cors_allowed_origins"`
11+
CorsAllowedMethods []string `yaml:"cors_allowed_methods"`
12+
CorsAllowedHeaders []string `yaml:"cors_allowed_headers"`
13+
CorsMaxAge int `yaml:"cors_max_age"`
14+
}
15+
16+
type ApiEnv struct {
17+
ApiDbHost string `env:"API_DB_HOST" envDefault:"localhost"`
18+
ApiDbPort int `env:"API_DB_PORT" envDefault:"5432"`
19+
ApiDbUser string `env:"API_DB_USER" envDefault:"postgres"`
20+
ApiDbPassword string `env:"API_DB_PASSWORD" envDefault:"12345678"`
21+
ApiDbName string `env:"API_DB_NAME" envDefault:"gnoland"`
22+
ApiDbSslmode string `env:"API_DB_SSLMODE" envDefault:"disable"`
23+
ApiDbPoolMaxConns int `env:"API_DB_POOL_MAX_CONNS" envDefault:"50"`
24+
ApiDbPoolMinConns int `env:"API_DB_POOL_MIN_CONNS" envDefault:"10"`
25+
ApiDbPoolMaxConnLifetime time.Duration `env:"API_DB_POOL_MAX_CONN_LIFETIME" envDefault:"10m"`
26+
ApiDbPoolMaxConnIdleTime time.Duration `env:"API_DB_POOL_MAX_CONN_IDLE_TIME" envDefault:"5m"`
27+
ApiDbPoolHealthCheckPeriod time.Duration `env:"API_DB_POOL_HEALTH_CHECK_PERIOD" envDefault:"1m"`
28+
ApiDbPoolMaxConnLifetimeJitter time.Duration `env:"API_DB_POOL_MAX_CONN_LIFETIME_JITTER" envDefault:"1m"`
29+
}

api/handlers/transactions.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"encoding/base64"
66
"fmt"
7-
"log"
87
"strings"
98

109
humatypes "github.com/Cogwheel-Validator/spectra-gnoland-indexer/api/huma-types"
@@ -27,9 +26,7 @@ func (h *TransactionsHandler) GetTransactionBasic(
2726
) (*humatypes.TransactionBasicGetOutput, error) {
2827
input.TxHash = strings.Trim(input.TxHash, " ")
2928
txHash, err := base64.URLEncoding.DecodeString(input.TxHash)
30-
log.Println("txHash base64url", input.TxHash)
3129
txHashBase64 := base64.StdEncoding.EncodeToString(txHash)
32-
log.Println("txHash base64", txHashBase64)
3330
if err != nil {
3431
return nil, huma.Error400BadRequest("Transaction hash is not valid base64url encoded", err)
3532
}
@@ -56,7 +53,6 @@ func (h *TransactionsHandler) GetTransactionMessage(
5653
if err != nil {
5754
return nil, huma.Error404NotFound(fmt.Sprintf("Transaction with hash %s not found", input.TxHash), err)
5855
}
59-
log.Println("msgType", msgType)
6056
switch msgType {
6157
case "bank_msg_send":
6258
data, err := h.db.GetBankSend(txHashBase64, h.chainName)

0 commit comments

Comments
 (0)