Skip to content

Commit c234e4e

Browse files
committed
fix loading .env in local dev
1 parent 180477b commit c234e4e

File tree

3 files changed

+138
-17
lines changed

3 files changed

+138
-17
lines changed

api/cmd/api/main.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,52 +63,55 @@ func main() {
6363
} else {
6464
slog.Info("defaulting to server development mode")
6565
}
66-
var err error
66+
67+
if opts.Stage == Development {
68+
// Load .env
69+
logger.Info("loading .env")
70+
err := godotenv.Load(".env")
71+
if err != nil {
72+
logger.Error("loading env", slog.Any("error", err))
73+
os.Exit(1)
74+
}
75+
}
76+
6777
// Parse env into config
78+
var err error
6879
var config Config
6980
err = env.Parse(&config)
7081
if err != nil {
71-
slog.Error("parsing env to config", slog.Any("error", err))
82+
logger.Error("parsing env to config", slog.Any("error", err))
7283
os.Exit(1)
7384
}
7485

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

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

80-
if opts.Stage == Development {
81-
// Load .env
82-
err := godotenv.Load(".env")
83-
if err != nil {
84-
slog.Error("loading env", slog.Any("error", err))
85-
os.Exit(1)
86-
}
87-
}
88-
8992
logger.Info("setting clerk secret key from environment config")
9093
clerk.SetKey(config.ClerkSecretKey)
9194

9295
// Setup Dependencies
9396
// Postgres
9497
db, err := sql.Open("pgx", dbURL)
9598
if err != nil {
96-
slog.Error("opening database", slog.Any("error", err))
99+
logger.Error("opening database", slog.Any("error", err))
97100
os.Exit(1)
98101
}
99102
logger.Info("pinging db")
100103

101104
dbctx, cancel := context.WithTimeout(ctx, time.Second*5)
102105
defer cancel()
103106
if err := db.PingContext(dbctx); err != nil {
104-
slog.Error("pinging db", slog.Any("error", err))
107+
logger.Error("pinging db", slog.Any("error", err))
105108
os.Exit(1)
106109
}
107110
logger.Info("successfully pinged db")
108111

109112
cfg, err := awsConfig.LoadDefaultConfig(ctx)
110113
if err != nil {
111-
slog.Error("loading default aws config", slog.Any("error", err))
114+
logger.Error("loading default aws config", slog.Any("error", err))
112115
os.Exit(1)
113116
}
114117

@@ -130,7 +133,7 @@ func main() {
130133
logger.Info("shutting down server")
131134
os.Exit(0)
132135
} else {
133-
slog.Error("unexpected error", slog.Any("error", err))
136+
logger.Error("unexpected error", slog.Any("error", err))
134137
os.Exit(1)
135138
}
136139
}

api/openapi.yaml

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,45 @@
11
components:
22
schemas:
3+
CreateUploadURLBody:
4+
additionalProperties: false
5+
properties:
6+
$schema:
7+
description: A URL to the JSON Schema for this object.
8+
examples:
9+
- https://example.com/schemas/CreateUploadURLBody.json
10+
format: uri
11+
readOnly: true
12+
type: string
13+
method:
14+
type: string
15+
signed_headers:
16+
additionalProperties:
17+
type: string
18+
type: object
19+
upload_url:
20+
type: string
21+
required:
22+
- method
23+
- upload_url
24+
- signed_headers
25+
type: object
26+
CreateUploadURLRequestBody:
27+
additionalProperties: false
28+
properties:
29+
$schema:
30+
description: A URL to the JSON Schema for this object.
31+
examples:
32+
- https://example.com/schemas/CreateUploadURLRequestBody.json
33+
format: uri
34+
readOnly: true
35+
type: string
36+
image_key:
37+
examples:
38+
- my-image.jpeg
39+
type: string
40+
required:
41+
- image_key
42+
type: object
343
ErrorDetail:
444
additionalProperties: false
545
properties:
@@ -83,10 +123,33 @@ components:
83123
scheme: bearer
84124
type: http
85125
info:
86-
title: My API
126+
title: Happened API
87127
version: 1.0.0
88128
openapi: 3.1.0
89129
paths:
130+
/create-upload-url:
131+
get:
132+
operationId: get-create-upload-url
133+
requestBody:
134+
content:
135+
application/json:
136+
schema:
137+
$ref: "#/components/schemas/CreateUploadURLRequestBody"
138+
required: true
139+
responses:
140+
"200":
141+
content:
142+
application/json:
143+
schema:
144+
$ref: "#/components/schemas/CreateUploadURLBody"
145+
description: OK
146+
default:
147+
content:
148+
application/problem+json:
149+
schema:
150+
$ref: "#/components/schemas/ErrorModel"
151+
description: Error
152+
summary: Get create upload URL
90153
/greeting/protected/{name}:
91154
get:
92155
description: Protected version of greet

