Skip to content

Commit

Permalink
fix: architecture, changed map names
Browse files Browse the repository at this point in the history
  • Loading branch information
Dias1c committed Jan 4, 2022
1 parent 43308f6 commit c84a7e2
Show file tree
Hide file tree
Showing 48 changed files with 142 additions and 88 deletions.
Empty file modified LICENSE
100644 → 100755
Empty file.
Empty file modified README.md
100644 → 100755
Empty file.
10 changes: 5 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (
"os"
"strings"

"github.com/Dias1c/lem-in/lemin"
"github.com/Dias1c/lem-in/web"
"github.com/Dias1c/lem-in/internal/lemin"
"github.com/Dias1c/lem-in/internal/web"
)

func main() {
if len(os.Args) != 2 {
fmt.Println("ERROR: Program takes only 1 argument (fileName or --flags)!")
fmt.Println("ERROR: Program takes argument (fileName or --flags)!")
os.Exit(1)
}
argument := os.Args[1]
Expand All @@ -26,12 +26,12 @@ func main() {
if *port != "" {
web.RunServer(*port)
} else if *filename != "" {
lemin.RunProgramWithFile(*filename)
lemin.RunProgramWithFile(*filename, true)
} else { // Default = Help
flag.Usage()
os.Exit(1)
}
} else { // Default
lemin.RunProgramWithFile(argument)
lemin.RunProgramWithFile(argument, false)
}
}
15 changes: 0 additions & 15 deletions general/program.go

This file was deleted.

Empty file modified go.mod
100644 → 100755
Empty file.
8 changes: 8 additions & 0 deletions internal/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Directory "internal"

The insides of the program. If you only need a console program, you can get rid of the `web`

## About subdirectories

- `lemin` - all logic of lemin here
- `web` - web logic here, depends on `lemin`
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
63 changes: 63 additions & 0 deletions internal/lemin/public.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package lemin

import (
"bufio"
"fmt"
"io"
"os"
"strings"
)

// RunProgramWithFile - path is filepath, writes result to output. Close program if has error.
func RunProgramWithFile(path string, showContent bool) {
err := WriteResultByFilePath(os.Stdout, path, showContent)
if err != nil {
fmt.Printf("ERROR: %v\n", err.Error())
os.Exit(1)
}
}

// WriteResultByFilePath - path is filepath.
func WriteResultByFilePath(w io.Writer, path string, writeContent bool) error {
file, err := os.Open(path)
defer file.Close()
if err != nil {
return err
} else if fInfo, _ := file.Stat(); fInfo.IsDir() {
err = fmt.Errorf("%v is directory", fInfo.Name())
return err
}

scanner := bufio.NewScanner(file)
result, err := GetResult(scanner)
if err != nil {
return err
}
if writeContent {
file.Seek(0, io.SeekStart)
_, err = io.Copy(w, file)
if err != nil {
return err
}
fmt.Fprint(w, "\n\n# result\n")
}
result.WriteResult(w)

return nil
}

// WriteResultByContent - using for Web, inputs writer for write result, writes nothing if returns error
func WriteResultByContent(w io.Writer, content string, writeContent bool) error {
scanner := bufio.NewScanner(strings.NewReader(content))
result, err := GetResult(scanner)
if err != nil {
return err
}

if writeContent {
fmt.Fprintf(w, "%v\n\n", content)
}
result.WriteResult(w)

return nil
}
6 changes: 3 additions & 3 deletions lemin/result.go → internal/lemin/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package lemin
import (
"bufio"

"github.com/Dias1c/lem-in/lemin/anthill"
"github.com/Dias1c/lem-in/internal/lemin/anthill"
)

