Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
33 changes: 18 additions & 15 deletions api/cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,52 +63,55 @@ func main() {
} else {
slog.Info("defaulting to server development mode")
}
var err error

Copy link
Copy Markdown
Collaborator Author

@anmho anmho Dec 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reordered load and parse steps for env.

if opts.Stage == Development {
// Load .env
logger.Info("loading .env")
err := godotenv.Load(".env")
if err != nil {
logger.Error("loading env", slog.Any("error", err))
os.Exit(1)
}
}

// Parse env into config
var err error
var config Config
err = env.Parse(&config)
if err != nil {
slog.Error("parsing env to config", slog.Any("error", err))
logger.Error("parsing env to config", slog.Any("error", err))
os.Exit(1)
}

logger.Info("config: ", slog.Any("config", config))
logger.Info("huma options: ", slog.Any("options", opts))

// Create DB_URL
dbURL := fmt.Sprintf("postgres://%s:%s@%s:%s/%s", config.DbUser, config.DbPass, config.DbHost, config.DbPort, config.DbName)

if opts.Stage == Development {
// Load .env
err := godotenv.Load(".env")
if err != nil {
slog.Error("loading env", slog.Any("error", err))
os.Exit(1)
}
}

logger.Info("setting clerk secret key from environment config")
clerk.SetKey(config.ClerkSecretKey)

// Setup Dependencies
// Postgres
db, err := sql.Open("pgx", dbURL)
if err != nil {
slog.Error("opening database", slog.Any("error", err))
logger.Error("opening database", slog.Any("error", err))
os.Exit(1)
}
logger.Info("pinging db")

dbctx, cancel := context.WithTimeout(ctx, time.Second*5)
defer cancel()
if err := db.PingContext(dbctx); err != nil {
slog.Error("pinging db", slog.Any("error", err))
logger.Error("pinging db", slog.Any("error", err))
os.Exit(1)
}
logger.Info("successfully pinged db")

cfg, err := awsConfig.LoadDefaultConfig(ctx)
if err != nil {
slog.Error("loading default aws config", slog.Any("error", err))
logger.Error("loading default aws config", slog.Any("error", err))
os.Exit(1)
}

Expand All @@ -130,7 +133,7 @@ func main() {
logger.Info("shutting down server")
os.Exit(0)
} else {
slog.Error("unexpected error", slog.Any("error", err))
logger.Error("unexpected error", slog.Any("error", err))
os.Exit(1)
}
}
Expand Down
65 changes: 64 additions & 1 deletion api/openapi.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,45 @@
components:
schemas:
CreateUploadURLBody:
additionalProperties: false
properties:
$schema:
description: A URL to the JSON Schema for this object.
examples:
- https://example.com/schemas/CreateUploadURLBody.json
format: uri
readOnly: true
type: string
method:
type: string
signed_headers:
additionalProperties:
type: string
type: object
upload_url:
type: string
required:
- method
- upload_url
- signed_headers
type: object
CreateUploadURLRequestBody:
additionalProperties: false
properties:
$schema:
description: A URL to the JSON Schema for this object.
examples:
- https://example.com/schemas/CreateUploadURLRequestBody.json
format: uri
readOnly: true
type: string
image_key:
examples:
- my-image.jpeg
type: string
required:
- image_key
type: object
ErrorDetail:
additionalProperties: false
properties:
Expand Down Expand Up @@ -83,10 +123,33 @@ components:
scheme: bearer
type: http
info:
title: My API
title: Happened API
version: 1.0.0
openapi: 3.1.0
paths:
/create-upload-url:
get:
operationId: get-create-upload-url
requestBody:
content:
application/json:
schema:
$ref: "#/components/schemas/CreateUploadURLRequestBody"
required: true
responses:
"200":
content:
application/json:
schema:
$ref: "#/components/schemas/CreateUploadURLBody"
description: OK
default:
content:
application/problem+json:
schema:
$ref: "#/components/schemas/ErrorModel"
description: Error
summary: Get create upload URL
/greeting/protected/{name}:
get:
description: Protected version of greet
Expand Down
57 changes: 56 additions & 1 deletion client-v2/gen/openapi.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,41 @@
/**
* Generated by orval v7.2.0 🍺
* Do not edit manually.
* My API
* Happened API
* OpenAPI spec version: 1.0.0
*/
import axios from 'axios'
import type {
AxiosRequestConfig,
AxiosResponse
} from 'axios'

// https://stackoverflow.com/questions/49579094/typescript-conditional-types-filter-out-readonly-properties-pick-only-requir/49579497#49579497
type IfEquals<X, Y, A = X, B = never> = (<T>() => T extends X ? 1 : 2) extends <
T,
>() => T extends Y ? 1 : 2
? A
: B;

type WritableKeys<T> = {
[P in keyof T]-?: IfEquals<
{ [Q in P]: T[P] },
{ -readonly [Q in P]: T[P] },
P
>;
}[keyof T];

type UnionToIntersection<U> =
(U extends any ? (k: U)=>void : never) extends ((k: infer I)=>void) ? I : never;
type DistributeReadOnlyOverUnions<T> = T extends any ? NonReadonly<T> : never;

type Writable<T> = Pick<T, WritableKeys<T>>;
type NonReadonly<T> = [T] extends [UnionToIntersection<T>] ? {
[P in keyof Writable<T>]: T[P] extends object
? NonReadonly<NonNullable<T[P]>>
: T[P];
} : DistributeReadOnlyOverUnions<T>;

export interface GreetingOutputBody {
/** A URL to the JSON Schema for this object. */
readonly $schema?: string;
Expand Down Expand Up @@ -47,11 +74,38 @@ export interface ErrorModel {
type?: string;
}

export interface CreateUploadURLRequestBody {
/** A URL to the JSON Schema for this object. */
readonly $schema?: string;
image_key: string;
}

export type CreateUploadURLBodySignedHeaders = {[key: string]: string};

export interface CreateUploadURLBody {
/** A URL to the JSON Schema for this object. */
readonly $schema?: string;
method: string;
signed_headers: CreateUploadURLBodySignedHeaders;
upload_url: string;
}





/**
* @summary Get create upload URL
*/
export const getCreateUploadUrl = <TData = AxiosResponse<CreateUploadURLBody>>(
createUploadURLRequestBody: NonReadonly<CreateUploadURLRequestBody>, options?: AxiosRequestConfig
): Promise<TData> => {
return axios.get(
`/create-upload-url`,options
);
}

/**
* Protected version of greet
* @summary Get a protected greeting
*/
Expand All @@ -75,5 +129,6 @@ export const getGreeting = <TData = AxiosResponse<GreetingOutputBody>>(
);
}

export type GetCreateUploadUrlResult = AxiosResponse<CreateUploadURLBody>
export type ProtectedGreetResult = AxiosResponse<GreetingOutputBody>
export type GetGreetingResult = AxiosResponse<GreetingOutputBody>