client-v2/gen/openapi.ts

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,41 @@
11
/**
22
* Generated by orval v7.2.0 🍺
33
* Do not edit manually.
4-
* My API
4+
* Happened API
55
* OpenAPI spec version: 1.0.0
66
*/
77
import axios from 'axios'
88
import type {
99
AxiosRequestConfig,
1010
AxiosResponse
1111
} from 'axios'
12+
13+
// https://stackoverflow.com/questions/49579094/typescript-conditional-types-filter-out-readonly-properties-pick-only-requir/49579497#49579497
14+
type IfEquals<X, Y, A = X, B = never> = (<T>() => T extends X ? 1 : 2) extends <
15+
T,
16+
>() => T extends Y ? 1 : 2
17+
? A
18+
: B;
19+
20+
type WritableKeys<T> = {
21+
[P in keyof T]-?: IfEquals<
22+
{ [Q in P]: T[P] },
23+
{ -readonly [Q in P]: T[P] },
24+
P
25+
>;
26+
}[keyof T];
27+
28+
type UnionToIntersection<U> =
29+
(U extends any ? (k: U)=>void : never) extends ((k: infer I)=>void) ? I : never;
30+
type DistributeReadOnlyOverUnions<T> = T extends any ? NonReadonly<T> : never;
31+
32+
type Writable<T> = Pick<T, WritableKeys<T>>;
33+
type NonReadonly<T> = [T] extends [UnionToIntersection<T>] ? {
34+
[P in keyof Writable<T>]: T[P] extends object
35+
? NonReadonly<NonNullable<T[P]>>
36+
: T[P];
37+
} : DistributeReadOnlyOverUnions<T>;
38+
1239
export interface GreetingOutputBody {
1340
/** A URL to the JSON Schema for this object. */
1441
readonly $schema?: string;
@@ -47,11 +74,38 @@ export interface ErrorModel {
4774
type?: string;
4875
}
4976

77+
export interface CreateUploadURLRequestBody {
78+
/** A URL to the JSON Schema for this object. */
79+
readonly $schema?: string;
80+
image_key: string;
81+
}
82+
83+
export type CreateUploadURLBodySignedHeaders = {[key: string]: string};
84+
85+
export interface CreateUploadURLBody {
86+
/** A URL to the JSON Schema for this object. */
87+
readonly $schema?: string;
88+
method: string;
89+
signed_headers: CreateUploadURLBodySignedHeaders;
90+
upload_url: string;
91+
}
92+
5093

5194

5295

5396

5497
/**
98+
* @summary Get create upload URL
99+
*/
100+
export const getCreateUploadUrl = <TData = AxiosResponse<CreateUploadURLBody>>(
101+
createUploadURLRequestBody: NonReadonly<CreateUploadURLRequestBody>, options?: AxiosRequestConfig
102+
): Promise<TData> => {
103+
return axios.get(
104+
`/create-upload-url`,options
105+
);
106+
}
107+
108+
/**
55109
* Protected version of greet
56110
* @summary Get a protected greeting
57111
*/
@@ -75,5 +129,6 @@ export const getGreeting = <TData = AxiosResponse<GreetingOutputBody>>(
75129
);
76130
}
77131

132+
export type GetCreateUploadUrlResult = AxiosResponse<CreateUploadURLBody>
78133
export type ProtectedGreetResult = AxiosResponse<GreetingOutputBody>
79134
export type GetGreetingResult = AxiosResponse<GreetingOutputBody>

0 commit comments

Comments
 (0)