Skip to content

Commit

Permalink
将依赖的库从CDN放到静态文件中,删除一些不必要的功能,支持go embed构建单一可执行文件
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyiwy committed Jul 9, 2022
1 parent 133bad6 commit c9537d9
Show file tree
Hide file tree
Showing 82 changed files with 141,147 additions and 1,102 deletions.
4 changes: 4 additions & 0 deletions build.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SET CGO_ENABLED=0
SET GOOS=linux
SET GOARCH=amd64
go build
13 changes: 0 additions & 13 deletions conf.yaml.default
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,3 @@ http:
enable: false
crt:
key:

database:
host: 127.0.0.1
port: 5432
username:
password:
dbname:

third-party:
amap:
key:
serverchan:
key:
57 changes: 57 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"os"
)

type App struct {
Mode string `yaml:"mode"`
LogFile string `yaml:"log-file"`
}
type Http struct {
Port string `yaml:"port"`
SSL struct {
Enable bool `yaml:"enable"`
Crt string `yaml:"crt"`
Key string `yaml:"key"`
} `yaml:"ssl"`
}

type Config struct {
App App `yaml:"app"`
Http Http `yaml:"http"`
}

var config = &Config{}

func init() {
var conf string
if len(os.Args) == 2 {
conf = os.Args[1]
}
if conf == "" {
conf = "conf.yaml"
}

data, err := ioutil.ReadFile(conf)
if err != nil {
log.Println("Config file not found, use default configs.")
config = &Config{
App: App{
Mode: "debug",
LogFile: "oktools.log",
},
Http: Http{
Port: "8888",
},
}
}

err = yaml.UnmarshalStrict(data, &config)
if err != nil {
log.Fatalln(err)
}
}
49 changes: 2 additions & 47 deletions src/contoller/controler.go → controler.go
Original file line number Diff line number Diff line change
@@ -1,43 +1,12 @@
package contoller
package main

import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"oktools/src/model"
"time"
)

const (
second = 1
minute = 60 * second
hour = 60 * minute
day = 24 * hour
)

var begin = time.Now()

func Uptime(c *gin.Context) {
diff := time.Now().Unix() - begin.Unix()
days := diff / day
hours := (diff - days*day) / hour
minutes := (diff - days*day - hours*hour) / minute
seconds := (diff - minutes*minute - days*day - hours*hour) / second

c.String(http.StatusOK,
fmt.Sprintf("The system launched in %s. already running for %d days, %d hours, %d minues, %d seconds.",
begin.Format("2006-01-02 15:04:05"), days, hours, minutes, seconds),
)
}

func Ping(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"msg": "pong"})
}

func Index(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", gin.H{
"tools": model.GetTools(),
})
c.HTML(http.StatusOK, "index.html", nil)
}

func Color(c *gin.Context) {
Expand All @@ -64,12 +33,6 @@ func FileHash(c *gin.Context) {
c.HTML(http.StatusOK, "file_hash.html", nil)
}

func IPInfo(c *gin.Context) {
c.HTML(http.StatusOK, "ip.html", gin.H{
"IP": c.ClientIP(),
})
}

func JSONView(c *gin.Context) {
c.HTML(http.StatusOK, "json.html", nil)
}
Expand All @@ -78,10 +41,6 @@ func Number(c *gin.Context) {
c.HTML(http.StatusOK, "number.html", nil)
}

func Placeholder(c *gin.Context) {
c.HTML(http.StatusOK, "placeholder.html", nil)
}

func QRCode(c *gin.Context) {
c.HTML(http.StatusOK, "qrcode.html", nil)
}
Expand Down Expand Up @@ -137,7 +96,3 @@ func JSON2YAML(c *gin.Context) {
func PDF2IMG(c *gin.Context) {
c.HTML(http.StatusOK, "pdf2img.html", nil)
}

func Clocks(c *gin.Context) {
c.HTML(http.StatusOK, "clocks.html", nil)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module oktools

go 1.12
go 1.16

require (
github.com/gin-gonic/gin v1.4.0
Expand Down
125 changes: 125 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package main

import (
"embed"
"github.com/gin-gonic/gin"
"html/template"
"log"
"net/http"
_ "net/http/pprof"
"os"
)

//go:embed templates
var tmpl embed.FS

//go:embed static
var assets embed.FS

func main() {
gin.SetMode(config.App.Mode)

if gin.Mode() == gin.ReleaseMode {
gin.DisableConsoleColor()

logfile := config.App.LogFile
if logfile == "" {
log.Fatalln("Please set the log file path!")
}

file, err := os.OpenFile(logfile, os.O_RDWR|os.O_CREATE|os.O_APPEND, os.ModePerm)
if err != nil {
file, err = os.Create(logfile)
if file == nil {
log.Fatalln(err)
}
}

defer func() {
err := file.Close()
if err != nil {
log.Fatalln(err)
}
}()

gin.DefaultWriter = file
log.SetOutput(file)
}

r := initRouter()

var err error

if gin.Mode() == gin.ReleaseMode {
runNoTLS()

err = r.RunTLS(":"+config.Http.Port, config.Http.SSL.Crt, config.Http.SSL.Key)
} else {
err = r.Run(":" + config.Http.Port)
}

if err != nil {
log.Fatalln("Something terrible happened:", err)
}
}

func runNoTLS() {
go func() {
e := gin.Default()
e.SetHTMLTemplate(template.Must(template.New("").ParseFS(tmpl, "templates/*.html")))

e.GET("/*path", func(c *gin.Context) {
uri := c.Request.RequestURI
if "/websocket" == uri {
WebSocket(c)
} else {
c.Redirect(http.StatusMovedPermanently, "https://oktools.net"+uri)
}
})

err := e.Run(":80")
if err != nil {
log.Fatalln("Something terrible happened:", err)
}
}()
}

func initRouter() *gin.Engine {
r := gin.Default()

r.SetHTMLTemplate(template.Must(template.New("").ParseFS(tmpl, "templates/*.html")))
r.Any("/static/*filepath", func(c *gin.Context) {
staticServer := http.FileServer(http.FS(assets))
staticServer.ServeHTTP(c.Writer, c.Request)
})

r.GET("/favicon.ico", func(c *gin.Context) {
c.Header("Cache-Control", "max-age=3153600")
c.File("./static/favicon.ico")
})

r.GET("/", Index)
r.GET("/base64", Base64)
r.GET("/image2base64", Image2Base64)
r.GET("/tinyimg", TinyImage)
r.GET("/hash", Hash)
r.GET("/file-hash", FileHash)
r.GET("/json", JSONView)
r.GET("/number", Number)
r.GET("/qrcode", QRCode)
r.GET("/regex", Regex)
r.GET("/timestamp", Timestamp)
r.GET("/color", Color)
r.GET("/aes", AES)
r.GET("/des", DES)
r.GET("/rsa", RSA)
r.GET("/morse", Morse)
r.GET("/url", URL)
r.GET("/unicode", Unicode)
r.GET("/json2go", JSON2GO)
r.GET("/json2xml", JSON2XML)
r.GET("/json2yaml", JSON2YAML)
r.GET("/pdf2img", PDF2IMG)
r.GET("/websocket", WebSocket)
return r
}
Loading

0 comments on commit c9537d9

Please sign in to comment.