Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

various fixes, and idiomatic Go #5

Merged
merged 5 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 1 addition & 2 deletions notecard/Dockerfile.aarch64
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ FROM balenalib/aarch64-alpine-golang:3.18-build as builder
WORKDIR /usr/src/app
COPY . ./

RUN go install && \
go build -o bin/notecard
RUN go build -o bin/notecard

FROM balenalib/aarch64-alpine:3.18-run as runner

Expand Down
3 changes: 1 addition & 2 deletions notecard/Dockerfile.amd64
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ FROM balenalib/amd64-alpine-golang:3.18-build as builder
WORKDIR /usr/src/app
COPY . ./

RUN go install && \
go build -o bin/notecard
RUN go build -o bin/notecard

FROM balenalib/amd64-alpine:3.18-run as runner

Expand Down
3 changes: 1 addition & 2 deletions notecard/Dockerfile.armv7hf
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ FROM balenalib/armv7hf-alpine-golang:3.18-build as builder
WORKDIR /usr/src/app
COPY . ./

RUN go install && \
go build -o bin/notecard
RUN go build -o bin/notecard

FROM balenalib/armv7hf-alpine:3.18-run as runner

Expand Down
3 changes: 1 addition & 2 deletions notecard/Dockerfile.template
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ FROM balenalib/%%BALENA_ARCH%%-alpine-golang:3.18-build as builder
WORKDIR /usr/src/app
COPY . ./

RUN go install && \
go build -o bin/notecard
RUN go build -o bin/notecard

FROM balenalib/%%BALENA_ARCH%%-alpine:3.18-run as runner

Expand Down
115 changes: 53 additions & 62 deletions notecard/main.go
Original file line number Diff line number Diff line change
@@ -1,107 +1,98 @@
package main

import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"

"github.com/blues/note-go/notecard"
)

type Transport string

const (
Serial Transport = "serial"
I2C Transport = "i2c"
Serial = "serial"
I2C = "i2c"
)

var card *notecard.Context
type server struct {
card *notecard.Context
}

func serveNotecard(w http.ResponseWriter, req *http.Request) {
func handleError(w http.ResponseWriter, err error, msg string) {
err_str := fmt.Sprintf("%s: %v", msg, err)
http.Error(w, err_str, http.StatusInternalServerError)
log.Print(err_str)
}

if req.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
fmt.Fprintf(w, "Method not allowed")
return
func (s *server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if s.card == nil {
log.Fatal("notecard not initialized")
}

var data map[string]interface{}
if err := json.NewDecoder(req.Body).Decode(&data); err != nil {
http.Error(w, fmt.Sprintf("Error decoding JSON: %v", err), http.StatusBadRequest)
if req.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
log.Printf("%s: Method not allowed", w)
return
}

for key, value := range data {
fmt.Printf("%s: %v\n", key, value)
}

jsonString, err := json.Marshal(data)
body, err := io.ReadAll(req.Body)
if err != nil {
fmt.Println("Error encoding JSON:", err)
w.WriteHeader(http.StatusInternalServerError)
panic(err)
}

// Print the resulting JSON string
fmt.Println(string(jsonString))

note_rsp, err := card.Transaction(data)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
panic(err)
handleError(w, err, "while reading request body")
return
}
req.Body.Close()

note_rsp_json, err := json.Marshal(note_rsp)
note_rsp, err := s.card.TransactionJSON(body)
if err != nil {
fmt.Println("Error encoding JSON:", err)
w.WriteHeader(http.StatusInternalServerError)
panic(err)
handleError(w, err, "while performing a card transaction")
return
}

// Set the Content-Type header to application/json
w.Header().Set("Content-Type", "application/json")
w.Write(note_rsp_json)

w.WriteHeader(http.StatusOK)
_, err = w.Write(note_rsp)
if err != nil {
log.Printf("error writing response: %v", err)
return
}
Bucknalla marked this conversation as resolved.
Show resolved Hide resolved
}

func setupNotecard(protocol Transport) *notecard.Context {
var card *notecard.Context
var err error
func setupNotecard(protocol string) (*notecard.Context, error) {
log.Printf("Setting up Notecard with protocol: %s\n", protocol)

fmt.Printf("Setting up Notecard with protocol: %s\n", protocol)
if protocol != Serial && protocol != I2C {
return nil, fmt.Errorf("unsupported transport protocol: %v", protocol)
}

if protocol == Transport("serial") {
card, err = notecard.OpenSerial("/dev/tty.usbmodemNOTE1", 9600)
if err != nil {
fmt.Printf("Error opening Notecard: %v\n", err)
panic(err)
}
} else if protocol == Transport("i2c") {
card, err = notecard.OpenI2C("/dev/i2c-1", 0x17)
if protocol == Serial {
card, err := notecard.OpenSerial("/dev/tty.usbmodemNOTE1", 9600)
if err != nil {
fmt.Printf("Error opening Notecard: %v\n", err)
panic(err)
return nil, fmt.Errorf("error opening Notecard: %v", err)
}
} else {
fmt.Printf("Missing transport protocol\n")
return card, nil
}

card, err := notecard.OpenI2C("/dev/i2c-1", 0x17)
if err != nil {
return nil, fmt.Errorf("error opening Notecard: %v", err)
}
return card
return card, nil
}

func main() {
var i interface{} = os.Getenv("NOTECARD_TRANSPORT")
transport, err := i.(Transport)

if !err {
fmt.Printf("Error getting transport protocol, defaulting to I2C...\n")
transport := os.Getenv("NOTECARD_TRANSPORT")
if transport == "" {
log.Printf("transport protocol not provided, defaulting to I2C...")
transport = I2C
}

card = setupNotecard(transport)
card, err := setupNotecard(transport)
if err != nil {
log.Fatalf("while setting up notecard: %v", err)
}
defer card.Close()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

we don't really care given our lifecycle, but this is going to fail (panic even), if the setup above failed (because card will be nil).


http.HandleFunc("/", serveNotecard)
http.Handle("/", &server{card: card})
http.ListenAndServe(":3434", nil)
}
Loading