modified files structure

This commit is contained in:
Paul 2020-01-26 17:12:47 +01:00
parent b831074864
commit 7515cf4344
8 changed files with 45 additions and 116 deletions

45
dip.go
View File

@ -1,45 +0,0 @@
package main
import (
"flag"
"fmt"
//"io/ioutil"
//"os"
//"strings"
"github.com/gobuffalo/packr/v2"
"github.com/labstack/echo/v4"
//"github.com/markbates/pkger"
)
func main() {
e := echo.New()
e.HideBanner = true
var host, port string
flag.Usage = Usage
flag.StringVar(&host, "host", "[::]", "Listen host")
flag.StringVar(&port, "port", "8080", "Listen port")
flag.Parse()
templatesbox := packr.New("templates", "./templates")
staticbox := packr.New("static", "./static")
//t := pkger.Dir("/templatefiles")
//fmt.Println(t)
//test2,_ := t.Open("index.html")
//test3,_ := ioutil.ReadAll(test2)
//fmt.Println(string(test3))
//io.Copy(os.Stdout,test2)
builtTemplates, _ := BuildTemplates(templatesbox)
e.Renderer = builtTemplates
e.GET("/", Dip)
e.GET("/static/*", func(c echo.Context) error {
return Static(staticbox, c)
})
e.Logger.Fatal(e.Start(fmt.Sprintf("%s:%s", host, port)))
}

2
go.mod
View File

@ -1,4 +1,4 @@
module dip module git.paulbsd.com/paulbsd/dip
go 1.13 go 1.13

View File

@ -5,4 +5,4 @@
// and any other packr generated files. // and any other packr generated files.
package main package main
import _ "dip/packrd" import _ "git.paulbsd.com/paulbsd/dip/packrd"

File diff suppressed because one or more lines are too long

21
src/dip.go Normal file
View File

@ -0,0 +1,21 @@
package dip
import (
"git.paulbsd.com/paulbsd/dip/src/ip"
"github.com/labstack/echo/v4"
"net/http"
"strings"
)
// Dip returns webpage
func Dip(c echo.Context) (err error) {
var ip IP
ip.GetIPInfo(c)
if strings.Contains(c.Request().Header.Get(echo.HeaderAccept), echo.MIMETextHTML) {
return c.Render(http.StatusOK, "index.html", ip)
} else {
return c.JSON(http.StatusOK, ip)
}
}

View File

@ -1,15 +1,12 @@
package main package ip
import ( import (
"flag" "flag"
"fmt" "fmt"
"log" "log"
"net" "net"
"net/http"
"os" "os"
"strings"
"github.com/gobuffalo/packr/v2"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"github.com/likexian/whois-go" "github.com/likexian/whois-go"
whoisparser "github.com/likexian/whois-parser-go" whoisparser "github.com/likexian/whois-parser-go"
@ -36,31 +33,6 @@ func (ip *IP) GetIPInfo(c echo.Context) (err error) {
return return
} }
// Dip returns webpage
func Dip(c echo.Context) (err error) {
var ip IP
ip.GetIPInfo(c)
if strings.Contains(c.Request().Header.Get(echo.HeaderAccept), echo.MIMETextHTML) {
return c.Render(http.StatusOK, "index.html", ip)
} else {
return c.JSON(http.StatusOK, ip)
}
}
// Static ...
func Static(box *packr.Box, c echo.Context) (err error) {
name := c.Param("*")
cnt, _ := box.FindString(name)
if strings.HasSuffix(name, ".js") {
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJavaScript)
} else if strings.HasSuffix(name, ".css") {
c.Response().Header().Set(echo.HeaderContentType, "text/css")
}
return c.String(http.StatusOK, cnt)
}
// CheckIPAddress check if ip is an IP address, return err if there's an error // CheckIPAddress check if ip is an IP address, return err if there's an error
func (ip *IP) CheckIPAddress() (err error) { func (ip *IP) CheckIPAddress() (err error) {
addr := net.ParseIP(ip.IP) addr := net.ParseIP(ip.IP)

20
src/static.go Normal file
View File

@ -0,0 +1,20 @@
package static
import (
"github.com/gobuffalo/packr/v2"
"github.com/labstack/echo/v4"
"net/http"
"strings"
)
// Static ...
func Static(box *packr.Box, c echo.Context) (err error) {
name := c.Param("*")
cnt, _ := box.FindString(name)
if strings.HasSuffix(name, ".js") {
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJavaScript)
} else if strings.HasSuffix(name, ".css") {
c.Response().Header().Set(echo.HeaderContentType, "text/css")
}
return c.String(http.StatusOK, cnt)
}

View File

@ -1,13 +1,10 @@
package main package templates
import ( import (
//"bytes"
//"fmt"
"html/template" "html/template"
"io" "io"
"strings" "strings"
//"github.com/gobuffalo/packr"
"github.com/gobuffalo/packr/v2" "github.com/gobuffalo/packr/v2"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
) )