-
Notifications
You must be signed in to change notification settings - Fork 12
refactor: restructure codebase into modular packages #37
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
47ad016
refactor: restructure codebase into modular packages
iomz ad38662
test: enhance the dead goroutine handling fixing potential memory lea…
iomz 6557abb
fix: prevent concurrent writers while still allowing a new loop once …
iomz 758732d
Improve docstrings and add comprehensive test coverage
iomz 507c2bf
Fix data races and simulator cycle 0 bug
iomz 3e6b187
Fix tests and improve code reliability
iomz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| --- | ||
| name: docker | ||
| on: | ||
| push: | ||
| # Publish `main` as Docker `latest` image. | ||
| branches: | ||
| - main | ||
| # Publish `v1.2.3` tags as releases. | ||
| tags: | ||
| - v*.*.* | ||
| jobs: | ||
| package: | ||
| runs-on: ubuntu-latest | ||
| env: | ||
| dockerfile: Dockerfile | ||
| image_name: iomz/golemu | ||
| platforms: linux/amd64,linux/arm64 | ||
| registry: ghcr.io | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - name: Docker meta | ||
| id: meta | ||
| uses: docker/metadata-action@v5 | ||
| with: | ||
| # list of Docker images to use as base name for tags | ||
| images: | | ||
| ${{ env.registry }}/${{ env.image_name }} | ||
| # generate Docker tags based on the following events/attributes | ||
| tags: | | ||
| type=schedule | ||
| type=ref,event=branch | ||
| type=semver,pattern={{version}} | ||
| type=semver,pattern={{major}}.{{minor}} | ||
| type=semver,pattern={{major}} | ||
| type=sha | ||
| - name: Set up QEMU | ||
| uses: docker/setup-qemu-action@v3 | ||
| - name: Set up Docker Buildx | ||
| uses: docker/setup-buildx-action@v3 | ||
| - name: Login to GitHub Container Registry | ||
| uses: docker/login-action@v3 | ||
| with: | ||
| registry: ${{ env.registry }} | ||
| username: ${{ github.actor }} | ||
| password: ${{ secrets.GITHUB_TOKEN }} | ||
| - name: Build and push Docker image | ||
| uses: docker/build-push-action@v6 | ||
| with: | ||
| context: . | ||
| push: true | ||
| platforms: "${{ env.platforms }}" | ||
| tags: ${{ steps.meta.outputs.tags }} | ||
| labels: ${{ steps.meta.outputs.labels }} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,3 +6,6 @@ | |
| *.swp | ||
| sim/* | ||
| vendor/* | ||
|
|
||
| coverage.out | ||
| golemu | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| // | ||
| // Use of this source code is governed by The MIT License | ||
| // that can be found in the LICENSE file. | ||
|
|
||
| package api | ||
|
|
||
| import ( | ||
| "net/http" | ||
|
|
||
| "github.com/fatih/structs" | ||
| "github.com/gin-gonic/gin" | ||
| "github.com/iomz/go-llrp" | ||
| "github.com/iomz/golemu/tag" | ||
| log "github.com/sirupsen/logrus" | ||
| ) | ||
|
|
||
| // Handler handles API requests | ||
| type Handler struct { | ||
| tagManagerChan chan tag.Manager | ||
| } | ||
|
|
||
| // NewHandler creates a new API handler | ||
| func NewHandler(tagManagerChan chan tag.Manager) *Handler { | ||
| return &Handler{ | ||
| tagManagerChan: tagManagerChan, | ||
| } | ||
| } | ||
|
|
||
| // PostTag handles tag addition requests | ||
| func (h *Handler) PostTag(c *gin.Context) { | ||
| var json []llrp.TagRecord | ||
| if err := c.ShouldBindJSON(&json); err != nil { | ||
| c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request", "details": err.Error()}) | ||
| return | ||
| } | ||
|
|
||
| if res := h.reqAddTag(json); res == "error" { | ||
| c.JSON(http.StatusConflict, gin.H{"error": "One or more tags already exist"}) | ||
| } else { | ||
| c.JSON(http.StatusCreated, gin.H{"message": "Tags added successfully"}) | ||
| } | ||
| } | ||
|
|
||
| // DeleteTag handles tag deletion requests | ||
| func (h *Handler) DeleteTag(c *gin.Context) { | ||
| var json []llrp.TagRecord | ||
| if err := c.ShouldBindJSON(&json); err != nil { | ||
| c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request", "details": err.Error()}) | ||
| return | ||
| } | ||
|
|
||
| if res := h.reqDeleteTag(json); res == "error" { | ||
| c.JSON(http.StatusNotFound, gin.H{"error": "One or more tags not found"}) | ||
| } else { | ||
| c.JSON(http.StatusOK, gin.H{"message": "Tags deleted successfully"}) | ||
| } | ||
| } | ||
|
|
||
| // GetTags handles tag retrieval requests | ||
| func (h *Handler) GetTags(c *gin.Context) { | ||
| tagList := h.reqRetrieveTag() | ||
| c.JSON(http.StatusOK, tagList) | ||
| } | ||
|
|
||
| func (h *Handler) reqAddTag(req []llrp.TagRecord) string { | ||
| validTags := []*llrp.Tag{} | ||
| for _, t := range req { | ||
| tagObj, err := llrp.NewTag(&llrp.TagRecord{ | ||
| PCBits: t.PCBits, | ||
| EPC: t.EPC, | ||
| }) | ||
| if err != nil { | ||
| log.Errorf("error creating tag: %v", err) | ||
| return "error" | ||
| } | ||
|
|
||
| validTags = append(validTags, tagObj) | ||
| } | ||
|
|
||
| for _, tagObj := range validTags { | ||
| add := tag.Manager{ | ||
| Action: tag.AddTags, | ||
| Tags: []*llrp.Tag{tagObj}, | ||
| } | ||
| h.tagManagerChan <- add | ||
| } | ||
|
|
||
| log.Debugf("add %v", req) | ||
| return "add" | ||
| } | ||
|
|
||
| func (h *Handler) reqDeleteTag(req []llrp.TagRecord) string { | ||
| hasError := false | ||
| for _, t := range req { | ||
| tagObj, err := llrp.NewTag(&llrp.TagRecord{ | ||
| PCBits: t.PCBits, | ||
| EPC: t.EPC, | ||
| }) | ||
| if err != nil { | ||
| log.Errorf("error creating tag: %v", err) | ||
| hasError = true | ||
| continue | ||
| } | ||
|
|
||
| deleteCmd := tag.Manager{ | ||
| Action: tag.DeleteTags, | ||
| Tags: []*llrp.Tag{tagObj}, | ||
| } | ||
| h.tagManagerChan <- deleteCmd | ||
| } | ||
|
|
||
| if hasError { | ||
| return "error" | ||
| } | ||
| log.Debugf("delete %v", req) | ||
| return "delete" | ||
| } | ||
|
|
||
| func (h *Handler) reqRetrieveTag() []map[string]interface{} { | ||
| retrieve := tag.Manager{ | ||
| Action: tag.RetrieveTags, | ||
| Tags: []*llrp.Tag{}, | ||
| } | ||
| h.tagManagerChan <- retrieve | ||
| retrieve = <-h.tagManagerChan | ||
| var tagList []map[string]interface{} | ||
| for _, tagObj := range retrieve.Tags { | ||
| t := structs.Map(llrp.NewTagRecord(*tagObj)) | ||
| tagList = append(tagList, t) | ||
| } | ||
| log.Debugf("retrieve: %v", tagList) | ||
| return tagList | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.