memos/bin/server/main.go

75 lines
2.2 KiB
Go
Raw Normal View History

2022-02-03 10:32:03 +03:00
package main
2022-08-24 16:53:12 +03:00
import (
2023-02-03 05:30:18 +03:00
"net/http"
2022-08-24 16:53:12 +03:00
"os"
2023-02-03 05:30:18 +03:00
"os/signal"
"syscall"
2022-08-24 16:53:12 +03:00
_ "github.com/mattn/go-sqlite3"
2022-09-05 16:14:17 +03:00
"context"
"fmt"
"github.com/usememos/memos/server"
"github.com/usememos/memos/server/profile"
2022-08-24 16:53:12 +03:00
)
2022-02-03 10:32:03 +03:00
2022-09-05 16:14:17 +03:00
const (
greetingBanner = `
`
)
2023-02-03 05:30:18 +03:00
func main() {
2023-01-03 15:05:37 +03:00
profile, err := profile.GetProfile()
if err != nil {
2023-02-03 05:30:18 +03:00
fmt.Printf("failed to get profile, error: %+v\n", err)
return
2023-01-03 15:05:37 +03:00
}
println("---")
println("profile")
println("mode:", profile.Mode)
println("port:", profile.Port)
println("dsn:", profile.DSN)
println("version:", profile.Version)
println("---")
2022-09-05 16:14:17 +03:00
2023-02-03 05:30:18 +03:00
ctx, cancel := context.WithCancel(context.Background())
s, err := server.NewServer(ctx, profile)
if err != nil {
2023-02-03 05:30:18 +03:00
cancel()
fmt.Printf("failed to create server, error: %+v\n", err)
return
2022-09-05 16:14:17 +03:00
}
2023-02-03 05:30:18 +03:00
c := make(chan os.Signal, 1)
// Trigger graceful shutdown on SIGINT or SIGTERM.
// The default signal sent by the `kill` command is SIGTERM,
// which is taken as the graceful shutdown signal for many systems, eg., Kubernetes, Gunicorn.
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
sig := <-c
fmt.Printf("%s received.\n", sig.String())
s.Shutdown(ctx)
cancel()
}()
2022-09-05 16:14:17 +03:00
println(greetingBanner)
fmt.Printf("Version %s has started at :%d\n", profile.Version, profile.Port)
2023-02-03 05:30:18 +03:00
if err := s.Start(ctx); err != nil {
if err != http.ErrServerClosed {
fmt.Printf("failed to start server, error: %+v\n", err)
cancel()
}
2022-08-24 16:53:12 +03:00
}
2023-02-03 05:30:18 +03:00
// Wait for CTRL-C.
<-ctx.Done()
2022-02-03 10:32:03 +03:00
}