Skip to content

Commit

Permalink
feat: app skeleton, create endpoints
Browse files Browse the repository at this point in the history
The applications structure has been implemented. This also includes
setting up CI pipelines and integrating with swagger. As of now a single
route to create endpoints has been added.
  • Loading branch information
crazybolillo committed Jun 15, 2024
1 parent 7cfab64 commit 6924523
Show file tree
Hide file tree
Showing 17 changed files with 3,865 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/qa.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: qa
on:
push:
branches:
- 'main'
pull_request:
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.22'
- uses: golangci/golangci-lint-action@v5
with:
version: latest
sqlc:
runs-on: ubuntu-latest
steps:
- uses: sqlc-dev/setup-sqlc@v4
with:
sqlc-version: '1.21.0'
- name: Vet
run: sqlc vet
- name: Diff
run: sqlc diff
22 changes: 22 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Go workspace file
go.work
go.work.sum

# env file
.env

# IDEs
.idea/
4 changes: 4 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
linters-settings:
errcheck:
exclude-functions:
- (github.com/jackc/pgx/v5.Tx).Rollback
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.PHONY: swagger docs

docs:
swag init --generalInfo cmd/main.go --outputTypes=yaml

swagger:
docker run --detach -p 4000:8080 -e API_URL=/doc/swagger.yaml --mount 'type=bind,src=$(shell pwd)/docs,dst=/usr/share/nginx/html/doc' swaggerapi/swagger-ui
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# eryth
API to perform configuration management on Asterisk servers. Project named after
[Erythrina americana](https://mexico.inaturalist.org/taxa/201455-Erythrina-americana).
51 changes: 51 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package main

import (
"context"
"github.com/crazybolillo/eryth/internal/handler"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/cors"
"github.com/jackc/pgx/v5"
"log/slog"
"net/http"
"os"
"strings"
)

// @title Asterisk Administration API
// @version 1.0
// @description API to perform configuration management on Asterisk servers.
// @host localhost:8080
func main() {
os.Exit(run(context.Background()))
}

func run(ctx context.Context) int {
err := serve(ctx)
if err != nil {
return 1
}

return 0
}

func serve(ctx context.Context) error {
conn, err := pgx.Connect(ctx, os.Getenv("DATABASE_URL"))
if err != nil {
slog.Error("failed to establish database connection")
return err
}
defer conn.Close(ctx)

r := chi.NewRouter()
r.Use(cors.Handler(cors.Options{
AllowedOrigins: strings.Split(os.Getenv("CORS_ALLOWED_ORIGINS"), ","),
}))
r.Use(middleware.AllowContentEncoding("application/json"))

endpoint := handler.Endpoint{Conn: conn}
r.Mount("/endpoint", endpoint.Router())

return http.ListenAndServe(":8080", r)
}
47 changes: 47 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
definitions:
handler.createRequest:
properties:
codecs:
items:
type: string
type: array
context:
type: string
id:
type: string
max_contacts:
type: integer
password:
type: string
realm:
type: string
transport:
type: string
type: object
host: localhost:8080
info:
contact: {}
description: API to perform configuration management on Asterisk servers.
title: Asterisk Administration API
version: "1.0"
paths:
/endpoint:
post:
consumes:
- application/json
parameters:
- description: Endpoint's information
in: body
name: payload
required: true
schema:
$ref: '#/definitions/handler.createRequest'
responses:
"204":
description: No Content
"400":
description: Bad Request
"500":
description: Internal Server Error
summary: Create a new endpoint.
swagger: "2.0"
14 changes: 14 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module github.com/crazybolillo/eryth

go 1.22

require github.com/jackc/pgx/v5 v5.6.0

require (
github.com/go-chi/chi/v5 v5.0.12 // indirect
github.com/go-chi/cors v1.2.1 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
)
32 changes: 32 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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/go-chi/chi/v5 v5.0.12 h1:9euLV5sTrTNTRUU9POmDUvfxyj6LAABLUcEWO+JJb4s=
github.com/go-chi/chi/v5 v5.0.12/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-chi/cors v1.2.1 h1:xEC8UT3Rlp2QuWNEr4Fs/c2EAGVKBwy/1vHx3bppil4=
github.com/go-chi/cors v1.2.1/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.6.0 h1:SWJzexBzPL5jb0GEsrPMLIsi/3jOo7RHlzTjcAeDrPY=
github.com/jackc/pgx/v5 v5.6.0/go.mod h1:DNZ/vlrUnhWCoFGxHAG8U2ljioxukquj7utPDgtQdTw=
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
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/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.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
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=
17 changes: 17 additions & 0 deletions internal/db/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package db

import "github.com/jackc/pgx/v5/pgtype"

func Text(value string) pgtype.Text {
return pgtype.Text{
String: value,
Valid: true,
}
}

func Int4(value int32) pgtype.Int4 {
return pgtype.Int4{
Int32: value,
Valid: true,
}
}
104 changes: 104 additions & 0 deletions internal/handler/endpoint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package handler

import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"github.com/crazybolillo/eryth/internal/db"
"github.com/crazybolillo/eryth/internal/sqlc"
"github.com/go-chi/chi/v5"
"github.com/jackc/pgx/v5"
"net/http"
"strings"
)

type Endpoint struct {
*pgx.Conn
}

type createRequest struct {
ID string `json:"id"`
Password string `json:"password"`
Realm string `json:"realm,omitempty"`
Transport string `json:"transport"`
Context string `json:"context"`
Codecs []string `json:"codecs"`
MaxContacts int32 `json:"max_contacts,omitempty"`
}

func (e *Endpoint) Router() chi.Router {
r := chi.NewRouter()
r.Post("/", e.create)

return r
}

// @Summary Create a new endpoint.
// @Accept json
// @Param payload body createRequest true "Endpoint's information"
// @Success 204
// @Failure 400
// @Failure 500
// @Router /endpoint [post]
func (e *Endpoint) create(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
payload := createRequest{
Realm: "asterisk",
MaxContacts: 1,
}

err := decoder.Decode(&payload)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}

tx, err := e.Begin(r.Context())
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
defer tx.Rollback(r.Context())

queries := sqlc.New(tx)

hash := md5.Sum([]byte(payload.ID + ":" + payload.Realm + ":" + payload.Password))
err = queries.NewMD5Auth(r.Context(), sqlc.NewMD5AuthParams{
ID: payload.ID,
Username: db.Text(payload.ID),
Realm: db.Text(payload.Realm),
Md5Cred: db.Text(hex.EncodeToString(hash[:])),
})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}

err = queries.NewEndpoint(r.Context(), sqlc.NewEndpointParams{
ID: payload.ID,
Transport: db.Text(payload.Transport),
Context: db.Text(payload.Context),
Allow: db.Text(strings.Join(payload.Codecs, ",")),
})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}

err = queries.NewAOR(r.Context(), sqlc.NewAORParams{
ID: payload.ID,
MaxContacts: db.Int4(payload.MaxContacts),
})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}

err = tx.Commit(r.Context())
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}

w.WriteHeader(http.StatusNoContent)
}
32 changes: 32 additions & 0 deletions internal/sqlc/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6924523

Please sign in to comment.