Initial go code (#2)
Reviewed-on: phoenix/textsender-api#2 Co-authored-by: phoenix <kundeng00@pm.me> Co-committed-by: phoenix <kundeng00@pm.me>
This commit is contained in:
@@ -0,0 +1,42 @@
|
|||||||
|
name: Go
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [ main ]
|
||||||
|
pull_request:
|
||||||
|
branches: [ main ]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: ubuntu-24.04 # You can change this to macos-latest or windows-latest if needed
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: Set up Go
|
||||||
|
uses: actions/setup-go@v3
|
||||||
|
with:
|
||||||
|
go-version: '1.24.5' # You can specify a specific version or 'stable'
|
||||||
|
|
||||||
|
- name: Build
|
||||||
|
run: go build -v ./...
|
||||||
|
|
||||||
|
- name: Test
|
||||||
|
run: go test -v ./...
|
||||||
|
|
||||||
|
- name: Run gofmt (optional)
|
||||||
|
# Uncomment to check code formatting with gofmt
|
||||||
|
run: |
|
||||||
|
if [ -n "$(gofmt -l.)" ]; then
|
||||||
|
echo "Go code is not formatted. Please run 'gofmt -w.' to fix it."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
- name: Run golint (optional)
|
||||||
|
# Uncomment to check code style with golint
|
||||||
|
run: |
|
||||||
|
if [ -n "$(golint./...)" ]; then
|
||||||
|
echo "Go code has style issues. Please run 'golint./...' to see them."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
/textsender-api
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "encoding/json"
|
||||||
|
import "fmt"
|
||||||
|
import "log"
|
||||||
|
import "net/http"
|
||||||
|
|
||||||
|
type MessageItem struct {
|
||||||
|
Id int `json:"id"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
}
|
||||||
|
|
||||||
|
const PORT = 8080
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println("textsender-api")
|
||||||
|
|
||||||
|
http.HandleFunc("/api/v1/message/draft", draftMessageHandler)
|
||||||
|
|
||||||
|
log.Println("Starting server", PORT)
|
||||||
|
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||||
|
}
|
||||||
|
|
||||||
|
func draftMessageHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
da := MessageItem{
|
||||||
|
Id: 1,
|
||||||
|
Message: "The Word",
|
||||||
|
}
|
||||||
|
|
||||||
|
respondWithJSON(w, http.StatusOK, da)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper function to send JSON responses
|
||||||
|
func respondWithJSON(w http.ResponseWriter, statusCode int, data interface{}) {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(statusCode)
|
||||||
|
if err := json.NewEncoder(w).Encode(data); err != nil {
|
||||||
|
log.Printf("Error encoding JSON: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user