Initialize AutoLibrary website with Gitea deployment
This commit is contained in:
commit
18216953e7
101 files changed
+12581
No files matched your search
@@ -0,0 +1,38 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
var (
|
||||
LogFile *os.File
|
||||
)
|
||||
|
||||
func InitLogger() error {
|
||||
logDir := filepath.Join(GetRootPath(), "logs")
|
||||
if err := os.MkdirAll(logDir, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
logPath := filepath.Join(logDir, "webserver.log")
|
||||
file, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
multiWriter := io.MultiWriter(os.Stdout, file)
|
||||
log.SetOutput(multiWriter)
|
||||
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
||||
|
||||
LogFile = file
|
||||
return nil
|
||||
}
|
||||
|
||||
func CloseLogger() {
|
||||
if LogFile != nil {
|
||||
LogFile.Close()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ServeHTTP drains requests before its caller closes databases and files.
|
||||
func ServeHTTP(ctx context.Context, address string, handler http.Handler) error {
|
||||
server := &http.Server{Addr: address, Handler: handler, ReadHeaderTimeout: 10 * time.Second, IdleTimeout: 60 * time.Second}
|
||||
finished := make(chan struct{})
|
||||
defer close(finished)
|
||||
shutdownDone := make(chan struct{})
|
||||
go func() {
|
||||
defer close(shutdownDone)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
timeout, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
if err := server.Shutdown(timeout); err != nil {
|
||||
_ = server.Close()
|
||||
}
|
||||
case <-finished:
|
||||
}
|
||||
}()
|
||||
err := server.ListenAndServe()
|
||||
if errors.Is(err, http.ErrServerClosed) {
|
||||
<-shutdownDone
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
ShanghaiLocation *time.Location
|
||||
)
|
||||
|
||||
func init() {
|
||||
var err error
|
||||
ShanghaiLocation, err = time.LoadLocation("Asia/Shanghai")
|
||||
if err != nil {
|
||||
ShanghaiLocation = time.FixedZone("CST", 8*60*60)
|
||||
}
|
||||
}
|
||||
|
||||
func NowInShanghai() time.Time {
|
||||
return time.Now().In(ShanghaiLocation)
|
||||
}
|
||||
|
||||
func ParseInShanghai(layout, value string) (time.Time, error) {
|
||||
return time.ParseInLocation(layout, value, ShanghaiLocation)
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func GetRootPath() string {
|
||||
// Production binaries resolve files beside the executable; development
|
||||
// commands resolve the current project, including go run's temp binary.
|
||||
working, _ := os.Getwd()
|
||||
executable, _ := os.Executable()
|
||||
for _, directory := range []string{filepath.Dir(executable), working} {
|
||||
if directory == "" {
|
||||
continue
|
||||
}
|
||||
for _, marker := range []string{"config.json", "frontend", "go.mod"} {
|
||||
if _, err := os.Stat(filepath.Join(directory, marker)); err == nil {
|
||||
return directory
|
||||
}
|
||||
}
|
||||
}
|
||||
if working != "" {
|
||||
return working
|
||||
}
|
||||
return filepath.Dir(executable)
|
||||
}
|
||||
Reference in new issue
Block a user