forked from wangyiwy/oktools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
将依赖的库从CDN放到静态文件中,删除一些不必要的功能,支持go embed构建单一可执行文件
- Loading branch information
Showing
82 changed files
with
141,147 additions
and
1,102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.