go project - add a migration and rename the dockerfiles so that Go is the primary
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/michaeldileo/SafelyYouCodingChallenge/internal/api"
|
||||
"github.com/michaeldileo/SafelyYouCodingChallenge/internal/store"
|
||||
)
|
||||
|
||||
func main() {
|
||||
deviceIDs, err := loadDeviceIDs("devices.csv")
|
||||
if err != nil {
|
||||
log.Fatalf("load devices: %v", err)
|
||||
}
|
||||
|
||||
s := store.New(deviceIDs)
|
||||
log.Printf("loaded %d known devices", len(deviceIDs))
|
||||
|
||||
srv := api.NewServer(s)
|
||||
|
||||
addr := ":6733"
|
||||
fmt.Println("listening on", addr)
|
||||
if err := http.ListenAndServe(addr, srv.Handler()); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// loadDeviceIDs reads devices.csv (header + one device_id per row).
|
||||
// Lives in main on purpose: startup I/O stays at the process edge;
|
||||
// the store only receives already-parsed IDs.
|
||||
func loadDeviceIDs(path string) ([]store.DeviceID, error) {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
r := csv.NewReader(f)
|
||||
|
||||
// Skip header: device_id
|
||||
if _, err := r.Read(); err != nil {
|
||||
return nil, fmt.Errorf("read header: %w", err)
|
||||
}
|
||||
|
||||
var ids []store.DeviceID
|
||||
for {
|
||||
record, err := r.Read()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(record) == 0 || record[0] == "" {
|
||||
continue
|
||||
}
|
||||
ids = append(ids, store.DeviceID(record[0]))
|
||||
}
|
||||
return ids, nil
|
||||
}
|
||||
Reference in New Issue
Block a user