Skip to content

Commit

Permalink
Simplified user interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Boernsman authored and Boernsman committed Oct 4, 2024
1 parent a75f8a3 commit 2705db1
Show file tree
Hide file tree
Showing 20 changed files with 169 additions and 10,313 deletions.
12 changes: 7 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,23 @@ RUN go build -o whereisit .
# Use a smaller image for deployment
FROM alpine:3.18

WORKDIR /app

# Install certificates for HTTPS
RUN apk --no-cache add ca-certificates

# Create a folder for certificates (assuming you have your own SSL cert and key)
RUN mkdir -p /etc/ssl/certs && mkdir -p /etc/ssl/private

# Copy the SSL certificates (adjust paths as necessary)
COPY ./certs/server.crt /etc/ssl/certs/
COPY ./certs/server.key /etc/ssl/private/
COPY ./server.crt* /etc/ssl/certs/
COPY ./server.key* /etc/ssl/private/

# Copy the built Go binary from the builder
COPY --from=builder /app/main /app/main
COPY --from=builder /app/whereisit /app/whereisit

# Expose HTTP on port 80 and HTTPS on port 443
EXPOSE 80 443
# Expose HTTP and HTTPS
EXPOSE 8180 443

# Command to run the executable
CMD ["/app/whereisit"]
50 changes: 31 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ type Device struct {
Added time.Time `json:"added"`
}

type Credentials struct {
Username string
Password string
APIKey string
type Config struct {
BasicAuthEnabled bool
Username string
Password string
ApiKeyAuthEnabled bool
APIKey string
}

func LoadCredentials(primaryPath, fallbackPath string) (*Credentials, error) {
func LoadConfiguration(primaryPath, fallbackPath string) (*Config, error) {
// Check if the primary file exists
if _, err := os.Stat(primaryPath); os.IsNotExist(err) {
log.Printf("Primary file %s not found, trying fallback file %s\n", primaryPath, fallbackPath)
Expand All @@ -55,27 +57,33 @@ func LoadCredentials(primaryPath, fallbackPath string) (*Credentials, error) {
return nil, fmt.Errorf("failed to load ini file: %w", err)
}

username := cfg.Section("auth").Key("username").String()
password := cfg.Section("auth").Key("password").String()
apiKey := cfg.Section("api").Key("api_key").String()
var config Config

if username == "" || password == "" || apiKey == "" {
return nil, fmt.Errorf("missing required credentials in ini file")
}
config.BasicAuthEnabled, _ = cfg.Section("basic_auth").Key("enabled").Bool()
if config.BasicAuthEnabled {
config.Username = cfg.Section("basic_auth").Key("username").String()
config.Password = cfg.Section("basic_auth").Key("password").String()

return &Credentials{
Username: username,
Password: password,
APIKey: apiKey,
}, nil
if config.Username == "" || config.Password == "" {
return nil, fmt.Errorf("missing required basic auth credentials in ini file")
}
}
config.ApiKeyAuthEnabled, _ = cfg.Section("api").Key("api_key_enabled").Bool()
if config.ApiKeyAuthEnabled {
config.APIKey = cfg.Section("api").Key("api_key").String()
if config.APIKey == "" {
return nil, fmt.Errorf("missing required credentials in ini file")
}
}
return &config, nil
}

func main() {

primaryIniFilePath := "/etc/whereisit.ini"
fallbackIniFilePath := "./whereisit.ini"

credentials, err := LoadCredentials(primaryIniFilePath, fallbackIniFilePath)
config, err := LoadConfiguration(primaryIniFilePath, fallbackIniFilePath)
if err != nil {
log.Fatalf("Error loading credentials: %v", err)
}
Expand Down Expand Up @@ -106,8 +114,12 @@ func main() {
slog.SetLogLoggerLevel(slog.LevelDebug)
apiRouter.Use(logRequest)
}
apiRouter.Use(KeyAuth(credentials.APIKey))
apiRouter.Use(BasicAuthMiddleware(credentials.Username, credentials.Password))
if config.ApiKeyAuthEnabled {
apiRouter.Use(KeyAuth(config.APIKey))
}
if config.BasicAuthEnabled {
apiRouter.Use(BasicAuthMiddleware(config.Username, config.Password))
}
apiRouter.HandleFunc("/register", RegisterDevice).Methods("POST")
apiRouter.HandleFunc("/devices", ListDevices).Methods("GET")
apiRouter.HandleFunc("/alldevices", ListAllDevices).Methods("GET")
Expand Down
21 changes: 0 additions & 21 deletions public/bulma-0.1.2/LICENSE

This file was deleted.

Loading

0 comments on commit 2705db1

Please sign in to comment.