init Project ✌️✌️
This commit is contained in:
commit
8a6dc19bdd
42 changed files with 249179 additions and 0 deletions
47
server/data/data.go
Normal file
47
server/data/data.go
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
package data
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
func readFile() map[string]interface{} {
|
||||
file, err := os.Open("data/data.json")
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("Error when open file: %s", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
defer file.Close()
|
||||
|
||||
var data map[string]interface{}
|
||||
|
||||
err = json.NewDecoder(file).Decode(&data)
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("Error when decode file: %s", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
type Data struct {
|
||||
recipe map[string]interface{}
|
||||
}
|
||||
|
||||
func NewData() *Data {
|
||||
return &Data{
|
||||
recipe: readFile(),
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Data) GetRecipe() map[string]interface{} {
|
||||
return d.recipe
|
||||
}
|
||||
|
||||
func (d *Data) GetRecipe01() []interface{} {
|
||||
return d.recipe["Recipe01"].([]interface{})
|
||||
}
|
||||
236126
server/data/data.json
Normal file
236126
server/data/data.json
Normal file
File diff suppressed because one or more lines are too long
5
server/go.mod
Normal file
5
server/go.mod
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
module recipe-manager
|
||||
|
||||
go 1.21.1
|
||||
|
||||
require github.com/go-chi/chi/v5 v5.0.10
|
||||
2
server/go.sum
Normal file
2
server/go.sum
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
github.com/go-chi/chi/v5 v5.0.10 h1:rLz5avzKpjqxrYwXNfmjkrYYXOyLJd37pz53UFHC6vk=
|
||||
github.com/go-chi/chi/v5 v5.0.10/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
|
||||
43
server/main.go
Normal file
43
server/main.go
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := NewServer(3000)
|
||||
|
||||
serverCtx, serverStopCtx := context.WithCancel(context.Background())
|
||||
sig := make(chan os.Signal, 1)
|
||||
signal.Notify(sig, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
|
||||
go func() {
|
||||
<-sig
|
||||
|
||||
shutdownCtx, _ := context.WithTimeout(serverCtx, 30*time.Second)
|
||||
|
||||
go func() {
|
||||
<-shutdownCtx.Done()
|
||||
if shutdownCtx.Err() == context.DeadlineExceeded {
|
||||
log.Println("Shutdown timeout, force exit")
|
||||
}
|
||||
}()
|
||||
|
||||
err := s.Shutdown(shutdownCtx)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
serverStopCtx()
|
||||
}()
|
||||
|
||||
err := s.Run()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
<-serverCtx.Done()
|
||||
}
|
||||
68
server/models/recipe.go
Normal file
68
server/models/recipe.go
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
package models
|
||||
|
||||
type Recipe struct {
|
||||
Timestamps string `json:"timestamps"`
|
||||
MachineSetting MatchineSetting `json:"MachineSetting"`
|
||||
Recipe01 []Recipe01 `json:"Recipe01"`
|
||||
Topping Topping `json:"Topping"`
|
||||
MaterailCode []MaterailCode `json:"MaterailCode"`
|
||||
}
|
||||
|
||||
type MatchineSetting struct {
|
||||
RecipeTag string `json:"RecipeTag"`
|
||||
StrTextShowError []string `json:"strTextShowError"`
|
||||
ConfigNumber int `json:"configNumber"`
|
||||
TemperatureMax int `json:"temperatureMax"`
|
||||
TemperatureMin int `json:"temperatureMin"`
|
||||
}
|
||||
|
||||
type MaterailCode struct {
|
||||
PackageDescription string `json:"PackageDescription"`
|
||||
RefillValuePerStep int `json:"RefillValuePerStep"`
|
||||
MaterialID int `json:"materialID"`
|
||||
MaterialCode string `json:"materialCode"`
|
||||
}
|
||||
|
||||
// export interface MaterialSetting {
|
||||
// AlarmIDWhenOffline: number;
|
||||
// BeanChannel: boolean;
|
||||
// CanisterType?: string;
|
||||
// DrainTimer: number;
|
||||
// IsEquipment: boolean;
|
||||
// LeavesChannel: boolean;
|
||||
// LowToOffline: number;
|
||||
// MaterialDescrption?: string;
|
||||
// MaterialStatus: number;
|
||||
// PowderChannel: boolean;
|
||||
// RefillUnitGram: boolean;
|
||||
// RefillUnitMilliliters: boolean;
|
||||
// RefillUnitPCS: boolean;
|
||||
// ScheduleDrainType: number;
|
||||
// SodaChannel: boolean;
|
||||
// StockAdjust: number;
|
||||
// StrTextShowError: string[];
|
||||
// SyrupChannel: boolean;
|
||||
// id: number;
|
||||
// idAlternate: number;
|
||||
// isUse: boolean;
|
||||
// materialOtherName: string;
|
||||
// materialName: string;
|
||||
// pathOtherName?: string;
|
||||
// pay_rettry_max_count: number;
|
||||
// RawMaterialUnit?: string;
|
||||
// RawMaterialData?: RawMaterialDatum[];
|
||||
// MaterialComment?: string;
|
||||
// MaterialStatusLastUpdate?: string;
|
||||
// feed_mode?: string;
|
||||
// MaterialParameter?: string;
|
||||
// }
|
||||
|
||||
type MaterialSetting struct {
|
||||
AlarmIDWhenOffline int `json:"AlarmIDWhenOffline"`
|
||||
BeanChannel bool `json:"BeanChannel"`
|
||||
CanisterType string `json:"CanisterType"`
|
||||
DrainTimer int `json:"DrainTimer"`
|
||||
}
|
||||
|
||||
type Recipe01 struct{}
|
||||
type Topping struct{}
|
||||
28
server/routers/recipe.go
Normal file
28
server/routers/recipe.go
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
package routers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"recipe-manager/data"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
type RecipeRouter struct {
|
||||
data *data.Data
|
||||
}
|
||||
|
||||
func NewRecipeRouter(data *data.Data) *RecipeRouter {
|
||||
return &RecipeRouter{
|
||||
data: data,
|
||||
}
|
||||
}
|
||||
|
||||
func (rr *RecipeRouter) Route(r *chi.Mux) {
|
||||
r.Route("/recipes", func(r chi.Router) {
|
||||
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(rr.data.GetRecipe())
|
||||
})
|
||||
})
|
||||
}
|
||||
51
server/server.go
Normal file
51
server/server.go
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"recipe-manager/data"
|
||||
"recipe-manager/routers"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
server *http.Server
|
||||
data *data.Data
|
||||
}
|
||||
|
||||
func NewServer(port uint) *Server {
|
||||
return &Server{
|
||||
server: &http.Server{Addr: fmt.Sprintf(":%d", port)},
|
||||
data: data.NewData(),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) Run() error {
|
||||
|
||||
s.server.Handler = createHandler()
|
||||
log.Printf("Server running on %s", s.server.Addr)
|
||||
return s.server.ListenAndServe()
|
||||
}
|
||||
|
||||
func createHandler() http.Handler {
|
||||
r := chi.NewRouter()
|
||||
|
||||
rr := routers.NewRecipeRouter(data.NewData())
|
||||
rr.Route(r)
|
||||
|
||||
r.NotFound(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
json.NewEncoder(w).Encode(map[string]string{"message": fmt.Sprintf("path %s are not exits", r.RequestURI)})
|
||||
})
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
func (s *Server) Shutdown(ctx context.Context) error {
|
||||
return s.server.Shutdown(ctx)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue