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 3 commits
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
12 changes: 11 additions & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
HARBOR_USERNAME=admin
HARBOR_PASSWORD=Harbor12345
ZOT_URL="127.0.0.1:8585"
ZOT_URL="127.0.0.1:8585"

PORT=8080
APP_ENV=local

DB_HOST=localhost
DB_PORT=5432
DB_DATABASE=groundcontrol
# Customize user and pass based on your config
DB_USERNAME=postgres
DB_PASSWORD=password
10 changes: 6 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ require (

)

require github.com/spf13/viper v1.19.0
require (
github.com/gorilla/mux v1.8.1
github.com/lib/pq v1.10.9
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 @@ -205,7 +210,6 @@ require (
github.com/google/wire v0.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.3 // indirect
github.com/gorilla/mux v1.8.1 // indirect
github.com/gorilla/schema v1.2.0 // indirect
github.com/gorilla/securecookie v1.1.2 // indirect
github.com/gorilla/sessions v1.2.2 // indirect
Expand Down Expand Up @@ -248,7 +252,6 @@ require (
github.com/liamg/iamgo v0.0.9 // indirect
github.com/liamg/jfather v0.0.7 // indirect
github.com/liamg/memoryfs v1.6.0 // indirect
github.com/lib/pq v1.10.9 // indirect
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
github.com/lunixbochs/struc v0.0.0-20200707160740-784aaebc1d40 // indirect
github.com/magiconair/properties v1.8.7 // 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
31 changes: 31 additions & 0 deletions ground-control/internal/database/db.go

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

81 changes: 81 additions & 0 deletions ground-control/internal/database/groups.sql.go

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

18 changes: 18 additions & 0 deletions ground-control/internal/database/models.go

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

74 changes: 74 additions & 0 deletions ground-control/internal/server/handlers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package server

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

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

type CreateGroupRequest struct {
GroupName string `json:"group_name"`
Username string `json:"username"`
Password string `json:"password"`
}

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) {
err := s.db.Ping()
if err != nil {
log.Fatalf("error pinging db: %v", err)
}

jsonResp, err := json.Marshal(map[string]string{"status": "healthy"})
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) createGroupHandler(w http.ResponseWriter, r *http.Request) {
// Decode request body
var req CreateGroupRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

params := database.CreateGroupParams{
GroupName: req.GroupName,
Username: req.Username,
Password: req.Password,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}

// Call the database query
result, err := s.dbQueries.CreateGroup(context.Background(), params)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

jsonResp, err := json.Marshal(result)
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
17 changes: 17 additions & 0 deletions ground-control/internal/server/routes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package server

import (
"net/http"

"github.com/gorilla/mux"
)

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

r.HandleFunc("/", s.HelloWorldHandler).Methods("GET")
r.HandleFunc("/health", s.healthHandler).Methods("GET")
r.HandleFunc("/group", s.createGroupHandler).Methods("POST")

return r
}
71 changes: 71 additions & 0 deletions ground-control/internal/server/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package server

import (
"database/sql"
"fmt"
"log"
"net/http"
"os"
"strconv"
"time"

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

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

type Server struct {
port int
db *sql.DB
dbQueries *database.Queries
}

var (
dbName = os.Getenv("DB_DATABASE")
password = os.Getenv("DB_PASSWORD")
username = os.Getenv("DB_USERNAME")
PORT = os.Getenv("DB_PORT")
HOST = os.Getenv("DB_HOST")
)

func NewServer() *http.Server {
port, err := strconv.Atoi(os.Getenv("PORT"))
if err != nil {
log.Fatalf("PORT is not valid: %v", err)
}

connStr := fmt.Sprintf(
"postgres://%s:%s@%s:%s/%s?sslmode=disable",
username,
password,
HOST,
PORT,
dbName,
)

db, err := sql.Open("postgres", connStr)
if err != nil {
log.Fatalf("Error in sql: %v", err)
}


dbQueries := database.New(db)

NewServer := &Server{
port: port,
db: db,
dbQueries: dbQueries,
}

// 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/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))
}
bupd marked this conversation as resolved.
Show resolved Hide resolved
}
7 changes: 7 additions & 0 deletions ground-control/seed/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "fmt"

func main() {
fmt.Println("seeding the database.")
}
7 changes: 7 additions & 0 deletions ground-control/sql/queries/groups.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- name: CreateGroup :one
INSERT INTO groups (id, group_name, username, password, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, $6)
RETURNING *;

-- name: GetGroups :many
SELECT * FROM groups;
Loading