// getResult - returns result, nil if shortest disjoint paths was found
func getResult(scanner *bufio.Scanner) (*anthill.Result, error) {
// GetResult - returns result, nil if shortest disjoint paths was found
func GetResult(scanner *bufio.Scanner) (*anthill.Result, error) {
terrain := anthill.CreateAnthill()
var err error
for scanner.Scan() {
Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion web/init.go → internal/web/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"regexp"
)

func validatePort(port string) error {
// ValidatePort - check port for valid. Returns error on invalid
func ValidatePort(port string) error {
pattern, err := regexp.Compile(`^:[\d]{1,5}$`)
if err != nil {
return err
Expand Down
20 changes: 10 additions & 10 deletions web/public.go → internal/web/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,27 @@ import (
"net/http"
"time"

general "github.com/Dias1c/lem-in/general"
routes "github.com/Dias1c/lem-in/web/routes"
routesApi "github.com/Dias1c/lem-in/web/routes/api"
routes "github.com/Dias1c/lem-in/internal/web/routes"
routesApi "github.com/Dias1c/lem-in/internal/web/routes/api"
)

// RunServer - starts server with setted port
func RunServer(port string) {
func RunServer(port string) error {
var err error
err = validatePort(port)
err = ValidatePort(port)
if err != nil {
general.CloseProgram(err)
return err
}
err = routes.InitTemplates()
if err != nil {
general.CloseProgram(err)
return err
}

// Init Handlers + Run Server
mux := http.NewServeMux()
// FS
assets := http.FileServer(http.Dir("web/public/assets/"))
mux.Handle("/assets/", http.StripPrefix("/assets/", assets))
fs := http.FileServer(http.Dir("internal/web/public/assets/"))
mux.Handle("/assets/", http.StripPrefix("/assets/", fs))
maps := http.FileServer(http.Dir("maps/"))
mux.Handle("/maps/", http.StripPrefix("/maps/", maps))
// Pages
Expand All @@ -44,6 +43,7 @@ func RunServer(port string) {
log.Printf("Server started on http://localhost%v", port)
err = server.ListenAndServe()
if err != nil {
general.CloseProgram(err)
return err
}
return nil
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions web/routes/api/lemin.go → internal/web/routes/api/lemin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"fmt"
"net/http"

lemin "github.com/Dias1c/lem-in/lemin"
helper "github.com/Dias1c/lem-in/web/helper"
lemin "github.com/Dias1c/lem-in/internal/lemin"
helper "github.com/Dias1c/lem-in/internal/web/helper"
)

// LeminHandler - handler func which inputs data about graph in json
Expand Down Expand Up @@ -49,7 +49,7 @@ Relations (roomName-roomName)
return
}
// Start Match result
if lErr := lemin.WriteResultByContent(w, data.Content); lErr != nil {
if lErr := lemin.WriteResultByContent(w, data.Content, true); lErr != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "%v", lErr.Error())
return
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion web/routes/index.go → internal/web/routes/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package routes
import (
"net/http"

helper "github.com/Dias1c/lem-in/web/helper"
helper "github.com/Dias1c/lem-in/internal/web/helper"
)

// IndexHandler - Main Page handler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var Templates *template.Template

// InitTemplates - init Templates
func InitTemplates() error {
files, err := template.ParseFiles("web/public/index.html", "web/public/error.html")
files, err := template.ParseFiles("internal/web/public/index.html", "internal/web/public/error.html")
if err != nil {
return err
}
Expand Down
49 changes: 0 additions & 49 deletions lemin/public.go

This file was deleted.

11 changes: 11 additions & 0 deletions maps/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Directory "maps"

This directory is responsible for maps

## Subdirectories

- `audit` - maps for audit
- `bhandary` - maps for check bhandary algo
- `custom` - maps created for fun
- `default` - default maps
- `planets` - big maps as with planets name
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions pkg/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Directory "pkg"

Here is packages wich you can use
22 changes: 22 additions & 0 deletions pkg/lemin/public.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package lemin

import (
"io"

lemin "github.com/Dias1c/lem-in/internal/lemin"
)

// RunProgramWithFile - path is filepath, writes result to output. Close program if has error.
func RunProgramWithFile(path string, showContent bool) {
lemin.RunProgramWithFile(path, showContent)
}

// WriteResultByFilePath - path is filepath.
func WriteResultByFilePath(w io.Writer, path string, writeContent bool) error {
return lemin.WriteResultByFilePath(w, path, writeContent)
}

// WriteResultByContent - using for Web, inputs writer for write result, writes nothing if returns error
func WriteResultByContent(w io.Writer, content string, writeContent bool) error {
return lemin.WriteResultByContent(w, content, writeContent)
}
10 changes: 10 additions & 0 deletions pkg/web/public.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package web

import (
web "github.com/Dias1c/lem-in/internal/web"
)

// RunServer - starts server with setted port
func RunServer(port string) {
web.RunServer(port)
}

0 comments on commit c84a7e2

Please sign in to comment.