initial commit Chapter 1

This commit is contained in:
Tyrel Souza 2023-05-04 13:17:27 -04:00
commit 0a07dec498
No known key found for this signature in database
GPG Key ID: F6582CF1308A2360
5 changed files with 140 additions and 0 deletions

12
cmd/server/main.go Normal file
View File

@ -0,0 +1,12 @@
package main
import (
"log"
"gitea.tyreldev/tyrel/proglog/internal/server"
)
func main() {
srv := server.NewHTTPServer(":8080")
log.Fatal(srv.ListenAndServe())
}

5
go.mod Normal file
View File

@ -0,0 +1,5 @@
module gitea.tyreldev/tyrel/proglog
go 1.20
require github.com/gorilla/mux v1.8.0 // indirect

2
go.sum Normal file
View File

@ -0,0 +1,2 @@
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=

82
internal/server/http.go Normal file
View File

@ -0,0 +1,82 @@
package server
import (
"encoding/json"
"net/http"
"github.com/gorilla/mux"
)
func NewHTTPServer(addr string) *http.Server {
httpsrv := newHttpServer()
r := mux.NewRouter()
r.HandleFunc("/", httpsrv.handleProduce).Methods("POST")
r.HandleFunc("/", httpsrv.handleConsume).Methods("GET")
return &http.Server{
Addr: addr,
Handler: r,
}
}
type httpServer struct {
Log *Log
}
func newHttpServer() *httpServer {
return &httpServer{
Log: NewLog(),
}
}
type ProduceRequest struct {
Record Record `json:"record"`
}
type ProduceResponse struct {
Offset uint64 `json:"offset"`
}
type ConsumeRequest struct {
Offset uint64 `json:"offset"`
}
type ConsumeResponse struct {
Record Record `json:"record"`
}
func (s *httpServer) handleProduce(w http.ResponseWriter, r *http.Request) {
var req ProduceRequest
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
off, err := s.Log.Append(req.Record)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
res := ProduceResponse{Offset: off}
err = json.NewEncoder(w).Encode(res)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func (s *httpServer) handleConsume(w http.ResponseWriter, r *http.Request) {
var req ConsumeRequest
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
record, err := s.Log.Read(req.Offset)
if err == ErrOffsetNotFound {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
res := ConsumeResponse{Record: record}
err = json.NewEncoder(w).Encode(res)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}

39
internal/server/log.go Normal file
View File

@ -0,0 +1,39 @@
package server
import (
"fmt"
"sync"
)
type Log struct {
mu sync.Mutex
records []Record
}
func NewLog() *Log {
return &Log{}
}
func (c *Log) Append(record Record) (uint64, error) {
c.mu.Lock()
defer c.mu.Unlock()
record.Offset = uint64(len(c.records))
c.records = append(c.records, record)
return record.Offset, nil
}
func (c *Log) Read(offset uint64) (Record, error) {
c.mu.Lock()
defer c.mu.Unlock()
if offset >= uint64(len(c.records)) {
return Record{}, ErrOffsetNotFound
}
return c.records[offset], nil
}
type Record struct {
Value []byte `json:"value"`
Offset uint64 `json:"offset"`
}
var ErrOffsetNotFound = fmt.Errorf("offset not found")