added support for dotenv files and config
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
package main
|
||||
|
||||
import "lib:dotenv"
|
||||
|
||||
Config :: struct {
|
||||
api_url: string,
|
||||
}
|
||||
|
||||
get_config :: proc() -> (^Config, ^Error) {
|
||||
env, _ := dotenv.parse_file(".env", context.temp_allocator)
|
||||
defer dotenv.destroy(env)
|
||||
|
||||
c := new(Config)
|
||||
|
||||
c.api_url = dotenv.get_string(env, "API_URL")
|
||||
|
||||
if err := validate_config(c); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
validate_config :: proc(c: ^Config) -> ^Error {
|
||||
err := new(Error)
|
||||
if c.api_url == "" do err.type = .Config; err.message = "'API_URL' is required"
|
||||
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package main
|
||||
|
||||
import "core:fmt"
|
||||
import "core:strings"
|
||||
ErrorType :: enum {
|
||||
Config,
|
||||
API,
|
||||
}
|
||||
|
||||
Error :: struct {
|
||||
type: ErrorType,
|
||||
message: string,
|
||||
}
|
||||
|
||||
format_error :: proc(err: ^Error) -> string {
|
||||
sb := strings.builder_make(context.temp_allocator)
|
||||
|
||||
return fmt.sbprintf(&sb, "[%s] => %s", err.type, err.message)
|
||||
}
|
||||
|
||||
+8
-7
@@ -24,6 +24,13 @@ universe: [dynamic]Galaxy
|
||||
rng: rand.Default_Random_State
|
||||
|
||||
main :: proc() {
|
||||
|
||||
c: ^Config
|
||||
|
||||
if c, err := get_config(); err != nil {
|
||||
panic(format_error(err))
|
||||
}
|
||||
|
||||
rng = rand.create(0xDE51_0000)
|
||||
context.random_generator = rand.default_random_generator(&rng)
|
||||
|
||||
@@ -103,11 +110,5 @@ draw :: proc() {
|
||||
}
|
||||
|
||||
rl.DrawFPS(10, 10)
|
||||
rl.DrawText(
|
||||
"DESI Explorer — drag to rotate, scroll to zoom",
|
||||
10,
|
||||
34,
|
||||
18,
|
||||
rl.RAYWHITE,
|
||||
)
|
||||
rl.DrawText("DESI Explorer — drag to rotate, scroll to zoom", 10, 34, 18, rl.RAYWHITE)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user