Skip to content

Commit

Permalink
feat: implement boilerplate code for http API
Browse files Browse the repository at this point in the history
  • Loading branch information
STommydx committed Feb 21, 2024
1 parent 115221b commit 694a531
Show file tree
Hide file tree
Showing 32 changed files with 1,838 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/go.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Go Test

on:
push:

jobs:

test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.21

- name: Build
run: go build -v ./...

- name: Test
run: go test -v ./...
29 changes: 29 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Created by https://www.toptal.com/developers/gitignore/api/go
# Edit at https://www.toptal.com/developers/gitignore?templates=go

### Go ###
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# 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

# End of https://www.toptal.com/developers/gitignore/api/go

.envrc
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Thomas Li

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
21 changes: 21 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package app

import (
"github.com/STommydx/FolioForge/config"
"github.com/STommydx/FolioForge/db"
"github.com/STommydx/FolioForge/healthz"
"github.com/STommydx/FolioForge/http"
"github.com/STommydx/FolioForge/logger"
"go.uber.org/fx"
)

func New() *fx.App {
app := fx.New(
fx.Provide(config.NewAppConfigFromEnv),
logger.Module,
http.Module,
healthz.Module,
db.Module,
)
return app
}
20 changes: 20 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cmd

import (
"os"

"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
Use: "FolioForge",
Short: "API toolkit designed to streamline the management and presentation of your professional portfolio",
Long: `FolioForge is an API toolkit designed to streamline the management and presentation of your professional portfolio. With FolioForge, you can easily update your experience, projects, and skills through its intuitive CRUD endpoints. Plus, it seamlessly generates a polished resume based on your data and a provided LaTeX template, empowering you to showcase your qualifications with confidence.`,
}

func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
20 changes: 20 additions & 0 deletions cmd/serve.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cmd

import (
"github.com/STommydx/FolioForge/app"
"github.com/spf13/cobra"
)

var serveCmd = &cobra.Command{
Use: "serve",
Short: "Start the API server",
Long: `Start the API server.`,
Run: func(cmd *cobra.Command, args []string) {
mainApp := app.New()
mainApp.Run()
},
}

func init() {
rootCmd.AddCommand(serveCmd)
}
20 changes: 20 additions & 0 deletions cmd/worker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

var workerCmd = &cobra.Command{
Use: "worker",
Short: "Start the worker",
Long: `Start the worker.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("worker called")
},
}

func init() {
rootCmd.AddCommand(workerCmd)
}
27 changes: 27 additions & 0 deletions config/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package config

import (
"context"

"github.com/STommydx/FolioForge/db"
"github.com/STommydx/FolioForge/http"
"github.com/STommydx/FolioForge/logger"
"github.com/sethvargo/go-envconfig"
"go.uber.org/fx"
)

type AppConfig struct {
fx.Out
Postgres *db.Config `env:", prefix=POSTGRES_"`
Logger *logger.Config `env:", prefix=LOG_"`
Api *http.Config `env:", prefix=API_"`
}

func NewAppConfigFromEnv() (AppConfig, error) {
ctx := context.Background()
config := AppConfig{}
if err := envconfig.Process(ctx, &config); err != nil {
return config, err
}
return config, nil
}
24 changes: 24 additions & 0 deletions db/atlas.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
data "external_schema" "gorm" {
program = [
"go",
"run",
"-mod=mod",
"ariga.io/atlas-provider-gorm",
"load",
"--path", ".",
"--dialect", "postgres", // | postgres | sqlite | sqlserver
]
}

env "gorm" {
src = data.external_schema.gorm.url
dev = "docker://postgres/15/dev"
migration {
dir = "file://migrations"
}
format {
migrate {
diff = "{{ sql . \" \" }}"
}
}
}
31 changes: 31 additions & 0 deletions db/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package db

import (
"fmt"

"gorm.io/driver/postgres"
"gorm.io/gorm"
)

type Config struct {
Host string `env:"HOST, required"`
Port string `env:"PORT, default=5432"`
Username string `env:"USER, required"`
Password string `env:"PASSWORD, required"`
Database string `env:"DB, default=folioforge"`
}

func NewPostgresDB(config *Config) (*gorm.DB, error) {
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s sslmode=disable",
config.Host,
config.Username,
config.Password,
config.Database,
)
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
return nil, err
}
return db, nil

}
14 changes: 14 additions & 0 deletions db/migrations/20240220070657.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- Create "profiles" table
CREATE TABLE "public"."profiles" (
"id" bytea NOT NULL,
"profile_name" text NULL,
"first_name" text NULL,
"last_name" text NULL,
"email" text NULL,
"phone" text NULL,
"created_at" timestamptz NULL,
"updated_at" timestamptz NULL,
PRIMARY KEY ("id")
);
-- Create index "idx_profiles_profile_name" to table: "profiles"
CREATE UNIQUE INDEX "idx_profiles_profile_name" ON "public"."profiles" ("profile_name");
2 changes: 2 additions & 0 deletions db/migrations/atlas.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
h1:0t0ZkOqmdPLmosv0/2YxWuzQ5gVwLH0NYA0vLMwVQDk=
20240220070657.sql h1:Oh11EPLob5cZLq9goyJ5i5psPiG2xdV8PE665t29y/Q=
19 changes: 19 additions & 0 deletions db/models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package db

import (
"time"

_ "ariga.io/atlas-provider-gorm/gormschema"
"github.com/oklog/ulid/v2"
)

type Profile struct {
ID ulid.ULID `gorm:"primaryKey"`
ProfileName string `gorm:"uniqueIndex"`
FirstName string
LastName string
Email string
Phone string
CreatedAt time.Time
UpdatedAt time.Time
}
7 changes: 7 additions & 0 deletions db/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package db

import "go.uber.org/fx"

var Module = fx.Options(
fx.Provide(NewPostgresDB),
)
11 changes: 11 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: '3'

services:
postgresql:
image: postgres:15
ports:
- "5432:5432"
environment:
POSTGRES_USER: folioforge
POSTGRES_PASSWORD: folioforge
POSTGRES_DB: folioforge
Loading

0 comments on commit 694a531

Please sign in to comment.