Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reformat code by goimports tool and add Makefile #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build:
GOOS=linux GOARCH=amd64 go build -o api-doc-server
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@
Сервер вернет нам файл по адресу /var/www/common-dir/test.server.handh.ru/index.html

### Сборка
GOOS=linux GOARCH=amd64 go build -o api-doc-server
```
make build
```
34 changes: 17 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package main

// Импортируем всё, что нам может понадобиться
import (
"fmt"
"log"
"net/http"
"io/ioutil"
"os"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"regexp"
)

Expand All @@ -21,7 +21,10 @@ func getRoot() string {
func main() {
http.HandleFunc("/", handler)
log.Println("Run and listen 8383")
http.ListenAndServe(":8383", nil)
err := http.ListenAndServe(":8383", nil)
if err != nil {
fmt.Printf("error during bootstrap server %s", err)
}
}

func handler(iWrt http.ResponseWriter, iReq *http.Request) {
Expand All @@ -30,17 +33,17 @@ func handler(iWrt http.ResponseWriter, iReq *http.Request) {
lGet = "index.html"
}

exp := "(.*):[:digit:]*"
const exp = "(.*):[:digit:]*"
r := regexp.MustCompile(exp)
host := r.FindString(iReq.Host)
if host != ""{
host = host[:len(host) - 1]
if host != "" {
host = host[:len(host)-1]
} else {
host = iReq.Host
}
log.Println("Host: " + host)

if host == "" || host == "localhost" {
if host == "" || host == "localhost" {
lGet = getRoot() + lGet
} else {
lGet = getRoot() + host + "/" + lGet
Expand All @@ -53,11 +56,8 @@ func handler(iWrt http.ResponseWriter, iReq *http.Request) {
func readFile(iFileName string) string {
log.Println("readFile: " + iFileName)
lData, err := ioutil.ReadFile(iFileName)
var lOut string
if !os.IsNotExist(err) {
lOut = string(lData)
} else {
lOut = "404"
if os.IsNotExist(err) {
return "404"
}
return lOut
}
return string(lData)
}