Преглед на файлове

added server initialisation

turos.robert преди 3 седмици
родител
ревизия
343dbd2123
променени са 12 файла, в които са добавени 179 реда и са изтрити 0 реда
  1. 1 0
      .gitignore
  2. 26 0
      Makefile
  3. 19 0
      cmds/webservice/Makefile
  4. 3 0
      cmds/webservice/go.mod
  5. 39 0
      cmds/webservice/main.go
  6. 7 0
      go.work
  7. 28 0
      lib/inout/files.go
  8. 3 0
      lib/inout/go.mod
  9. 3 0
      lib/server/go.mod
  10. 13 0
      lib/server/handlers.go
  11. 30 0
      lib/server/srv.go
  12. 7 0
      lib/server/types.go

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+*bin

+ 26 - 0
Makefile

@@ -0,0 +1,26 @@
+.DEFAULT_GOAL := help
+
+commit:
+	
+	@read -p "Enter commit message:" msg; \
+	git add . && git status && sleep 1 && git commit -m "$$msg"
+
+sync: pull push
+
+push:
+	@git push origin master
+
+pull:
+	@git pull --rebase origin master
+
+all: commit sync
+
+help:
+	@clear
+	@echo "Comenzi disponibile: "
+	@echo "     pull -  ia ultimele modificare din repository"
+	@echo "     commit - adauga in repository modificarile"
+	@echo "     push -  pune toate modificarile in repository"
+	@echo "     all  -  commit pull push"
+	
+

+ 19 - 0
cmds/webservice/Makefile

@@ -0,0 +1,19 @@
+
+BINARY = mynotes.bin
+BUILD_DIR = ./bin/
+CFG_FILE = mynotes_cfg.json
+
+.DEFAULT_GOAL := help
+
+build: 
+	@CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o $(BUILD_DIR)/$(BINARY) .
+
+run: build 
+	@cd $(BUILD_DIR) && ./$(BINARY) --run $(CFG_FILE)
+
+help:
+	@clear
+	@echo "Comenzi disponibile: "
+	@echo "		build - construieste executabilul"
+	@echo "		run - executa programul"
+

+ 3 - 0
cmds/webservice/go.mod

@@ -0,0 +1,3 @@
+module git.linuxit.ro/turos.robert/Site-Cnapsys/cmds/webservice
+
+go 1.25.0

+ 39 - 0
cmds/webservice/main.go

@@ -0,0 +1,39 @@
+package main
+
+import (
+	"fmt"
+	"os"
+
+	"git.linuxit.ro/turos.robert/Site-Cnapsys/lib/server"
+)
+
+func main() {
+	args := os.Args
+
+	switch len(args) {
+	case 3:
+		if args[1] == "--run" {
+			serve(args[2])
+			os.Exit(0)
+		}
+		fmt.Printf("Comanda nerecunoscuta: %s\n", args[1])
+
+	default:
+		fmt.Println("Aplicatia necesita minim 2 parametri")
+	}
+	fmt.Printf("Argumente rulare: %v\n", args)
+}
+
+func serve(filename string) {
+	srv := server.NewServer(filename)
+	checkError(srv.Err)
+	srv.Run()
+}
+
+func checkError(err error) {
+	if err != nil {
+		fmt.Printf("Eroare %+v\n", err)
+		os.Exit(0)
+	}
+
+}

+ 7 - 0
go.work

@@ -0,0 +1,7 @@
+go 1.25.0
+use (
+./cmds/webservice
+
+./lib/inout
+./lib/server
+)

+ 28 - 0
lib/inout/files.go

@@ -0,0 +1,28 @@
+package inout
+
+import (
+	"encoding/json"
+	"fmt"
+	"os"
+)
+
+func FileToObj(filename string, obj any) error {
+	data, err := ReadFile(filename)
+	if err != nil {
+		return err
+	}
+	return json.Unmarshal(data, obj)
+}
+
+func ReadFile(filename string) ([]byte, error) {
+	return os.ReadFile(filename)
+}
+
+func FileToBytes(filename string) []byte {
+	data, err := ReadFile(filename)
+	if err != nil {
+		return fmt.Appendf(nil, "%+v", err)
+	}
+	return data
+
+}

+ 3 - 0
lib/inout/go.mod

@@ -0,0 +1,3 @@
+module git.linuxit.ro/turos.robert/Site-Cnapsys/lib/inout
+
+go 1.25.0

+ 3 - 0
lib/server/go.mod

@@ -0,0 +1,3 @@
+module git.linuxit.ro/turos.robert/Site-Cnapsys/lib/server
+
+go 1.25.0

+ 13 - 0
lib/server/handlers.go

@@ -0,0 +1,13 @@
+package server
+
+import (
+	"net/http"
+
+	"git.linuxit.ro/turos.robert/Site-Cnapsys/lib/inout"
+)
+
+func Wildcard(w http.ResponseWriter, r *http.Request) {
+	if r.Method == "GET" {
+		w.Write(inout.FileToBytes("./templates/index.html"))
+	}
+}

+ 30 - 0
lib/server/srv.go

@@ -0,0 +1,30 @@
+package server
+
+import (
+	"fmt"
+	"net/http"
+
+	"git.linuxit.ro/turos.robert/Site-Cnapsys/lib/inout"
+)
+
+func NewServer(filename string) *Server {
+	srv := Server{}
+	err := inout.FileToObj(filename, &srv)
+	if err != nil {
+		srv.Err = err
+	}
+	return &srv
+}
+
+func (s *Server) Run() {
+	http.HandleFunc("/", Wildcard)
+
+	fmt.Println(s.Port, s.Host)
+
+	fmt.Printf("Server running at http://%s:%s/\n", s.Host, s.Port)
+	err := http.ListenAndServe(fmt.Sprintf("%s:%s", s.Host, s.Port), nil)
+	if err != nil {
+		fmt.Printf("Error starting server: %+v\n", err)
+	}
+
+}

+ 7 - 0
lib/server/types.go

@@ -0,0 +1,7 @@
+package server
+
+type Server struct {
+	Host string `json:"srvhost"`
+	Port string `json:"srvport"`
+	Err  error
+}