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

feat: Add Ground Control #32

Merged
merged 9 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ require (

)

require github.com/spf13/viper v1.19.0
require (
github.com/spf13/viper v1.19.0
github.com/stretchr/testify v1.9.0
)

require (
cloud.google.com/go v0.112.1 // indirect
Expand Down Expand Up @@ -346,7 +349,6 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
github.com/stefanberger/go-pkcs11uri v0.0.0-20230803200340-78284954bff6 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/swaggo/files v0.0.0-20220610200504-28940afbdbfe // indirect
github.com/swaggo/http-swagger v1.3.4 // indirect
Expand Down
46 changes: 46 additions & 0 deletions ground-control/.air.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"

[build]
args_bin = []
bin = "./main"
cmd = "make build"
delay = 1000
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
exclude_file = []
exclude_regex = ["_test.go"]
exclude_unchanged = false
follow_symlink = false
full_bin = ""
include_dir = []
include_ext = ["go", "tpl", "tmpl", "html"]
include_file = []
kill_delay = "0s"
log = "build-errors.log"
poll = false
poll_interval = 0
post_cmd = []
pre_cmd = []
rerun = false
rerun_delay = 500
send_interrupt = false
stop_on_error = false

[color]
app = ""
build = "yellow"
main = "magenta"
runner = "green"
watcher = "cyan"

[log]
main_only = false
time = false

[misc]
clean_on_exit = false

[screen]
clear_on_rebuild = false
keep_scroll = true
29 changes: 29 additions & 0 deletions ground-control/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# 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

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work
tmp/

# IDE specific files
.vscode
.idea

# .env file
.env

# Project build
main
18 changes: 18 additions & 0 deletions ground-control/cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"fmt"

"container-registry.com/harbor-satellite/ground-control/internal/server"
_ "github.com/joho/godotenv/autoload"
)

func main() {
server := server.NewServer()

fmt.Printf("Ground Control running on port: %s\n", server.Addr)
err := server.ListenAndServe()
if err != nil {
panic(fmt.Sprintf("cannot start server: %s", err))
}
}
18 changes: 18 additions & 0 deletions ground-control/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module container-registry.com/harbor-satellite/ground-control

go 1.22.4

require (
github.com/gorilla/mux v1.8.1
github.com/jackc/pgx/v5 v5.6.0
github.com/joho/godotenv v1.5.1
)

require (
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/text v0.14.0 // indirect
)
38 changes: 38 additions & 0 deletions ground-control/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
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/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
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=
gorm.io/gorm v1.25.10 h1:dQpO+33KalOA+aFYGlK+EfxcI5MbO7EP2yYygwh9h+s=
gorm.io/gorm v1.25.10/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
60 changes: 60 additions & 0 deletions ground-control/internal/database/database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package database

import (
"context"
"database/sql"
"fmt"
"log"
"os"
"time"

_ "github.com/jackc/pgx/v5/stdlib"
_ "github.com/joho/godotenv/autoload"
)

type Service interface {
Health() map[string]string
}

type service struct {
db *sql.DB
}

var (
database = os.Getenv("DB_DATABASE")
password = os.Getenv("DB_PASSWORD")
username = os.Getenv("DB_USERNAME")
port = os.Getenv("DB_PORT")
host = os.Getenv("DB_HOST")
dbInstance *service
)

func New() Service {
// Reuse Connection
if dbInstance != nil {
return dbInstance
}
connStr := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable", username, password, host, port, database)
db, err := sql.Open("pgx", connStr)
if err != nil {
log.Fatal(err)
}
bupd marked this conversation as resolved.
Show resolved Hide resolved
dbInstance = &service{
db: db,
}
return dbInstance
}

func (s *service) Health() map[string]string {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()

err := s.db.PingContext(ctx)
if err != nil {
log.Fatalf(fmt.Sprintf("db down: %v", err))
}
bupd marked this conversation as resolved.
Show resolved Hide resolved

return map[string]string{
"message": "It's healthy",
}
}
41 changes: 41 additions & 0 deletions ground-control/internal/server/routes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package server

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

"github.com/gorilla/mux"
)

func (s *Server) RegisterRoutes() http.Handler {
r := mux.NewRouter()

r.HandleFunc("/", s.HelloWorldHandler)

r.HandleFunc("/health", s.healthHandler)

return r
}

func (s *Server) HelloWorldHandler(w http.ResponseWriter, r *http.Request) {
resp := make(map[string]string)
resp["message"] = "Hello World"

jsonResp, err := json.Marshal(resp)
if err != nil {
log.Fatalf("error handling JSON marshal. Err: %v", err)
}

_, _ = w.Write(jsonResp)
}
bupd marked this conversation as resolved.
Show resolved Hide resolved

func (s *Server) healthHandler(w http.ResponseWriter, r *http.Request) {
jsonResp, err := json.Marshal(s.db.Health())

if err != nil {
log.Fatalf("error handling JSON marshal. Err: %v", err)
}

_, _ = w.Write(jsonResp)
bupd marked this conversation as resolved.
Show resolved Hide resolved
}
39 changes: 39 additions & 0 deletions ground-control/internal/server/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package server

import (
"fmt"
"net/http"
"os"
"strconv"
"time"

_ "github.com/joho/godotenv/autoload"

"container-registry.com/harbor-satellite/ground-control/internal/database"
)

type Server struct {
port int

db database.Service
}

func NewServer() *http.Server {
port, _ := strconv.Atoi(os.Getenv("PORT"))
bupd marked this conversation as resolved.
Show resolved Hide resolved
NewServer := &Server{
port: port,

db: database.New(),
}

// Declare Server config
server := &http.Server{
Addr: fmt.Sprintf(":%d", NewServer.port),
Handler: NewServer.RegisterRoutes(),
IdleTimeout: time.Minute,
ReadTimeout: 10 * time.Second,
WriteTimeout: 30 * time.Second,
}

return server
}
18 changes: 18 additions & 0 deletions ground-control/sql/schema/001_groups.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-- +goose Up

CREATE TABLE groups (
id SERIAL PRIMARY KEY,
group_name VARCHAR(255) NOT NULL,
username VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
bupd marked this conversation as resolved.
Show resolved Hide resolved

INSERT INTO groups (group_name, username, password) VALUES ('admin_group', 'admin_user', 'admin_pass');
INSERT INTO groups (group_name, username, password) VALUES ('dev_group', 'dev_user', 'dev_pass');
INSERT INTO groups (group_name, username, password) VALUES ('test_group', 'test_user', 'test_pass');
bupd marked this conversation as resolved.
Show resolved Hide resolved

-- +goose Down
DROP TABLE groups;